Howdy, Stranger!

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

Categories

Help with for loop ?

HerdHerd Member Posts: 1
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

  • DB1DB1 Member Posts: 1,142
    : 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]
  • kangelkangel Member Posts: 20
    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;
    }

  • Andre YoungAndre Young USAMember Posts: 0

    ____ || http://forcoder.org || free ebooks and video tutorials about < Visual Basic, C#, Scratch, Ruby, Objective-C, C++, JavaScript, Delphi, MATLAB, R, Swift, PHP, C, Python, Go, Visual Basic .NET, PL/SQL, Java, Perl, Assembly Rust, Transact-SQL, COBOL, FoxPro, Scala, ABAP, VBScript, Awk, Prolog, Clojure, Fortran, LabVIEW, F#, Apex, D, ML, Julia, Crystal, Scheme, Dart, Hack, Lisp, Bash, Kotlin, Lua, SAS, Erlang, Ada, Alice, Logo /> __

Sign In or Register to comment.