jewport101
jewport101

Reputation: 23

Writing a simple for statement program that shows asterisks

i want to create a program that uses two for statements to display the pattern of asterisks shown below.

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

i can make them using quite a few for statements but i only want to use 2 of them, to make it shorter this is what i have:

 #include <iostream>
 #include <iomanip>
 using namespace std;

 int main()
 {
 int row = 1;
 int astrk = 1;

 for ( int row = 1; row < 2; row += 1)
 {
 for ( int astrk = 1; astrk <= 2; astrk += 1)
 cout << '*';
 cout << endl;
 }// end for
 for ( int row = 1; row < 2; row += 1)
 {
 for ( int astrk = 1; astrk <= 4; astrk +=1)
 cout << '*';
 cout << endl;
 }//end for
 for ( int row = 1; row < 2; row += 1)
 {
 for ( int astrk = 1; astrk <= 6; astrk += 1)
 cout << '*';
 cout << endl;
 }// end for
 for ( int row = 1; row < 2; row += 1)
 {
 for ( int astrk = 1; astrk <= 8; astrk += 1)
 cout << '*';
 cout << endl;
 }// end for
 for ( int row = 1; row < 2; row += 1)
 {
 for ( int astrk = 1; astrk <= 10; astrk += 1)
 cout << '*';
 cout << endl;
 }// end for

 return 0;
 }

help please? :)

Upvotes: 1

Views: 16167

Answers (4)

jdialogc
jdialogc

Reputation: 1

You can also try out the following code

  for(int i=0;i<6;i++){//used to print out rows

        for(int j=0;j<=i;j++){used to print asterisk in each row
                cout<<"**";
                }
        cout<<endl;
        }

for more details visit: http://include.site40.net/aasterics.php

Upvotes: 0

MFarmer
MFarmer

Reputation: 1716

Note the pattern:

Line 1 has 2 asterisks, Line 2 has 4 asterisks, Line 3 has 6 asterisks, ...

The number of asterisks to print is determined by multiplying the line number by 2.

Like emartel showed, you can dynamically determine how many times the inner for loop will run by basing the inner for loop's test expression upon the current i value of the outer loop (line number) multiplied by 2 (number of single asterisks to print).

Most problems like these will come down to having the inner for loop use the outer for loops counter value in some clever way to print the desired output pattern.

Upvotes: 0

emartel
emartel

Reputation: 7773

You should rewrite this using two for loops, once controlling the rows, and the other one controlling the column.

You have 5 rows, and on each row, you have 2, 4, 6, etc... stars.

for(int i = 1; i <= 5; ++i) // five rows
{
    for(int j = 1; j <= i * 2; ++j) // we have 2 stars for each row number -> 2, 4, 6, etc...
    {
        cout << "*";
    }
    cout << "\n";
}

Try to understand your problem, figure out a solution on paper and then try to implement it, it will be way simpler if you're learning how to program.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

You should have an outer loop for the number of lines, and an inner loop for the asterisks. When the inner loop is done printing the asterisks, the outer loop prints a newline, and increases the asterisk count.

In pseudo-code:

for (from one to the number of lines)
{
    for (one to the number of asterisks)
    {
        print an asterisk
    }

    print a newline
    increase number of asterisks
}

Upvotes: 1

Related Questions