0

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    //This do while loop checks if user entered number 1-8
    int x;
    do

    {
        x = get_int("Height: ");
    }

    while (x < 1 || x > 9);
    //These for loops represnt the rows and columns

    for (int i = 0; i < x; i++)

    {
        for (int j = 0; j < x; j++)

        {
            // Formula for telling if to print a hash
            if (i + j >= x - 1)

                printf("#");

            else 

                printf(" ");
        }

            printf("  ");

            printf("\n");
    }

    for (int i = 0; i < x; i++)

    {
        for (int j = 0; j < x; j++)

    {
            if (i + j >= x - 1)

                printf("#");
    }

    printf("\n");

    }

}

Cannot seem to get the triangles to print next to each other, the second one prints on the row below the the first triangle.

1

Your went out from you "main" loop for (int i = 0; i < x; i++) and print new line between left and right triangles.

So dialog between your and machine is like:

  1. Give me a colum
  2. Print " " or "#"
  3. Transfer me to a new row
  4. Print "#" or " ".

All your code should print just one row and finish this with printf("\n")

| improve this answer | |
0

*Disclaimer: I got 100% on this problem, I'm not sure if I did it the most efficient way but I can help you out.

Once you use 'i' for loop to get the correct row, I'd print the number of spaces needed, then the number of '#' needed, etc. I didn't nest a third loop like you did. I used a total of 4 loops, one main one and three simple loops nested in the one main loop to print spaces or #s.

To get the formula for how many spaces and # needed, I did mine by hand for a 4x4. For example: when i=0, height =4, I need 3 spaces printed...

Hope that helps!

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .