0

I can get to the prompt, input an int and get a reasonably expected response such as a simple "thanks for the int". Beyond that, though I have read all the material and watched all the videos from pset0 to pset2, I cannot seen to get what I need to write mario's pyramid. I have tried for over 3 weeks, and I can't even get a hash printed. I can't figure out how to use my int to make hashes and spaces. So, understanding that this is my very first time with anything related to computer programming, can anyone direct me to the lesson material that will help me understand how to write a pyramid from an int? Thanks for any help or advice you may have to offer.

Sincerely,

David Mann

1
  • I would suggest to get help on the facebook group, such questions aren't topic specific and hence not healthy for this site.
    – sinister
    May 4, 2015 at 16:35

1 Answer 1

1

sorry to hear that you're having so much trouble getting started. Let me open by saying the obvious. Go back and reread everything, watch the videos, and read the reading material suggested in the problem sets. BUT, think about it in parts - how to implement a printf() command, loops, etc. and think about how you can use them.

Next (especially at this stage), programming is about breaking problems down into as small a chunk or step as you can. Worry about doing one small task at a time. Again, as you try to implement each technique or tool, go back and review the material for that item, such as a printf, or a while loop.

To get you started, I'll give you an example of how I would think about it. This is only partial code, it doesn't include the main block or all the #includes that a full program would have. It is just code snipets to get you thinking. I don't want to complicate things. So, let's begin.

Let's say you wanted to write a routine to print a certain number of #'s. The first thing is that I want to do is to print out a hash mark. That code is:

printf("#");

Easy enough. It prints a single hash. No linefeed, nothing else. Now, I want to do it, let's say, 5 times. What tool helps me do that? Answer: a FOR loop! So, let's wrap the printf with one.

for(int i = 0; i < 5; i++)
{
    printf("#");
}

This will print out 5 consecutive hashes, ##### Also, since I think I have a fully functional block of code, I'll compile and run it to verify that it works. Once I've fixed all my syntax errors and am confident I haven't introduced any bugs or logic errors, I now want to think about making this more generic, so it will handle any number of hashes. Time to introduce a variable into the mix.

Also, while I'm at it, everything is printed on one line. Time to put in a line feed too.

int hashes = 5;  // create the var and initialize to 5 for testing.

// replace 5 with hashes to make the loop generic for any number of hashes.
for(int i = 0; i < hashes; i++)
{
    printf("#");
}

// add a line feed
printf("\n");

So now, I have a loop that will print any number of hashes followed by a line feed. This is the thinking I follow when writing a program.

Another tip. If you're having a problem with something in the actual program that you're working on, sometimes it helps to create a separate, small program to test only the small piece that you're working on. That way, you leave the work that you have created so far alone, while you can rewrite, break, and fix the small test prog that you're playing with. Once you get it working, you can migrate the code to the main program and get it working there.

BIG TIP: DO NOT TRY TO WRITE THE WHOLE PROGRAM AT ONCE! Write it in small, logical blocks, and then compile the program and test that each section of code is working correctly before starting the next small block. Trying to write too much at once, especially when you're first learning, can introduce too many errors for you to easily find and fix and you'll get frustrated.

Finally, take advantage of the discussion forums, like reddit or CS50x Slack (referenced in course web page under discussion tab.) There are usually people around, especially on Slack, that can answer questions, often in real time.

Does this help? ;-) If not, add your questions in comments. IF it does, please mark this question as answered. Let's keep up on forum housekeeping.

2
  • btw, sorry for the long, long answer, but you seemed to need a good pointer in the right direction to get you rolling.
    – Cliff B
    May 4, 2015 at 4:41
  • Cliff,Your advice I believe will be very helpful; especially on the part about "Do not try to write the whole program at once!". It gets frustrating after I get the first part of the program running (asking for an "int", getting the "int" and actually giving a response such as "Thank you for the int!". But then trying to grow the program into making the hashes, etc. ruins all the prior work. I will try to break it up, try to get the next phase working now. I will let you know how it goes. May 9, 2015 at 2:45

You must log in to answer this question.

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