2d Array Reverse Crash

Write your question here.
Hello my program takes an input turns it into a 2d array then reverses it. When I try to output my array it outputs 3 negative numbers then crashes.
Error:
A buffer overrun has occurred in 2d Array Reverse.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.
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
36
37
38
39
40
41
42
43
44
45
46
47
// 2d Array Reverse.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int board[2][3];
	int width=3;
	int height=4;
	cout<< "Enter your First Row First Number"<<endl;
	cin>> board[0][1];
	cout<< "Enter your First Row Second Number"<<endl;
	cin>> board[0][2];
	cout<< "Enter your First Row Third Number"<<endl;
	cin>> board[0][3];
	cout<< "Enter your Second Row First Number"<<endl;
	cin>> board[1][1];
	cout<< "Enter your Second Row Second Number"<<endl;
	cin>> board[1][2];
 	cout<< "Enter your Second Row Third Number"<<endl;
	cin>> board[1][3];
	cout<< "Enter your Third Row First Number"<<endl;
	cin>> board[2][1];
	cout<< "Enter your Third Row Second Number"<<endl;
	cin>> board[2][2];
	cout<< "Enter your Third Row Third Number"<<endl;
	cin>> board[2][3];
	cout<< "Enter your Fourth Row 1st Number"<<endl;
	cin>> board[3][1];
	cout<< "Enter your Fourth Row 3rd Number"<<endl;
	cin>> board[3][2];
	cout<< "Enter your Fourth Row 4th Number"<<endl;
	cin>> board[3][3];
	for(int i = 0; i<width; i++)
	{
		for(int j=0; j<height; j++)
		{
			cout<< board[i][j] << " "<<endl;
		}
	}
	return 0;
}

Last edited on
You have your array dimensions mixed up. You initialize an array to by 3x4, but you try to enter numbers into a 4x3 array. The for loop is correct for a 3x4 array.
So how do I do it correctly?
The left [] should only go up to 2, and the right [] should go up to 3.
it still gives me
A buffer overrun has occurred in 2d Array Reverse.exe which has corrupted the program's internal state. Press Break to debug the program or Continue to terminate the program.
(I updated the code)
Sorry, I'm not getting any errors. Even if I enter bad data the program still completes.
int board[2][3];

board is defined to only have two rows and three columns.

[0,0][0,1][0,2]
[1,0][1,1][1,2]

The code is trying to access elements that don't exist.
I changed it to [3][4] and same problem.
OK, 3 rows by 4 columns is this.

[0,0][0,1][0,2][0,3]
[1,0][1,1][1,2][1,3]
[2,0][2,1][2,2][2,3]


This code still tries to access elements that don't exist.
1
2
3
4
5
6
cout<< "Enter your Fourth Row 1st Number"<<endl;
cin>> board[3][1];
cout<< "Enter your Fourth Row 3rd Number"<<endl;
cin>> board[3][2];
cout<< "Enter your Fourth Row 4th Number"<<endl;
cin>> board[3][3];



The first row, first number is [0,0]
1
2
cout<< "Enter your First Row First Number"<<endl;
cin>> board[0][1];
Oh, Thanks.
Topic archived. No new replies allowed.