Thread: No idea where to start with this program that needs loops...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    5

    No idea where to start with this program that needs loops...

    Well, my compute science teacher gave us a lab to do over break, and I decided to just get started. It's very easy, but I simply do not know what kind of loop to use, nor do I know what I use to increment the asterisks. Here's the lab:

    Write a C program that prints the following patterns below one another. There is no user
    input. Use for loops to generate the patterns. All asterisks (*) should be printed by a
    single printf() statement. Use getchar() to pause the screen between patterns. You need
    not define functions for this lab.


    A)
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

    B)....

    and so on

    All I need is a few legs to stand on and then I'll be figuring this out. Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There are for, while and do...while loops. Maybe you should read a little about them to figure out which one might be the best for your task.
    Try to visualize how you want the program to perform. In type of code. "How would I write code that can make this"? That is a typical question a developer asks him/herself.
    Take some time and get used to the available loops, their syntax and what they might be good for and you'll get used to it.

    I'll help you a little. For things such as this, a for loop is usually best.
    This site has a lot of good tutorials you can read if you're wondering about loops and stuff.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you want 1 asterisk, and then 2, and then 3, etc. So perhaps a variable holding this maximum value would be a good idea. You could put this into a loop which loops from 1 to whatever the maximum number of asterisks in a line is.

    1, 2, 3, etc is all very well, but you want to actually print that number of asterisks. How can you do this? With another loop inside the first, which starts from 0 (or 1) and loops until it reaches the value of the outer loop's control variable. So you get 1, \n, 1, 2, \n, 1, 2, 3, \n, . . . .

    What kind of loop you use (for, while, do-while) is entirely up to you. There isn't a huge difference between the different kinds of loops -- everything that can be done with one loop can be done with the other two, although maybe not very prettily. However, for loops lend themselves naturally to counting, so consider using them.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An Idea I have...
    By TechWins in forum Tech Board
    Replies: 1
    Last Post: 07-25-2003, 05:49 AM
  2. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  3. payroll program with loops
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2002, 09:49 PM
  4. Start a program
    By FunkeeMunkee in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 07:18 PM