Home > Back-end >  Why the while loop not changing the variable value here?
Why the while loop not changing the variable value here?

Time:10-06

This C program is to create the pyramid of the user's entered height. But it doesn't seem to work as intended. The while loop isn't increasing the k variable. Please, help it, where am I at fault?

#include <stdio.h>

int main()
{
  int height, i, j, k = 0;

  printf("This program is to make a pyramid\n\n");

  printf("Enter the pyramid height: \n");
  scanf("%d", &height);

  for (i = 1; i <= height; i  ) 
  {
    for (j = 1; j <= height - i; j  )
    {
      printf(" ");
    }

    while (k != 2 * i - 1) 
    {
      printf("*");
        k;
    }
    printf("\n");
  }

  return 0;
}

CodePudding user response:

As @Bodo has diagnosed in the comments, k is not being reset to something valid on each iteration.

Here's a solution that uses fewer variables and is not quite so hard to keep track of where counters are.

#include <stdio.h>

int main() {
    printf(
        "This program is to make a pyramid.\n"
        "Enter the pyramid height (min 3 ): "
    );

    // shorter variable names don't hide operations.
    int h;
    // check that functions work, and that data is reasonable
    if( ( scanf( "%d", &h ) != 1 ) || ( h < 3 ) ) {
        puts( "Bad input" );
        return 1;
    }

    // nested loops
    for( int r = 1; r <= h; r   ) {
        for ( int c = r; c < h r r; c   ) // simple limit value
            if( c <= h ) // clear decision of ' ' or '*'
                putchar( ' ' );
            else
                putchar( '*' ); // putchar faster than printf()
        putchar( '\n' );
    }

    return 0;
}

This version provides the desired output, shifted one space to the right.
An exercise is to figure out what change is needed to not shift the pyramid to the right.

Uses only 'h'eight, 'r'ow and 'c'olumn variables.

CodePudding user response:

Take some time thinking carefully about a problem before starting to write the code. The choice of a good method of solution -- an algorithm -- is important. It's often useful to work through a short example of the algorithm using paper and pencil.

You want to print a number of lines equal to the value of height. Each of those lines has a specific number of spaces followed by a specific number of stars.

In this instance a "for" loop appears to be better suited than a "while" loop. A "while" loop is typically used when the exit point of the loop is not immediately apparent. A "for" loop is useful if the loop's starting point, end point and increment are clearly defined. Since you know exactly how many spaces and stars are required for each line a "for" loop is probably the better choice here.

/* pyramid.c

*/

#include <stdio.h>

#define MAXROWS 36

int main (void)
{
    printf("\n"
           "pyramid program\n"
           "\n");

    int rows;
    do {
        printf("number of rows (1 to %d): ", MAXROWS);   
        scanf("%d", &rows);
    } while ((rows < 1) || (rows > MAXROWS));
    printf("\n");

    // set initial count of spaces and stars
    int spaces = rows - 1;
    int stars = 1;

    // print each line
    for (int i = 1; i <= rows;   i) {

        // print spaces
        for (int j = 1; j <= spaces;   j) {
            printf(" ");
        }

        // print stars
        for (int j = 1; j <= stars;   j) {
            printf("*");
        }

        // print newline
        printf("\n");

        // adjust count of spaces and stars
        --spaces;
        stars  = 2;
    }

    return 0;
}
  • Related