Rows: display a pattern that increments in each row

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolla MacPherson

    Rows: display a pattern that increments in each row

    Hi
    I'm a newbie and want to display a pattern that will increment with each
    line of display.
    I thought i might be able to count the rows and use that info to increment
    my display.
    I've got my starting point using setw and don't want to use cout for every
    line of display. What should i be looking for to use.
    Thanks


  • Karl Heinz Buchegger

    #2
    Re: Rows: display a pattern that increments in each row



    Nicolla MacPherson wrote:[color=blue]
    >
    > Hi
    > I'm a newbie and want to display a pattern that will increment with each
    > line of display.[/color]

    what pattern?
    [color=blue]
    > I thought i might be able to count the rows and use that info to increment
    > my display.[/color]

    You might. It depends on the pattern.
    [color=blue]
    > I've got my starting point using setw and don't want to use cout for every
    > line of display. What should i be looking for to use.[/color]

    Using cout for every single line is the easiest thing you could do, *if*
    your pattern is that way. It all depends on what the pattern looks like.
    If your pattern is such that you can derive some formula from the line count,
    then things are going the easy way.

    Example: You have to produce this pattern:

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

    So what do you recognize? Every line consists of spaces followed
    by '*' characters. How many are in each line? Lets make a table:

    line # | # of spaces # of asteriks
    ---------+-----------------------------
    0 | 5 1
    1 | 4 3
    2 | 3 5
    3 | 2 7
    4 | 1 9

    Now can you come up with some formulas that emit the
    number of spaces when given the line number? What about
    the number of asteriks?

    #_of_spaces = 5 - line_#
    #_of_asteriks = 2 * line_# + 1

    So your output loop basically looks like this

    for( line = 0; line < 5; ++line )
    {
    compute NrOfSpaces as 5 - line
    compute NrOfAsteriks as 2 * line + 1

    output NrOfSpaces ' '
    output NrOfAsteriks '*'

    output '\n'
    }

    And thats it for this specific pattern.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Pete

      #3
      Re: Rows: display a pattern that increments in each row

      Nicolla

      If you could draw some of the pattern or just post the homework question...
      :-) .... that would help.

      Pete

      "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
      news:3F38DE27.A 08B362F@gascad. at...[color=blue]
      >
      >
      > Nicolla MacPherson wrote:[color=green]
      > >
      > > Hi
      > > I'm a newbie and want to display a pattern that will increment with each
      > > line of display.[/color]
      >
      > what pattern?
      >[color=green]
      > > I thought i might be able to count the rows and use that info to[/color][/color]
      increment[color=blue][color=green]
      > > my display.[/color]
      >
      > You might. It depends on the pattern.
      >[color=green]
      > > I've got my starting point using setw and don't want to use cout for[/color][/color]
      every[color=blue][color=green]
      > > line of display. What should i be looking for to use.[/color]
      >
      > Using cout for every single line is the easiest thing you could do, *if*
      > your pattern is that way. It all depends on what the pattern looks like.
      > If your pattern is such that you can derive some formula from the line[/color]
      count,[color=blue]
      > then things are going the easy way.
      >
      > Example: You have to produce this pattern:
      >
      > *
      > ***
      > *****
      > *******
      > *********
      >
      > So what do you recognize? Every line consists of spaces followed
      > by '*' characters. How many are in each line? Lets make a table:
      >
      > line # | # of spaces # of asteriks
      > ---------+-----------------------------
      > 0 | 5 1
      > 1 | 4 3
      > 2 | 3 5
      > 3 | 2 7
      > 4 | 1 9
      >
      > Now can you come up with some formulas that emit the
      > number of spaces when given the line number? What about
      > the number of asteriks?
      >
      > #_of_spaces = 5 - line_#
      > #_of_asteriks = 2 * line_# + 1
      >
      > So your output loop basically looks like this
      >
      > for( line = 0; line < 5; ++line )
      > {
      > compute NrOfSpaces as 5 - line
      > compute NrOfAsteriks as 2 * line + 1
      >
      > output NrOfSpaces ' '
      > output NrOfAsteriks '*'
      >
      > output '\n'
      > }
      >
      > And thats it for this specific pattern.
      >
      > --
      > Karl Heinz Buchegger
      > kbuchegg@gascad .at[/color]


      Comment

      • Nicolla

        #4
        Re: Rows: display a pattern that increments in each row

        Hi Pete
        this is my code so far what i'm having trouble with is that the pyramid code
        has to include 2 for loops. But i just can't display the lines individually
        with out using cout all the time. If you can just give me some pointers as
        to what i'm doing wrong as I can't copy anyones code.
        Cheers

        Nicolla

        #include <iostream> //For cin, cout

        #include <iomanip> //For setw()

        using namespace std;

        const maxLines = 20;
        const maxColumns = 1;


        void main (void)
        {


        int line=0;
        int numCarats =0;
        char carat ='^';



        cout <<"\n\n\n"; //Move down the screen 3 lines
        cout <<setw(40) <<carat <<endl; //Define where to start with the display

        // while (numCarats <= 19) numCarats !=19; numCarats++)
        // {
        // cout <<'^';
        // }




        for (line = 4;line < 16; ++line) //number of lines to be displayed
        {

        //width for column

        cout.width (40);


        //position left side characters within the column

        cout <<setw(39) <<carat <<resetiosflags (ios::left)<< setw(1) <<carat;

        //Fill the left side, allows blank space to be entered before the carat
        amount entered
        //This allows for the carats to be right justified

        cout.fill (' ');
        cout <<carat <<endl;
        cout <<setw(38) <<carat <<resetiosflags (ios::left)<< setw(1) <<carat ;
        cout.fill (' ');
        cout <<carat <<endl;
        }






        }

        "Pete" <s366888@studen t.uq.edu.au> wrote in message
        news:bhaqee$jui $1@bunyip.cc.uq .edu.au...[color=blue]
        > Nicolla
        >
        > If you could draw some of the pattern or just post the homework[/color]
        question...[color=blue]
        > :-) .... that would help.
        >
        > Pete
        >
        > "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
        > news:3F38DE27.A 08B362F@gascad. at...[color=green]
        > >
        > >
        > > Nicolla MacPherson wrote:[color=darkred]
        > > >
        > > > Hi
        > > > I'm a newbie and want to display a pattern that will increment with[/color][/color][/color]
        each[color=blue][color=green][color=darkred]
        > > > line of display.[/color]
        > >
        > > what pattern?
        > >[color=darkred]
        > > > I thought i might be able to count the rows and use that info to[/color][/color]
        > increment[color=green][color=darkred]
        > > > my display.[/color]
        > >
        > > You might. It depends on the pattern.
        > >[color=darkred]
        > > > I've got my starting point using setw and don't want to use cout for[/color][/color]
        > every[color=green][color=darkred]
        > > > line of display. What should i be looking for to use.[/color]
        > >
        > > Using cout for every single line is the easiest thing you could do, *if*
        > > your pattern is that way. It all depends on what the pattern looks like.
        > > If your pattern is such that you can derive some formula from the line[/color]
        > count,[color=green]
        > > then things are going the easy way.
        > >
        > > Example: You have to produce this pattern:
        > >
        > > *
        > > ***
        > > *****
        > > *******
        > > *********
        > >
        > > So what do you recognize? Every line consists of spaces followed
        > > by '*' characters. How many are in each line? Lets make a table:
        > >
        > > line # | # of spaces # of asteriks
        > > ---------+-----------------------------
        > > 0 | 5 1
        > > 1 | 4 3
        > > 2 | 3 5
        > > 3 | 2 7
        > > 4 | 1 9
        > >
        > > Now can you come up with some formulas that emit the
        > > number of spaces when given the line number? What about
        > > the number of asteriks?
        > >
        > > #_of_spaces = 5 - line_#
        > > #_of_asteriks = 2 * line_# + 1
        > >
        > > So your output loop basically looks like this
        > >
        > > for( line = 0; line < 5; ++line )
        > > {
        > > compute NrOfSpaces as 5 - line
        > > compute NrOfAsteriks as 2 * line + 1
        > >
        > > output NrOfSpaces ' '
        > > output NrOfAsteriks '*'
        > >
        > > output '\n'
        > > }
        > >
        > > And thats it for this specific pattern.
        > >
        > > --
        > > Karl Heinz Buchegger
        > > kbuchegg@gascad .at[/color]
        >
        >[/color]


        Comment

        • Karl Heinz Buchegger

          #5
          Re: Rows: display a pattern that increments in each row



          Nicolla wrote:[color=blue]
          >
          > Hi Pete
          > this is my code so far what i'm having trouble with is that the pyramid code
          > has to include 2 for loops.[/color]

          Well. If you need 2 loops, you need 2 loops.
          BTW: I don't see 2 loops in the posted code, only 1
          [color=blue]
          > But i just can't display the lines individually
          > with out using cout all the time.[/color]

          Indeed. You do a lot of things with cout in your loop.
          I haven't analyzed it completely, but just by looking
          at it it seems to much. Try to simplify it.
          [color=blue]
          > If you can just give me some pointers as
          > to what i'm doing wrong as I can't copy anyones code.[/color]

          Well. For this to know, we would need to know your goal.
          That is: what does the pattern look like you have to produce.

          --
          Karl Heinz Buchegger
          kbuchegg@gascad .at

          Comment

          Working...