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

• 05-28-2010
Programmer_P
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?
• 05-28-2010
jerimo
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.
• 05-28-2010
C_ntua
Quote:

Originally Posted by Programmer_P
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
• 05-28-2010
iMalc
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.
• 05-29-2010
jeffcobb
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.```
• 05-29-2010
iMalc
Quote:

Code:

`char array[2,3];`

Perhaps you're getting confused with the syntax from a different language here?
• 05-29-2010
jeffcobb
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```
• 05-29-2010
grumpy
Quote:

Originally Posted by jerimo
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.
• 05-29-2010
Programmer_P
Quote:

Originally Posted by iMalc
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.
Quote:

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.
• 05-29-2010
Programmer_P
Quote:

Originally Posted by C_ntua
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.