Need Help figuring out the output to a C program

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
OK heres the Code

#include<stdio.h>
int main()
{
int i, j;

for ( i = 0; i <= 3; i++ )
{
for( j = 0; j <= 4; j++ )
{
if (( i + 1 == j ) || ( j + 1 == 4 ))
{
printf("+");
}
else
{
printf("o");
}
}
printf("\n");
}


return 0;

}

When i worked this out on paper here is my version of the output:

0+00+\n
00++0\n
000+0\n
0+00+\n

Am i correct?

When i compiled it, the program gave me:

0+0+0\n
00++0\n
000+0\n
000++\n


I don't know what i did wrong, my friend worked this out on paper and got the same result that i got.

Can anyone confirm the output?
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
i'm too lazy to actually read the code, but have you tried going step by step through the debugger? that sounds like it would solve your problem.
 

zephyrprime

Diamond Member
Feb 18, 2001
7,512
2
81
I worked it out, the compiler is right (big surprise).

Do it like this:
=j=_0_1_2_3_4

i=0_0_+_0_+_0
i=1_0_0_+_+_0
i=2_0_0_0_+_0
i=3_0_0_0_+_+

sorry if this is obtuse. I think you may just be losing track of the (i + 1 == j ) || ( j + 1 == 4) statements.

the ( j + 1 == 4) statement creates a vertical row at j=3 because 3+1 == 4
 

imported_vr6

Platinum Member
Jul 6, 2001
2,740
0
0
hymm i guess i am losing track huh??

well thanks guys, i'll just memorize the results for now and figure this out later!

Thanks
kwan1