-1

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;
}
the busybee
  • 10,755
  • 3
  • 13
  • 30
Daksh
  • 27
  • 5
  • 1
    Did you try to debug this program already? See [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wovano Oct 05 '22 at 10:53
  • 2
    How did you find out that "the while loop isn't increasing the `k` variable"? When I run the program, `k` is incremented several times. Please [edit] your question and add more details. You wrote "it doesn't seem to work as intended". What input do you use, what actual output do you get and what do you expect instead. – Bodo Oct 05 '22 at 11:00
  • 1
    Please describe in your question how `k` is supposed to count when `i` is `1`, `2` and `3` and compare this what your program actually does. – Bodo Oct 05 '22 at 11:46
  • Additional hint: The variable `k` keeps its value from one iteration of the outer `for` loop to the next iteration. Is this what you want? Use a debugger to check how the value of `k` changes. (Or add `printf("k = %d\n", k);` in the body of the outer `for` loop.) – Bodo Oct 05 '22 at 12:58

2 Answers2

1

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.

Fe2O3
  • 6,077
  • 2
  • 4
  • 20
1

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;
}
Verbatim
  • 245
  • 1
  • 2
  • 8