2017-05-09 4 views
-1

Hey je travaille sur le problème CS50 plus confortable et je ne peux pas comprendre comment imprimer la deuxième mario pyramide sur la même ligne. Dans mon code, il est déjà imprimé, mais ce n'est pas sur la même ligne.Imprimez mario deux demi-pyramide CS50

Peu importe que vous me guidiez ou que vous me montriez comment le faire. J'utilise le CS50 en tant que pratique, je ne transforme rien. Donc, ce ne serait pas de la triche.

#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

variables nom: au lieu d'un commentaire qui indique "// variable gap" .. appeler la variable 'gap'! Ou quelque chose d'encore plus descriptif –

+0

Je me noie dans les commentaires de code !! – paddy

+0

Puisqu'un retour à la ligne termine une ligne, sans option pour revenir à la ligne précédente (bien, n'utilisant pas les bases), vous devrez simplement imprimer à gauche et à droite en même temps. – Evert

Répondre

0

Pourquoi ne pas simplement faire quelque chose comme dans la première boucle:

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("#"); 
} 
appropriée
+0

Je vais essayer ça. Je dois aussi inclure les espaces entre chaque # et ensuite deux espaces entre les pyramides gauche et droite. Merci pour votre contribution. Je l'apprécie –

+0

Merci pour cela. Ça a marché. La seule chose qui ne fonctionne pas est l'espace entre les deux pyramides. Mais le reste fonctionne: D Pouvez-vous m'aider avec les deux espaces vides? –

+0

NVM J'ai figuré l'écart J'ai fait ce qui suit: pour (k = 0; k <1; k ++) { printf (""); } –