2017-05-09 2 views
-1

Hey Ich arbeite an der CS50 komfortabler Problem und ich kann nicht herausfinden, wie die zweite Mario-Pyramide auf der gleichen Linie zu drucken. In meinem Code druckt es bereits, aber es ist nicht in der gleichen Zeile.Ausdrucken mario zwei halbe Pyramide CS50

Es spielt keine Rolle, ob Sie mich führen oder mir zeigen, wie es geht. Ich benutze CS50 als Übung, ich mache nichts. Also wäre das nicht Betrug.

#include <stdio.h> 
#include <ctype.h> 

int main(void) 
{ 
    int height = 0; 

    // left pyramid variables 
    int i = 0; 
    int j = 0; 
    int k = 0; 

    // variable for gap 
    int g = 0; 

    // right pyramid variables 
    int l = 0; 
    int m = 0; 
    int n = 0; 


    //do - while loop -- works 

    do 
    { 
     printf("Height: "); 
     scanf("%d", &height); 
    } 

    while (height < 0 || height > 23); 

    // Print pyramids 

     // print spaces for left pyramid (less spaces needed with time) ✓ 
     // print hashes for left pyramid ✓ 
     // print gap (2) 
     // print hashes for right pyramid 
     // print new line - for next row 

    // Left Pyramid 

    // Rows -- determines the height 
    for (i = 0; i < height; i++) 
    { 
     // Cols -- in this one we are doing the spaces 

     // the -i makes it left aligned -- to make it right aligned remove the "-1" 
     for (j = 0; j < height-i; j++) 
     { 
      // Printing Spaces 
      printf(" "); 
     } 
     // "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one 
     // whenever the height increases, so that's why we add + 1 to it 
     // if I don't add 1 to it what it does is that prints a new line, and then it prints 
     // 4 things instead of 5 for example. 
     for (k = 0; k < i + 1; k++) 
     { 
      printf("#"); 
     } 

     // Print new line 
     printf("\n"); 
    } 

    // Gap -- fix gap, the rest works how it should -- I think I need to make everything 
    // inside one loop 

    // for (g = 0; g < height; g++) 
    // { 
    //  printf(" "); 
    // } 

    // Right Pyramid 

     // Rows -- determines the height 
    for (l = 0; l < height; l++) 
    { 

     // Cols -- in this one we are doing the spaces 

     // right aligned 
     for (m = 0; m < height; m++) 
     { 
      // Printing Spaces 
      printf(" "); 
     } 
     // "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one 
     // whenever the height increases, so that's why we add + 1 to it 
     // if I don't add 1 to it what it does is that prints a new line, and then it prints 
     // 4 things instead of 5 for example. 
     for (n = 0; n < l + 1; n++) 
     { 
      printf("#"); 
     } 

     // Print new line 
     printf("\n"); 
    } 


    return 0; 
} 
+0

Namen Variablen entsprechend: eher als ein Kommentar, der „// Variable für gap“ .. rufen Sie die Variable ‚Lücke‘, heißt es! Oder etwas noch aussagekräftigeres –

+0

Ich ertrinke in Code-Kommentare !! – paddy

+0

Da ein Zeilenumbruch eine Zeile beendet und es nicht möglich ist, zur vorherigen Zeile zurückzukehren (ohne die Grundlagen zu verwenden), müssen Sie einfach gleichzeitig links und rechts drucken. – Evert

Antwort

0

Warum nicht nur so etwas wie in der ersten Schleife:

for (k = 0; k < i + 1; k++) 
{ 
    printf("#"); 
} 

/* this is new */ 
/* Draw the gap */ 
for (k = 0; k < gap; k++) { 
    printf(" "); 
} 
/* Draw the left part*/ 
for (k = 0; k < i + 1; k++) 
{ 
    printf("#"); 
} 
+0

Ich werde das versuchen. Ich muss auch die Leerzeichen zwischen jeder und zwei Lücken zwischen der linken und rechten Pyramide einschließen. Danke für deinen Beitrag. Ich schätze es –

+0

Danke dafür. Es funktionierte. Das einzige, was nicht funktioniert, ist die zweifache Lücke zwischen den beiden Pyramiden. Aber der Rest funktioniert: D Kannst du mir mit den beiden Lücken helfen? –

+0

NVM Ich dachte, die Lücke aus Ich tat folgendes: für (k = 0; k <1; k ++) { printf (""); } –