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");
}
}
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");
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加