For Loop in C

Intel / Medion
February 14, 2009 at 12:46:41
Specs: Windows XP, 1g ram
This topic is basically the same as http://www.computing.net/answers/pr... .

I've read through the help, and I spotted this code.
for (i = 4; i >= 0; i--)
{
for (j = 4;j >= i; j--)
printf ("*");
printf ("\n");
}

I have an assignment similar to this, but it prompts the user to input a value. I am having a few trouble though, the pattern isn't coming out like how it's suppose to.

main(){
int max, rownum, starnum;
printf("Enter width: ");
scanf("%d", &max);
for (rownum = 1 ; rownum <= max ; rownum++)
{
for (starnum = 1 ; starnum <= rownum ; starnum++)
printf("*");
printf("\n");

}
}

I've looked at the code in the first, and my code. It's a similar output (well suppose to rather), but the for loop value is decreasing whereas mine is increasing. I'd figure it would be the same but it's not giving me the result that I want. Is there a problem in the code I have?


See More: For Loop in C

Report •


#1
February 14, 2009 at 13:09:36
I switched up the numbers and sign in the for loop to make it similar to mine. And it works, but mine doesn't.

for (i = 1; i <= 4; i++)
{
for (j = 1;j <= i; j++)
printf ("*");
printf ("\n");
}



Report •

#2
February 14, 2009 at 13:10:16
>>It's a similar output but the for loop value is decreasing whereas mine is increasing.

Your code:

rownum++
starnum++

The code you found

i--
j--


Can you see why your code is increasing the
value, ( ++ )
while the code you found is decreasing the
value? ( -- )

MIKE


Report •

#3
February 14, 2009 at 13:14:57
Yup, I see why, so the loop eventually stops.

Report •

Related Solutions

#4
February 15, 2009 at 00:02:06
for (rownum = 1 . . .
This is incorrect.

Report •


Ask Question