*** Declaring Array inside another Array ***
-
Hi Everyone, How do I declare an Array inside another Array? For example, I have the following two arrays: ///////////////////////////////////////////////////////// int UserInputSize1, UserInputSize2; int *array1; array1= new int[UserInputSize1]; int *array2; array2=new int[UserInputSize2]; //the size of the arrays will be determined by the user input during runtime. /////////////////////////////////////////////////////// How do I declare them so under array1, it’ll contain the other array? So for example, if the size of array1 is 3 and the size of array2 is 10, then under every element in array1, I’ll be able to store 10 numbers. Then I’ll be able to store a total of 30 numbers with this example. I would like to do something like this: Array1[0].Array2[0]=5; Array1[0].Array2[1]=10; Array1[0].Array2[3]=11; … If anyone knows, Please let me know. Thanks in advance Steve
-
Hi Everyone, How do I declare an Array inside another Array? For example, I have the following two arrays: ///////////////////////////////////////////////////////// int UserInputSize1, UserInputSize2; int *array1; array1= new int[UserInputSize1]; int *array2; array2=new int[UserInputSize2]; //the size of the arrays will be determined by the user input during runtime. /////////////////////////////////////////////////////// How do I declare them so under array1, it’ll contain the other array? So for example, if the size of array1 is 3 and the size of array2 is 10, then under every element in array1, I’ll be able to store 10 numbers. Then I’ll be able to store a total of 30 numbers with this example. I would like to do something like this: Array1[0].Array2[0]=5; Array1[0].Array2[1]=10; Array1[0].Array2[3]=11; … If anyone knows, Please let me know. Thanks in advance Steve
You first make an array of int* (in effect, an array of arrays) and then allocate one array for each of the int pointers. Thus:
int** bigarray = new int* [UserInputSize1];
for (int i=0; i < UserInputSize1; i++)
bigarray[i] = new int [UserInputSize2];bigarray[0][0] = 5;
bigarray[0][1] = 10;
bigarray[0][2] = 15;Then free the memory in reverse order:
for (int i=0; i < UserInputSize1; i++)
delete [] bigarray[i];delete [] bigarray;
-
Hi Everyone, How do I declare an Array inside another Array? For example, I have the following two arrays: ///////////////////////////////////////////////////////// int UserInputSize1, UserInputSize2; int *array1; array1= new int[UserInputSize1]; int *array2; array2=new int[UserInputSize2]; //the size of the arrays will be determined by the user input during runtime. /////////////////////////////////////////////////////// How do I declare them so under array1, it’ll contain the other array? So for example, if the size of array1 is 3 and the size of array2 is 10, then under every element in array1, I’ll be able to store 10 numbers. Then I’ll be able to store a total of 30 numbers with this example. I would like to do something like this: Array1[0].Array2[0]=5; Array1[0].Array2[1]=10; Array1[0].Array2[3]=11; … If anyone knows, Please let me know. Thanks in advance Steve
The following is a complete program. Is it what you are asking for? If so, then there are many good books about the C and C++ languages that would cover such things much better than the volunteers helping with this forum. #include using namespace std; int main(int argc, char *argv[], char *envp[]) { int Array[3][10], i=0, i1, i2; for (i1=0; i1<3; ++i1) for (i2=0; i2<10; ++i2) Array[i1][i2] = i++; for (i1=0; i1<3; ++i1) { for (i2=0; i2<10; ++i2) cout << Array[i1][i2] << ' '; cout << endl; } return 0; }