Thread: Assigning different values to two-dimensional array elements at the same time?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Assigning different values to two-dimensional array elements at the same time?

    Hey, I was wondering if its possible to assign elements of a two-dimensional array different values at the same time, and if so, how to do it. Also, can I make each dimension of the array a different size than the other dimension, or does the size of both have to be the same?

  2. #2
    Your imaginary friend
    Join Date
    Jan 2010
    Location
    Canada
    Posts
    76
    A computer can't do anything at the same time, but as it's blazingly fast it seems that it does everything at the same instent;
    So basic-ly you just make the assigning of values one after each other.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Programmer_P View Post
    Hey, I was wondering if its possible to assign elements of a two-dimensional array different values at the same time, and if so, how to do it. Also, can I make each dimension of the array a different size than the other dimension, or does the size of both have to be the same?
    They can be different, like an MxN array.
    I am not sure what you mean with the different value, give some pseudo-code

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You can get the effect of atomicity (stuff happens at a single moment) by protecting the appropriate data with locking such as critical sections. Nothing else is able to read or touch the data in a partially updated state.
    However, this is only relevant when one is doing multithreading, which I don't think you are doing or actually asking about. When there is only one thread, there is no need for such a question.
    Perhaps rather than being about "the same time" your question is more about "in the same line of code"?

    As for your second question, you can represent a multi-dimensional array in several different ways. e.g. int a[2][3][4][5]; would be a four-dimensional array with 120 elements. Each dimension being of a different size.
    You can even have ragged sized dimensions such as an array of strings where each string is potentially a different length. It's all in how you set it up.

    If I've again incorrectly guessed what you were actually trying to ask, then perhaps you could explain a little better next time.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Well I don't know why on earth you feel the need to but if it is a matter of setting two values to two different 2 dimensional array just to show you can do it, a very carefully crafted union should do it. But why, that is the question and the setup for the above would probably take more overhead than simply

    Array[x][y] = 5;
    Array(x][z] = 4;

    I am not saying the union trick would be useful but within certain parameters it is possible.
    Code:
    union Foo
    {
          long long lValue;
          char array[2,3];
    };
    
    struct Foo foo;
    foo.lValue = 214923; <= this would have the effect of setting more than one element at a time.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    char array[2,3];
    Perhaps you're getting confused with the syntax from a different language here?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    This code works and sets all members of a 2x4 array in one go:
    Code:
    typedef unsigned long long Biglong;
    union Myunion
    {
       Biglong val;
       uint8_t buf[2][4];
    };
    // then
       cout << "foo_test 1.0" << endl;
       union Myunion U;
       memset(&U, 0, sizeof( union Myunion));
       int x,y;
       for(x = 0; x < 2; x++)
       {
          for(y = 0; y < 4; y++)
          {
    	 char out[255];
    	 sprintf(out, "\tU[%d][%d] = %d\n", x, y, (int)U.buf[x][y]);
    	 printf(out);
          }
       }
       cout << "\n\nsetting all with one single command..." << endl;
       Biglong l =  18446744073709551615;
       U.val = l;
       for(x = 0; x < 2; x++)
       {
          for(y = 0; y < 4; y++)
          {
    	 char achOut[255];
    	 sprintf(achOut, "\tU[%d][%d] = %d\n", x, y, (int)U.buf[x][y]);
    	 printf(achOut);
          }
       }
       return 0;
    Produces:
    Code:
    foo_test 1.0
    	U[0][0] = 0
    	U[0][1] = 0
    	U[0][2] = 0
    	U[0][3] = 0
    	U[1][0] = 0
    	U[1][1] = 0
    	U[1][2] = 0
    	U[1][3] = 0
    
    
    setting all with one single command...
    	U[0][0] = 255
    	U[0][1] = 255
    	U[0][2] = 255
    	U[0][3] = 255
    	U[1][0] = 255
    	U[1][1] = 255
    	U[1][2] = 255
    	U[1][3] = 255
    j
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by jerimo View Post
    A computer can't do anything at the same time, but as it's blazingly fast it seems that it does everything at the same instent;
    So basic-ly you just make the assigning of values one after each other.
    That's no longer true, thanks to multiprocessor systems and multicore processors, that allow operations to be performed concurrently (as long as the results of those operations don't simultaneously whack the same memory locations, registers, etc).

    The techniques to do it are outside the scope of standard C and C++. However, most modern operating systems do support threading, and modern developers can use thread libraries. Problem is, thread libraries are system dependent.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by iMalc View Post
    Perhaps rather than being about "the same time" your question is more about "in the same line of code"?
    Yep, that's what I meant.
    As for your second question, you can represent a multi-dimensional array in several different ways. e.g. int a[2][3][4][5]; would be a four-dimensional array with 120 elements. Each dimension being of a different size.
    You can even have ragged sized dimensions such as an array of strings where each string is potentially a different length. It's all in how you set it up.

    If I've again incorrectly guessed what you were actually trying to ask, then perhaps you could explain a little better next time.
    Thank you. That answers that. Now I'll still waiting for an answer to my first question.

  10. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by C_ntua View Post
    They can be different, like an MxN array.
    I am not sure what you mean with the different value, give some pseudo-code
    Ok...

    Start off with two strings, one called "sourceString" and another called "searchString".
    Find out if sourceString contains searchString. To do this, I iterate through both strings, using a nested for loop inside another for loop to iterate through the sourceString, while the outer for loop loops through the search string. For each character of the searchString, iterate through the entire sourceString, checking for character matches. Assuming we found a 'same' character, we then add the indexes of the character match in both the sourceString and the searchString. I wanted to use a two-dimensional int array for that, with the idea being that the left-hand dimension of the array would be a size the number of 'same' characters found in the sourceString and would contain the 'same' characters' indexes of the sourceString, while the right-hand dimension of the array would be a size the number of 'same' characters found in the searchString. Obviously, assuming "searchStringSize" is less than "sourceStringSize", those two dimensions would be a different size, hence the second question. And as to the first question, I needed a way to store the current iteration number of the inner for loop in the left-hand dimension of the 2D array (i.e. for the sourceString indexes) at the same "time" (or rather, the "same line of code", as y'all pointed out) that I store the current iteration number of the outer loop in the right-hand dimension of the 2D array for the indexes of the 'same' characters in the searchString.

    I would then add some more logic to find out if the 'same' characters are grouped together and follow the same order in both strings. But I ended up just using two int vectors to store the indexes instead.
    Last edited by Programmer_P; 05-29-2010 at 10:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  2. assigning values to multidimensional array
    By jjj93421 in forum C++ Programming
    Replies: 2
    Last Post: 08-01-2004, 04:31 PM
  3. assigning values in file to an array
    By divinyl in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 08:33 AM
  4. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 10:15 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM