Reputation: 27
I just started doing the cs50 course but having trouble with the first problem sheet. The aim is to produce right aligned steps (steps you use in Mario just before finishing the level and jump on the flag pole).
The code I have written produces the steps but not right aligned.
But why does my code produce the steps? Shouldn't the condition for the second for loop always be false since i = j when the second for loop is executed?
Sorry if this has been answered before but I'm having trouble articulating my question.
#include <stdio.h>
#include <cs50.h>
int main(){
int height = get_int("How tall are the pyramids: ");
for (int i = 0; i < height; i++){
printf("##");
for (int j = 0; j < i; j++){
printf("#");
}
printf("\n");
}
}
Upvotes: 2
Views: 219
Reputation: 56885
Good attempt; however, you'll need to print a series of spaces before your steps in order to produce right-alignment. This isn't necessarily obvious since the spaces are invisible (and unnecessary for a left-aligned triangle).
The formula for the number of spaces to print is height - row_number - 1
, and the formula for the number of step characters to print is row_number + 1
(or column_number <= row_number
). I recommend experimenting with these numbers and attempting to produce different patterns which will help your understanding. The reason for the extra -1
/+1
offset is to avoid printing a blank line above the triangle.
To answer your question about your second loop termination condition, it'll only be false once you've printed a number of step characters less than the row number, while the first loop is the inverse of this.
#include <stdio.h>
#include <cs50.h>
int main() {
int height = get_int("How tall are the pyramids: ");
for (int i = 0; i < height; i++) {
for (int j = 0; j < height - i - 1; j++) {
printf(" ");
}
for (int j = 0; j <= i; j++) {
printf("#");
}
printf("\n");
}
}
Output (height = 10):
#
##
###
####
#####
######
#######
########
#########
##########
Upvotes: 2
Reputation: 685
Copy and paste this into codechef.com/ide using GCC 6.3. There are a few sample pryamids, left right center both upside down and right side up. To answer your specific quesiton, j is assigned to 0 in the second (nested) for loop, so j is not = to i at the start of the loop.
#include <stdio.h>
int main(){
int height = 10;//get_int("How tall are the pyramids: ");
printf("regular left justified\n");
for (int i = 0; i < height; i++){
printf("##");
for (int j = 0; j < i; j++){
printf("#");
}
printf("\n");
}
printf("Upside down left justified\n");
for (int i = height; i >0 ; i--){
printf("##");
for (int j = 0; j < i; j++){
printf("#");
}
printf("\n");
}
printf("upside down centered pryamid\n");
for (int i = 0; i < height; i++){
//printf("##");
for (int j = 0; j < i; j++){
printf(" ");
}
for( int j=i; j<height; j++){
printf("##");
}
printf("\n");
}
printf("regular centered pryamid\n");
for (int i = 0; i < height; i++){
//printf("##");
for (int j = height; j > i; j--){
printf(" ");
}
for( int j=height; j>=height-i; j--){
printf("##");
}
printf("\n");
}
printf("regular right justified\n");
for (int i = 0; i < height; i++){
//printf("##");
for (int j = height; j > i; j--){
printf(" ");
}
for( int j=height; j>=height-i; j--){
printf("#");
}
printf("\n");
}
printf("upside down right justified\n");
for (int i = 0; i < height; i++){
//printf("##");
for( int j=height; j>=height-i; j--){
printf(" ");
}
for (int j = height; j > i; j--){
printf("#");
}
printf("\n");
}
}
Upvotes: 0