0

My assignment is to print out a triangular number's digits leading up to the final value in pyramid shape, but I can't for the life of me get it evenly spaced and centered, and I can't just use %6i or something because it has to work for any number we put in. I've tried justification, using a separate variable to calculate spacing based on the user defined number, but none of it worked.

Our result should look like this image, using 15 as an example.

Our result should look like this image

It's due by midnight, and we've been at it for days. My code is sort of long:

//Include the usual libraries
#include <stdio.h>

//Declare the main function
int main (void)

{

  //Declare variables
  int  space, n, triNum, ast, num, triDisplay = 0, number = 1;

  //Welcome the user to your program
  printf("Welcome to Phase 1!\nThis program will display a triangular number of your choosing in 'graphical' format.\n");

  //Prompt the user for the triangular number
  printf("Enter the triangular number: ");
   scanf("%i", &triNum);

   //This loop keeps track of how many times the nested loops have run.
   n = 1;
   while(n<=triNum)
   {

    //This loop prints the spaces before the asterisks, and decreases the amount printed after each line.
    space=n;
    while(space>0, space<triNum)
    {
     printf(" ");
     space++;
    }//End of second loop

    //This loop prints the asterisks and increases the amount printed by one for each line.
    ast=n;
    while(ast>0)
     {
      printf("* ");
      ast--;
     }//End of third loop

   printf("\n") ;
   n++;
   }//End of first loop

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------

   //This loop is identical to the first loop, but is used to display a different set of loops
   n = 1;
   while(n<=triNum)
   {


    space=n;
    while(space>0, space<triNum)
     {
      printf(" ");
      space++;
     } 

    //This loop prints the number associated with each asterisk in triangle format
     num = 1; 
    while(num>0, num<=n)
     {
     printf("%i ",number);
     number++;
     num++;
     }
    n++;
    printf("\n");
   }
    //This loop calculates the actual value of the triangular number
    n=1;
   while(n<=triNum)
   {    
    triDisplay += n;
    n++;
   }

  printf("\nThe triangular number of %i is %i.\n", triNum, triDisplay);
  //Thank the user for using your program
  printf("Thank you for using this program! Have a nice day.\n");

//End function
return 0;
}

This is what ours currently looks like.

This is what ours currently looks like

4

2 回答 2

0

Looks like you are pretty close, the reason you are off seems to be that you always have one space between your numbers. The first image has multiple spaces between numbers, figure out how to compute the number of spaces based on the number of digits in your number and you should have it.

Does the problem specification say how many spaces should be included, or exactly what the final alignment of the pyramid should be?

This comes VERY close to what you need, the only difference is there is an extra 2 spaces at the beginning of each line, because the 3 digit number that starts the bottom row still uses 5 spaces. So you probably need to figure out how to use exactly x digits for the first number in each row, where x equals the number of digits for the first number in the bottom row, 3 in this example.

while (n <= triNum)
{


    space = n * 3;
    while (space>0, space<(triNum * 3))
    {
        printf(" ");
        space++;
    }

    //This loop prints the number associated with each asterisk in triangle format
    num = 1;
    while (num>0, num <= n)
    {
        printf("%5i ", number);
        number++;
        num++;
    }
    n++;
    printf("\n");
}

output:

                                              1
                                           2     3
                                        4     5     6
                                     7     8     9    10
                                 11    12    13    14    15
                              16    17    18    19    20    21
                           22    23    24    25    26    27    28
                        29    30    31    32    33    34    35    36
                     37    38    39    40    41    42    43    44    45
                  46    47    48    49    50    51    52    53    54    55
               56    57    58    59    60    61    62    63    64    65    66
            67    68    69    70    71    72    73    74    75    76    77    78
         79    80    81    82    83    84    85    86    87    88    89    90    91
      92    93    94    95    96    97    98    99   100   101   102   103   104   105
  106   107   108   109   110   111   112   113   114   115   116   117   118   119   120

Let me explain what I changed. I suggest you follow along when you update your code, so you understand and can fix the last little bit and make sure it works for other numbers.

The first thing I noticed was that you were not getting enough spaces in the beginning. Given each the numbers go up to 3 digits, I figured you should be adding 3 spaces instead of 1, so I changed

while (space>0, space<triNum)

to

while (space>0, space<(triNum * 3))

This looked a little better, but now there were too many spaces in the bottom rows, so I figured we should also be subtracting out 3 instead of 1 when we adjust the number of spaces, so I changed

space = n;

to

space = n * 3;

This certainly looked like the correct number of spaces in front, but things still didn't seem to line up. So looking at your correct pyramid, I saw that each number seemed to be taking up 6 spaces, so I changed

printf("%i ", number);

to

printf("%5i ", number);

And that is where it is now. It worked for a number of different examples that I put in, by no means exhaustive though. I think it will be ok as long as you don't end up printing numbers with greater than 5 digits. If the number of spaces really doesn't matter (i.e. have to be minimal and still have balanced rows), then you can likely increase the width in printf() to whatever can hold the max number you must accommodate.

于 2018-03-13T04:19:29.097 回答
0

Read the printf() manual page again. (And again, and again, and again. When you've read twice a day for a week, you can read it just once a week for the next month, and then once a month for the next year, and thereafter once a year for life — and you'll still probably spot something new after 30 years, if only because the specification has changed over that time. The same is necessary for scanf() too, except it's harder; you probably need to read it twice a week for a month.)

You can tell printf() how wide to print using an int variable (or value) to specify the width.

For example:

for (int i = 0; i < 10; i++)
    printf("[%*d%*s]\n", i + 3, 121, i, "");

will generate:

[121]
[ 121 ]
[  121  ]
[   121   ]
[    121    ]
[     121     ]
[      121      ]
[       121       ]
[        121        ]
[         121         ]

You can use things like snprintf() to find out how wide the number is and adjust your calculations appropriately.

于 2018-03-13T04:43:21.523 回答