What's wrong.... [modified]
-
The below code sorts a matrix diagonally. Does it, but at the end it's throwing an "illegal" may be a bad access error. Why ?
int main(int argc, char* argv[]) { int A[5][5];int i;int j; int temp; int c,flag; }
input : 1-1-1 2-2-2 3-3-3 output: 1-2-3 1-2-3 1-2-3 -- modified at 6:02 Friday 12th January, 2007*
-
The below code sorts a matrix diagonally. Does it, but at the end it's throwing an "illegal" may be a bad access error. Why ?
int main(int argc, char* argv[]) { int A[5][5];int i;int j; int temp; int c,flag; }
input : 1-1-1 2-2-2 3-3-3 output: 1-2-3 1-2-3 1-2-3 -- modified at 6:02 Friday 12th January, 2007*
You are accessing declared array with invalid index. int A[5][5]; should be accessed only from A[0][0] to A[4][4] where as you are accessing it through A[1][1] to A[5][5]
Astricks wrote:
for (i = 1;i<=5;i++) {
You should modify this to,
for (int i = 0 ; i <5 ; i++)
at all places you have done mistake.
Prasad Notifier using ATL | Operator new[],delete[][^]
-
The below code sorts a matrix diagonally. Does it, but at the end it's throwing an "illegal" may be a bad access error. Why ?
int main(int argc, char* argv[]) { int A[5][5];int i;int j; int temp; int c,flag; }
input : 1-1-1 2-2-2 3-3-3 output: 1-2-3 1-2-3 1-2-3 -- modified at 6:02 Friday 12th January, 2007*
C/C++ arrays are 0-based, i.e. if you declare:
int a[5];
then you have the following items:
a[0], a[1], a[2], a[3], a[4];
on the other hand,
a[5]
, is out-of-bounds. so the correct iteration will befor (i = 0;i<5;i++)
a[i]= (whatever);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
The below code sorts a matrix diagonally. Does it, but at the end it's throwing an "illegal" may be a bad access error. Why ?
int main(int argc, char* argv[]) { int A[5][5];int i;int j; int temp; int c,flag; }
input : 1-1-1 2-2-2 3-3-3 output: 1-2-3 1-2-3 1-2-3 -- modified at 6:02 Friday 12th January, 2007*
-
int A[5][5];int A[6][6];
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.
-
The below code sorts a matrix diagonally. Does it, but at the end it's throwing an "illegal" may be a bad access error. Why ?
int main(int argc, char* argv[]) { int A[5][5];int i;int j; int temp; int c,flag; }
input : 1-1-1 2-2-2 3-3-3 output: 1-2-3 1-2-3 1-2-3 -- modified at 6:02 Friday 12th January, 2007*
In these lines: if (j-1 > 0 && i+1 <=5 ) { if (A[i][j] < A[i+1][j-]) "i" can be as large as 4. Which means elements A[5][j] are being accessed - but the declaration was int A[5][5] - i.e. the maximum element is A[4][j] So array out of bounds ..
cheers, Neil
-
you're wasting 11 integers...:):-D:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
int A[5][5];int A[6][6];
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.
This will surely work, but the poor guy will never come to know what is wrong with his approach! I think thats the reason for you being voted down. (I gave a 5 to bring u up :-D because you put your time and efforts and shouldn't get voted down)
Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா
-
This will surely work, but the poor guy will never come to know what is wrong with his approach! I think thats the reason for you being voted down. (I gave a 5 to bring u up :-D because you put your time and efforts and shouldn't get voted down)
Nobody can give you wiser advice than yourself. - Cicero ப்ரம்மா
It was a quick-fix :rolleyes:, But I said that to indicate the error is about array-out-of-bounds. The 1 voted would have missed my point. :sigh:... anyway thanks :)
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.
-
It was a quick-fix :rolleyes:, But I said that to indicate the error is about array-out-of-bounds. The 1 voted would have missed my point. :sigh:... anyway thanks :)
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.
I decided to add my 1 to the vote, because 0 was not a choice. You received a 1 vote because you did not provide an answer to the question. If the person asking the question understood your [joke] answer then they would not have needed to ask it in the first place. :sigh: Sorry, but the idea is to help and not make fun of the person asking the question.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
I decided to add my 1 to the vote, because 0 was not a choice. You received a 1 vote because you did not provide an answer to the question. If the person asking the question understood your [joke] answer then they would not have needed to ask it in the first place. :sigh: Sorry, but the idea is to help and not make fun of the person asking the question.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
-
hmm :(
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.
It seems that on this forum we must only answer to questions;)
WhiteSky
-
C/C++ arrays are 0-based, i.e. if you declare:
int a[5];
then you have the following items:
a[0], a[1], a[2], a[3], a[4];
on the other hand,
a[5]
, is out-of-bounds. so the correct iteration will befor (i = 0;i<5;i++)
a[i]= (whatever);:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
You are accessing declared array with invalid index. int A[5][5]; should be accessed only from A[0][0] to A[4][4] where as you are accessing it through A[1][1] to A[5][5]
Astricks wrote:
for (i = 1;i<=5;i++) {
You should modify this to,
for (int i = 0 ; i <5 ; i++)
at all places you have done mistake.
Prasad Notifier using ATL | Operator new[],delete[][^]
-
int A[5][5];int A[6][6];
Code-Frog:So if this is Pumpkinhead. Time for him to run and hide. It's an interesting thought really.