By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Strange loop

P: n/a
Hello!

I wrote short program:

#include <stdio.h>

main(){
int Uporzadkowana[255][255];
int Tablica[64*1024];
Tablica[0]=1;
for(int i = 0; i < 256; i++){
for(int j = 0; j < 256; j++){
printf("i=%i, j=%i...",i,j);
}
}
printf("%i",Tablica[0]);
}

And "i" and "j" both grow up, but when I've add this line:

Uporzadkowana[j][i]=Tablica[0];

into loop, under printf, "i" is always "0", why??? I'm using CBuilder5.
---
Chris


Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Krzysztof Kolago wrote:

I wrote short program:

#include <stdio.h>

main(){
int Uporzadkowana[255][255];
int Tablica[64*1024];
Tablica[0]=1;
for(int i = 0; i < 256; i++){
for(int j = 0; j < 256; j++){
printf("i=%i, j=%i...",i,j);
}
}
printf("%i",Tablica[0]);
}

And "i" and "j" both grow up, but when I've add this line:

Uporzadkowana[j][i]=Tablica[0];

into loop, under printf, "i" is always "0", why???


Hello Chris,

I rewrote your code slightly so that it complies with the C++ standard:

#include <cstdio>
using std::printf;

int main() {
int Uporzadkowana[255][255];
int Tablica[64*1024];
Tablica[0]=1;
for(int i = 0; i < 255; i++){
for(int j = 0; j < 255; j++){
printf("i=%i, j=%i...",i,j);
Uporzadkowana[j][i]=Tablica[0];
}
}
printf("%i",Tablica[0]);
}

Also note that I changed the loop conditions to "i < 255" and "j < 255."
Each dimension of the two-dimensional array is 255 elements, indexed from 0
to 254. 255 is not a valid index.

What happens when you compile and run my version of your program? When I
run it, I get the output you would expect. i is not always 0; it changes
incrementally from 0 to 254.

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Jul 19 '05 #2

P: n/a
Krzysztof Kolago wrote:
Hello!

I wrote short program:

#include <stdio.h>

main(){
int Uporzadkowana[255][255];
int Tablica[64*1024];
Tablica[0]=1;
for(int i = 0; i < 256; i++){ This iterates 256 times, yet your array is only 255 elements.

I'm not sure this is your problem but it's certainly an issue.
for(int j = 0; j < 256; j++){
printf("i=%i, j=%i...",i,j);
}
}
printf("%i",Tablica[0]);
}

And "i" and "j" both grow up, but when I've add this line:

Uporzadkowana[j][i]=Tablica[0];

into loop, under printf, "i" is always "0", why??? I'm using CBuilder5.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.