Thread: I need help with this...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    I need help with this...

    I have this lab to do in school, yet I just don't understand it at all. Plus I'm on break so my teacher can't help me.

    Lab 16
    Stars
    Objective
    To explore nested for loops and the use of the index variable.
    Assignment
    Read the following code carefully. In the box, write out what you predict the output will be.
    #include <iostream>

    using namespace std;

    int main()
    {
    int row, col;
    cout << endl;
    for(row = 0; row < 5; row++)
    {
    for(col = row; col >= 0; col --)
    cout << "*";
    cout << endl;
    }
    return 0;
    }

    Type in and run the code to see if you were right.
    Now, write a C++ program that will generate the
    following patterns. Note that you must use for
    loops and may not use any if statements! Each of
    the designs should be implemented in a separate
    function or functions.
    *****
    ****
    ***
    **
    *

    *
    **
    ***
    ****
    *****

    *
    ***
    *****
    *******
    *********

    For the following pattern you may use if
    statements, but you may not simply output the text
    of the pattern using cout.
    *******
    ** ** (Note: This pattern is
    * * * * a square.)
    * * *
    * * * *
    ** **
    *******

    Modify each function to accept a single integer parameter N and draw your patterns to a height of N rows.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Modify each function to accept a single integer parameter N and draw your patterns to a height of N rows.

    To find that, I'd start by playing with the loops max's, and see what changes in the output. Once you find which loop controls the height, ask the user for an input, and put that var as the loops max. Although I think the var names kind of give away which loop controls the height.

  3. #3
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    valaris kinda hit it on the nose... if you know how a loop works this lab is cake. I remember doing one similar a long time ago.

    Code:
        int max_height = 5;
        for(int height = 1; height <= max_height; height++)
        {
            for(int len = 1; len <= height; len++)
            {
                cout << "*";
            }
            cout << endl;
        }
    This is the same code that you have there, but with different names. Like i said valaris said exactly what you need. i believe that is correct in c++. I would try and mess with the numbers until you understand loops. All the code in the first 'for' block will be executed until the second argument is true. So second 'for' loop will be executed so many times and it has to go through 'len' amount of times.
    Last edited by mramazing; 12-24-2008 at 04:10 PM.
    -- Will you show me how to c++?

  4. #4
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Or you can use Sebastiani's signature to understand these kind of things
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed