Help with for loop ?

I am a beginning programer in C and need some help.
I have written the folowing program:
#include
int main()
{
int i = 0;
int c = 10;
int s = 0;
int w = 1;

for (i=1; i<=c; i++)
{
for (s = 1; s<=w; s++)
printf("*");
printf("
");
w = w++;
}
return 0;
}
It produces the desired output :
*
**
***
****
*****
******
*******
********
*********
**********
I now need the program to also produce the following:
**********
*********
********
*******
******
*****
****
***
**
*
I have tried the following and I enter an endless loop.
Can somebody tell me what I am doing wrong? Thanks.
#include <stdio.h>
int main()
{
int i = 0;
int c = 10;
int s = 0;
int w = 1;

for (i=1; i<=c; i++)
{
for (s = 1; s<=w; s++)
printf("*");
printf("
");
w = w++;
}
for (i=1; i<=c; i++)
{
for (s = 10; s<=w; s--)
printf("*");
printf("
");
w = w--;
}


return 0;
}



Comments

  • : I am a beginning programer in C and need some help.
    : I have written the folowing program:
    : #include
    : int main()
    : {
    : int i = 0;
    : int c = 10;
    : int s = 0;
    : int w = 1;
    :
    : for (i=1; i<=c; i++)
    : {
    : for (s = 1; s<=w; s++)
    : printf("*");
    : printf("
    ");
    : w = w++;
    : }
    : return 0;
    : }
    : It produces the desired output :
    : *
    : **
    : ***
    : ****
    : *****
    : ******
    : *******
    : ********
    : *********
    : **********
    : I now need the program to also produce the following:
    : **********
    : *********
    : ********
    : *******
    : ******
    : *****
    : ****
    : ***
    : **
    : *
    : I have tried the following and I enter an endless loop.
    : Can somebody tell me what I am doing wrong? Thanks.
    : #include <stdio.h>
    : int main()
    : {
    : int i = 0;
    : int c = 10;
    : int s = 0;
    : int w = 1;
    :
    : for (i=1; i<=c; i++)
    : {
    : for (s = 1; s<=w; s++)
    : printf("*");
    : printf("
    ");
    : w = w++;
    : }
    : for (i=1; i<=c; i++)
    : {
    : for (s = 10; s<=w; s--)
    : printf("*");
    : printf("
    ");
    : w = w--;
    : }
    :
    :
    : return 0;
    : }
    :
    :
    :
    :
    Your last for loop is the problem
    [CODE]for (s = 10; s<=w; s--)[/CODE]
    After your first loop the value pointed to w is 11, and since the loop starts out with s being less than 11 and then decrements s each loop, it will never end.
    [CODE]
    #include <stdio.h>

    int main()
    {
    int i = 0;
    int c = 10;
    int s = 0;
    int w = 1;

    for (i=1; i<=c; i++)
    {
    for (s = 1; s<=w; s++)
    printf("*");

    printf("
    ");
    w = w++;
    }

    //try a printf here to see what value w is
    printf("w = %d
    ", w);

    for (i=1; i<=c; i++)
    {
    for (s=1; s<w; s++)
    printf("*");

    printf("
    ");
    w = w--;
    }
    return 0;
    } [/code]
  • Try this I'm also a beginner 'C' programmer but this compiles and runs on my system. good luck!

    #include
    int main()
    {
    int i = 0;
    int c = 10;
    int s = 0;
    int w = 1;

    for (i=1; i<=c; i++)
    {
    for (s = 1; s<=w; s++)
    printf("*");
    printf("
    ");
    w = w++;
    }
    for (i=0; i<=c; i++)
    {
    for (s = 1; s<=w; s++)
    printf("*");
    printf("
    ");
    w = w--;
    }
    return 0;
    }

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories