Entering Data into a 2d Array.

Hello everyone, I'm trying to enter data into a 2d array, but I am not sure if I am doing it right. I have entered data into a single array before, but not two. I tried running break points, but I think I'm confusing myself even more. Here is my current code, does this correctly enter my data into a 2d array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        const int rows = 3;
	const int columns = 4;
	int my2dArray[rows][columns] = { 0, 0 };

for (int x = 0; x <= rows; x++)
	{
		for (int y = 0; y <= columns; y++)
		{
			while (my2dArray[x][y] != -1)
			{
				cin >> my2dArray[x][y];
			}
		}
	}
First of all, it's pretty useless to declare the array as
int my2dArray[rows][columns] = {0, 0 };
Simply type int my2dArray[rows][columns];

Second of all, in the for loops, don't use <= but < because when the array has 5 members, they are 0,1,2,3,4.

Last, I don't know why you use the while loop at line 9. It seems pretty useless to me.
1
2
3
4
5
6
7
8
9
10
for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < columns; y++)
		{
			//while (my2dArray[x][y] != -1)
			//{
				cin >> my2dArray[x][y];
			//}
		}
	}
Well, I wanted it to originally be if I entered x as -1 it would bolt me out of the loop. Also does this mean that my data is being properly entered into the 2d array?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
 
int main() {	
const int rows = 4;
	const int columns = 5;
	int my2dArray[rows][columns];
	

for (int x = 0; x < rows; x++)
	{
		for (int y = 0; y < columns; y++)
		{
			 my2dArray[x][y] = x+y;	
			 cout << my2dArray[x][y];	
		}
		cout<<endl;
	}
}
Topic archived. No new replies allowed.