This is my first time posting in here, hopefully I am doing it right.
Basically I need help trying to figure out some code that I wrote for class using C. The purpose o
Here's your code, just reformatted and stripped of comments:
for(int tall = 0;tall<user_i;tall++)
{
for(int space=0; space<=user_i-tall; space++){
printf(" ");
}
for(int hash=0; hash<=tall; hash++){
printf("#");
}
printf("\n");
}
And the same code, with some explanation interjected:
// This is a simple loop, it will loop user_i times.
// tall will be [0,1,...,(user_i - 1)] inclusive.
// If we peek at the end of the loop, we see that we're printing a newline character.
// In fact, each iteration of this loop will be a single line.
for(int tall=0; tall<user_i; tall++)
{
// "For each line, we do the following:"
//
// This will loop (user_i - tall + 1) times, each time printing a single space.
// As we've seen, tall starts at 0 and increases by 1 per line.
// On the first line, tall = 0 and this will loop (user_i + 1) times, printing that many spaces
// On the last line, tall = (user_i - 1) and this will loop 0 times, not printing any spaces
for(int space=0; space<=user_i-tall; space++){
printf(" ");
}
// This will loop (tall + 1) times, each printing a single hash
// On the first line, tall = 0 and this will loop 1 time, printing 1 hash
// On the last line, tall = (user_i - 1) and this will loop user_i times, printing that many hashes
for(int hash=0; hash<=tall; hash++){
printf("#");
}
// Finally, we print a newline
printf("\n");
}
First off, let's get the body of the loops out of the way. The first one just prints spaces, and the second one prints hash marks.
We want to print a line like this, where _ is a space:
______######
So the magic question is, how many spaces and #s do we need to print?
At each line, we want to print 1 more # than the line before, and 1 fewer spaces than the line before. This is the purpose "tall" serves in the outer loop. You can think of this as "the number of hash marks that should be printed on this line."
All of the remaining characters to print on the line should be spaces. As a result, we can take the total line length (which is the number the user entered), and subtract the number of hash marks on this line, and that's the number of spaces we need. This is the condition in the first for loop:
for ( int space = 0; space <= user_i - tall; space++ )
// ~~~~~~~~~~~~~
// Number of spaces == number_user_entered - current_line
Then we need to print the number of hash marks, which is always equal to the current line:
for ( int hash = 0; hash <= tall; hash++ )
// ~~~~
// Remember, "tall" is the current line
This whole thing is in a for loop that just repeats once per line.
Renaming some of the variables and introducing some new names can make this whole thing much easier to understand:
#include <stdio.h>
int main ( void )
{
int userProvidedNumber;
printf ( "Hello there and welcome to the pyramid creator program\n" );
printf ( "Please enter a non negative INTEGER from 0 to 23\n" );
scanf ( "%d", &userProvidedNumber );
while ( userProvidedNumber < 0 || userProvidedNumber > 23 )
{
scanf ( "%d", &userProvidedNumber );
}
for ( int currentLine = 0; currentLine < userProvidedNumber; currentLine++ )
{
int numberOfSpacesToPrint = userProvidedNumber - currentLine;
int numberOfHashesToPrint = currentLine;
for ( int space = 0; space <= numberOfSpacesToPrint; space++ )
{
printf ( " " );
}
for ( int hash = 0; hash <= numberOfHashesToPrint; hash++ )
{
printf ( "#" );
}
// We need to specify the printf("\n"); statement here
printf ( "\n" );
}
return 0;
}
You should use indentatation to make your code more readable and therefore easier to understand.
What your code does is print user_i
lines that consist of user_i-tall+1
spaces and then tall+1
hashes. Because the iteration index tall
increases with every pass this means that one more hash is printed. They are right aligned because a space is left out as well. Subsequently you have 'growing' rows of hashes that form a pyramid.
What you are doing is equivalent to this pseudo code:
for every i between 0 and user_i do:
print " " (user_i-i+1) times
print "#" (i+1) times
#include <stdio.h>
// Please note spacing of
// - functions braces
// - for loops braces
// - equations
// - indentation
int main(void)
{
// Char holds all the values we want
// Also, declaire all your variables at the top
unsigned char user_i;
unsigned char tall, space, hash;
// One call to printf is more efficient
printf("Hello there and welcome to the pyramid creator program\n"
"Please enter a non negative INTEGER from 0 to 23\n");
// This is suited for a do-while. Exercise to the reader for adding in a
// print when user input is invalid.
do scanf("%d", &user_i);
while (user_i < 0 || user_i > 23);
// For each level of the pyramid (starting from the top)...
// Goes from 0 to user_i - 1
for (tall = 0; tall < user_i; tall++) {
// We are going to make each line user_i + 2 characters wide
// At tall = 0, this will be user_i + 1 characters worth of spaces
// At tall = 1, this will be user_i + 1 - 1 characters worth of spaces
// ...
// At tall = user_i - 1, this will be user_i + 1 - (user_i - 1) characters worth of spaces
for (space = 0; space <= user_i - tall; space++)
printf(" "); // no '\n', so characters print right next to one another
// because of using '<=' inequality
// \_
// At tall = 0, this will be 0 + 1 characters worth of hashes
// At tall = 1, this will be 1 + 1 characters worth of hashes
// ...
// At tall = user_i - 1, this will be user_i - 1 + 1 characters worth of spaces
for (hash = 0; hash <= tall; hash++)
printf("#");
// Level complete. Add a newline to start the next level
printf("\n");
}
return 0;
}