Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Strange loop

Reply
Thread Tools

Strange loop

 
 
Krzysztof Kolago
Guest
Posts: n/a
 
      11-16-2003
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




 
Reply With Quote
 
 
 
 
Russell Hanneken
Guest
Posts: n/a
 
      11-16-2003
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:rintf;

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

Remove the 'g' from my address to send me mail.


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      11-16-2003
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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Getting a loop to activate a loop above it Byte Python 4 03-24-2006 03:04 AM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments
 



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 48 49 50 51 52 53 54 55 56 57