Christopher
Christopher

Reputation: 137

Programming a reversed pyramid in c

Im working on an optional test review problem for an introduction to C class, and I need to have a program that prints out the following based upon what ever number a user enters:

Enter a number: 5
     5
    44
   333
  2222
 11111
000000
 11111
  2222
   333
    44
     5

So far this is the code that I have written

#include <stdio.h>
int main(void){
   int row,column,space;
   int number;

   printf("Enter a number: ");
   scanf_s("%d",&number);

   for (row = 1; row <= number + 1; row++){
    for (space = number; space >=row; space--){
        printf(" ");
    }
    for(column = 1;column <= row; column++){
        printf("%d",space);
    }
    printf("\n");
   }
   for (row = 1; row <=number;row++){
    for(space = 1;space <= row;space++){
        printf(" ");
    }
    for(column = number;column >=row;column--){
        printf("%d",space);
    }
    printf("\n");
  }
  return 0;
}

This is the output that I get

Enter a number: 5
     0
    11
   222
  3333
 44444
555555
 22222
  3333
   444
    55
     6

I've spent quite a few hours trying to figure out how to print the upper half of the half diamond using the user entered numbers but I can't seem to figure it out. Could anyone point me in the right direction?

Upvotes: 1

Views: 847

Answers (5)

user3629249
user3629249

Reputation: 16540

the following code outputs the first half of the problem.
Notice the checking for errors in the call to scanf()
It compiles cleanly and works correctly
I leave it to you to complete the function for the second half of the output
which should be easy now that you have the first half

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int row;
    int i; // loop counter/index
    int n; // user input

    printf( "\nEnter the wedge width: ");
    if(1 != scanf(" %d", &n) )
    { // then, scanf failed
        perror( "scanf failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, scanf successful

    for( row = 0; row <= n; row++ )
    {
        //  print n-row ' '
        for( i=0; i<(n-row); i++)
        {
            printf(" ");
        }

        //  print row+1  '(n-row)'
        for( i=0; i<(row+1); i++)
        {
            printf( "%1d", (n-row));
        }
        printf( "\n" );
    }

    // reverse the calculations to print the lower half
    return 0;
} // end function: main

Upvotes: 0

pranav prashant
pranav prashant

Reputation: 307

There are numerous ways, I've just modified your code

#include <stdio.h>
int main(void){
    int row,column,space;
    int number;

    printf("Enter a number: ");
    scanf("%d",&number);

    for (row = 1; row <= number + 1; row++){
        for (space = number; space >=row; space--){
            printf(" ");
        }
        for(column = 1;column <= row; column++){
            printf("%d",number -space); //change1
        }
        printf("\n");
    }
    for (row = 1; row <=number;row++){
        for(space = 0;space < row;space++){ //change 2
            printf(" ");
        }
        for(column = number;column >=row;column--){
            printf("%d",space);
        }
        printf("\n");
    }
    return 0;
}

Upvotes: 0

schnaader
schnaader

Reputation: 49719

Your numbers are just off a bit, correct the printf calls and you're done:

First one:

printf("%d", number - space);

Second one:

printf("%d", space - 1);

A slightly better (more readable and a bit more logical) way would be to use other variables instead:

First one:

printf("%d", number + 1 - row);

Second one:

printf("%d", row);

Also note that some basic math can help you to realize the following:

  • Total number of rows: 2 * number + 1
  • Number of spaces: 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5 => abs(number - row) (If starting your rows with 0)
  • Number to print: Same as "Number of spaces"
  • Number count: 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 => 6 - number_of_spaces

This gives a much cleaner, more readable version with only one loop:

#include <stdio.h>
#include <math.h>

int main(void){
   int row,column,space;
   int number;

   printf("Enter a number: ");
   scanf_s("%d",&number);

   for (row = 0; row <= number * 2; row++){
    int number_of_spaces = abs(number - row);
    int number_to_print = number_of_spaces;
    int number_count = 6 - number_of_spaces;
    for (space = 1; space <= number_of_spaces; space++){
        printf(" ");
    }
    for(column = 1;column <= number_count; column++){
        printf("%d", number_to_print);
    }
    printf("\n");
   }
  }
  return 0;
}

Upvotes: 2

Matt
Matt

Reputation: 15081

for (row = 0; row <= number + number; row++) {
    int t = row * (row <= number) + (number + number - row) * (row > number);
    printf("%*c", number - t, ' ');
    printf("%*d\n", t + 1, t);
}

Upvotes: 0

Mario
Mario

Reputation: 36487

To expand on schnaader's answer (which is perfectly fine and complete), you can improve your code even more, letting printf() do the spacing for you rather than doing a loop and several calls to printf():

printf("%*s", width, "");

Here width is replaced with the calculated space you'd like to fill. The precision * is a special placeholder that tells the function to take the actual precision/length from the parameter list. Since the string to print is empty, it will always fill the whole range with space characters.

Upvotes: 2

Related Questions