Strange loop

K

Krzysztof Kolago

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]=Tablica[0];

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

Russell Hanneken

Krzysztof said:
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]=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]=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.
 
G

Gianni Mariani

Krzysztof said:
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]=Tablica[0];

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top