0

I am having trouble with problem set 1 and could use some help please!

Specifically, I am having problems generating the blocks in a pyramid. I have watched the walk through but am having trouble translating my ideas to code.

I understand that I need to print x number of spaces based on the user input but cannot figure out how to get x spaces to print. Can someone please help!

Is it ok that I paste my code in here? Edit: here is my code thus far thanks for your help!!

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

int main (void)

{

//asks user for the number of blocks
          printf("Please specify the number of blocks to be printed\n");


//declares a variable t to store the users input (number of blocks)
         int t;


//retrieve block number from user
          do
            {
                 t = GetInt();
            }
          while (t > 23 && t < 0)



//confirms to the user that the input has been accepted

          if (t < 23 && t > 0)
            {
                 printf("Thank you %i blocks with be generated, t"); //error message here "error: more '%' conversions than data arguments" need help with this please 
                 printf("\n");
            }

//pyramid generation (my hashes always come out in a straight line one space apart)
          for (int i = 0; i < t, i++)
            {
              printf("%c, t"); //(How do I represent x number of spaces that corresponds to the t variable that is based on user input??
              printf("#");
              printf("\n");
            }

}

Other issues: any input is accepted even if it is above 23 or below 0 I am not sure what is wrong with my do while loop at the beginning

I know there is a lot here but if someone could spare a few minutes to help me out it would be greatly appreciated and I will be happy to do the same once I know what I am doing!

Thank you so much!

Thomas

2
  • Hi Thomas, it is okay to post the code you have when asking for help. Did you go through the tour of the site? It may help answer some of your questions about posting questions.
    – Aaron S
    Commented Nov 2, 2015 at 21:42
  • Hi Aaron! Thanks for replying! I just took the tour I will post my code! Thanks!
    – Thomas
    Commented Nov 5, 2015 at 20:00

2 Answers 2

1

I noticed that you use "&&" (and) statements in your do while condition. Because you are using an and it will only happen if both conditions,t > 23 && t < 0, are true. You should use || (or) instead because a number cannot be both more than 23 and less than 0.

When you are printing statements and want to include a variable you need to have the , variable out side of the quotation marks the format is: printf("blah %(what ever the letter for the type of data) blah", )

In addition you should probably print the spaces and # using separate loops. Try this out, work on the program and let me know how it went and if you need more help the community is here to help you. If you have not done so already you should look back on some of the earlier psets to practice and watch some of the videos. If this is the answer you are looking for then check that this is the answer.

good luck and have fun

1
  • Thank you Aaron! I will update once I have made the changes!
    – Thomas
    Commented Nov 7, 2015 at 16:53
0

From what you have been taught so far you are gonna need three loops to build the pyramid. First loop is gonna switch lines, second loop is gonna add the spaces, the third loop is gonna add the #. The latter two loops must be nested within the first. since you wanna know how many spaces are to be printed, take a look at the pyramid 1. ## 2. ### 3. #### 4. ##### 5.###### you can see that the every line has same number of characters including the spaces. Eg: In first line there are two # but there are also 4 spaces In second line there are three # but there are 3 spaces Hence the no of spaces= Height of pyramid +1 -#in the row Hope i could help

You must log in to answer this question.

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