Array within an array

I want to have an array within an array .
For example, I want b[10] to store 10 different values for a[2]
I've written the code for this ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <conio.h>

using namespace std;

int main ()
{int x,y,c,d,e,i;
int b[100];
int a[100];
for (y=0;y<3;y++)
{
	for (i=0;i<2;i++)
	{if (i==0)
	{cout <<"X coordinate"<<endl;
	
	}
	else 
	{cout <<"Y coordinate"<<endl;
	
	}
	cin>>a[i];

	}

	b[y]=a;


}
for (x=0;x<3;x++)
{cout<<b[x]<<endl;

}

return 0;
}

But ,it says "invalid conversion from 'int*' to 'int' So,I changed b[100] to *b[100] ,I don't really know what that does though.
Now, I figured,it should run fine, but it prints the last value for "x coordinate" three times.
So:


X coordinate 1
Y coordinate 2
X coordinate 3
Y coordinate 4
X coordinate 5
Y coordinate 6
5
5
5


How am I supposed to make this work?
Thanks
It sounds like you want a two-dimensional array (I think that is what you are asking for).
http://www.cplusplus.com/doc/tutorial/arrays/

A * character defines a pointer. I believe you are probably not looking to dynamically allocate memory for your array since you already seem to know what size you want.
Last edited on
Can you tell me how I would do it in this case?
closed account (E0p9LyTq)
killbox wrote:
Can you tell me how I would do it in this case?


int myArray[100][100];.

If you want to access the 10th "b" element of the 2nd "a" element you could use (remember array elements are zero-based!):
int some_value = myArray[1][9];
Last edited on
OK.
I was looking for something like this :--
B[y]= a[i]
So, for every different value of y, there is an array.
So if there are nine values for y, there are nine different arrays.
With multidimensional arrays, I'd have to make mine dimensions.
closed account (E0p9LyTq)
killbox wrote:
For example, I want b[10] to store 10 different values for a[2]


Just declare a 3 dimensional array, for example: int myArray[100][100][10];. Access the "c[]" 10 elements by:

1
2
3
4
for (int i = 0; i < 10; i++)
   {
      myArray[2][10][i] = i;
   }
int myArray[100][100];

If you want to access the 10th "b" element of the 2nd "a" element you could use (remember array elements are zero-based!):
int some_valuemyArray[1][9];

I'm not sure this makes sense.
 
int some_valuemyArray[1][9];
is declaring another, separate array with one row and nine columns.

To access an element of the first array, it would be something like
 
cout << "second row, tenth column value is " <<  myArray[1][9];
Last edited on
closed account (E0p9LyTq)
Chervil wrote:
I'm not sure this makes sense.

That was some VERY sloppy editing on my part, I cleaned it up.

It was supposed to be int some_value = myArray[1][9]; OOOOOOPS!
Original problem, read 10 coodinates and print them out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int main ()
{
    const int SIZE = 10;
    
    int coord[2][SIZE];
    char label[2] = { 'X', 'Y' };
    
    for (int n=0; n<SIZE; n++)
    {
        for (int i=0; i<2; i++)
        {
            cout << label[i] << " coordinate\n";
    
            cin >> coord[i][n];
        }
    }

    
    for (int n=0; n<SIZE; n++)
    {
        cout << '(' << coord[0][n] << ',' << coord[1][n] << ")\n";
    }

}
Did it. Had to look up a few tutorials on multidimensional arrays .Initially I did this:int i [2][2][2][2][2]; to create 5 dimensions. Looked up some videos,and the final code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <conio.h>

using namespace std;

int main ()
{int a,b,c,d,e,f;
int i[7][2];

for (a=0;a<7;a++)
{

   for (b=0;b<2;b++)
{if (b==0)
{cout <<"X coordinate "<<(a+1)<<endl;
}
else
{cout<<"Y coordinate "<<(a+1)<<endl;
}
cin>>i[a][b];
}
}


     
return 0;
}




Thanks guys!
Topic archived. No new replies allowed.