This code crashes, unless the indicated line is commented out, then it works beautifully. I cannot understand this because it is the 3rd in a series of 4 repetitions of the same thing. Any help is appreciated. Thanks

``````int x=0;
int y=0;
int lp=0;
for(lp=1;lp<=4;lp++)
{
y=0;
x++;
y++;
A[x][y]=rand() % 9 + 0;
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
y++;
A[x][y]=rand() % 9 + 0;
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
y++;
A[x][y]=rand() % 9 + 0;//comment out this line and it works
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
y++;
A[x][y]=rand() % 9 + 0;
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";``````

Expected output

1-1 A=6
1-2 A=6
1-3 A=5
1-4 A=3
2-1 A=3
2-2 A=7
2-3 A=8
2-4 A=0
3-1 A=4
3-2 A=4
3-3 A=8
3-4 A=2
4-1 A=7
4-2 A=2
4-3 A=6
4-4 A=5

4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by NP-complete

my guess is that you are going out of the array bounds. Whats the size of the
array?

heres the full code

``````int main()
{
srand(time(NULL));
int A[4][4];

int x=0;
int y=0;
int lp=0;
for(lp=1;lp<=4;lp++)
{
y=0;
x++;
y++;
A[x][y]=rand() % 9 + 0;
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
y++;
A[x][y]=rand() % 9 + 0;
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
y++;
A[x][y]=rand() % 9 + 0;//comment out this line and it works
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
y++;
A[x][y]=rand() % 9 + 0;
cout << x << "-" << y<< " A=" <<A[x][y] << " " << "\n";
}

return 0;
}``````

..what truly baffles me, is that if

``cout << "Fine till here";``

is added on line 27, it will print that then fail...... I dont understand what is failing..

Ok, so I figured it out with the help from the post above about array bounds.

i changed

``````int A[4][4];
to
int A[5][5];``````

But i dont understand why it works, could anyone explain?

may i know which compiler you are using?
You surely have array out of bounds in line 24.

Also may i know the headers you are including??

HmmMMMmmm... IMHO, I think this can be done in a simpler way, by using two loops instead of one, that iterates over the entire range of your 2D array. If you have worked with matrices in C++, then you'll know what I'm talking about. Now in C++, array indices start from 0, not 1, so you'll need to fix your loop or your array dimensions. Try this:

``````for(int i = 0; i < 4; ++i) { // To traverse the rows like so: ((0,0), (1,0)... etc
for(int j = 0; j < 4; ++j) { // To traverse a column like so: (0, 0), (0,1)...etc
A[i][j] = rand() % 9;
cout << i+1 << "-" << j+1 << " A=" <<A[i][j] << " " << "\n"; // I guess its cheating, but you get your output :)
}
}``````

(For this code, you wouldn't have to change your array's dimensions)

Hope this helps!

Edited by amrith92: n/a

@NP-complete
From the functions the OP has used, I should think that he has used the following headers:

``````#include <iostream>
#include <cstdlib>
#include <ctime>``````

@tendavola
glad that you figured it out.

Explanation:
An array ALWAYS starts from 0. So if you have something like int A[4] the first element will be at A[0]. Then A[1],A[2] and A[3]. Note we have already reached 4 elements as we wanted (0-3) hence A[4] is meaningless and when you try to assign something to A[4] we say that you are going out of bounds.

Hope that helps.

Edited by NP-complete: n/a

@amrith
Yep he should include all the three.

His main problem i guess was the out of bounds thing.

The above explanation was for tendavola.

@tendavola:

You must also take a deeper look into what amrith92 posted. his code is generally the way you should do these things. And if you follow, his loops iterates from 0 to
"less than" 4. That is, it effectively goes 0,1,2 and 3.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.