## wakesin

hey there Daniweb.

So I am in need of some help with a project I have.

We need to create a program that will print out rows of asterisks as tall as defined by the user.

``````For example, if the user entered, 1 2 3 4 3 2 1 10 0, my program should print:

*
*
*
*
*
*
*   *
***  *
***** *
********``````

1 star in the first row, 2 in the 2nd, and so on.

so far all i have is that i need to place the numbers in an array.....I am very lost and any insight would be appreciated!

## iamthwee 1,547

This would be simple if the graph was on its side.

A few for loops would suffice. Yes you are right in that you should dump it into an array.

This is a simple transformation problem.

## wakesin

any other insight?

## wakesin

heres what i have so far

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

int main();
{

const int MAX = 100;
int values[MAX];

for (int i = 0; i < MAX; i++;
{
cout << "Chart " << endl;
cin >> i;``````

## VernonDozier 2,218

You need a nested loop. The trick in these(always) is to figure out what the loop counter boundaries are.

``````int i, j;

for(i = ?; i < ?; i++)
{
for(j = ?; j < ?; j++)
{
cout << " ";
}
for(j = ?; j < ?; j++)
{
cout << "*";
}
cout << endl;
}``````

Now fill in the question marks for a height of 1, then a height of 2, then a height of 3, then a height of 4. By this time you should see a pattern based on the height.

## VernonDozier 2,218

We need to create a program that will print out rows of asterisks as tall as defined by the user.

``````For example, if the user entered, 1 2 3 4 3 2 1 10 0, my program should print:

*
*
*
*
*
*
*   *
***  *
***** *
********``````

1 star in the first row, 2 in the 2nd, and so on.

Disregard my last post(sort of). I had simply looked at the pyramid and the description didn't make sense, so I disregarded it. Replace the word "row" with "column" and it matches. But make sure you are reading it right and you really want "column", not "row", because they are two different problems.

Your rough skeleton is still above, except you may need to add some inner loops. Or you can approach it completely differently and use a 2-D array or something else. But the outer loop will be the same. You start by filling in the question marks for the outer/i loop. You need to go through that loop 10 times because you need ten lines because 10 is the maximum number the user entered.

## prvnkmr449 -8

``````#include<dos.h>
#include<stdio.h>
#include<conio.h>
void main()
{
union REGS i,o;
int max_row=20;
int num[5];
clrscr();
i.h.ah=2;   //positioning the cursor
i.h.bh=0;
for(int j=0;j<5;j++)
{
clrscr();
printf("Enter number at %d : ",j);
scanf("%d",&num[j]);
}
clrscr();
for(j=0;j<5;j++)
{
i.h.dl=2+j;//x-direction
for(int row=max_row-num[j],n=0;row<max_row;row++,n++)
{
i.h.dh=max_row-num[j]+n;//y-direction
int86(0x10,&i,&o);
printf("*");
}
}
getch();
}``````
commented: Horrible code -3

## Duki 552

Personally, here's what I would do.

Imagine drawing the stars logically:

``````[ ][ ][ ][*][ ][ ][ ]
[ ][ ][*][*][*][ ][ ]
[ ][*][*][*][*][*][ ]

[*][*][*][*][*][*][*]``````

We need to figure out how to input these accordingly though. For me, it makes more sense to initialize our array with all *'s to start with. This just makes our loops easier to create (you'll see later):

``````int temp;
const int COL = 6;   //These consts could be done away with
const int ROW = 4;   //if you're using a vector

int ROW_INDEX = ROW - 1;  //We use this to keep track of which row we're on in the loop

char stars[ROW][COL] ;

//Code excluded - this initialization is easy enough to figure out.

[*][*][*][*][*][*][*]

[*][*][*][*][*][*][*]

[*][*][*][*][*][*][*]

[*][*][*][*][*][*][*]``````

Now that we have our array initialized, it's time to get a value and then adjust our first column accordingly:

``````cout << "How many stars: " << flush ;

for (int i = 0 ; i < ... ; i++)    //For every column...
{
cin >> temp;
for (int j = ... ; j >= ... ; j--)   //...we need to adjust the rows...
stars[j][i] = ' ';   //...removing '*' where necessary.
}``````

I've left blanks for you to fill in.

So, now that we have this awesome-sauce picture stored in our array, we need to figure out how to output it. Easy enough - Just remember the picture is inverted from what we've logically stored.

``````for (int i = ... ; i >= 0 ; i--)  //What should we output first - notice how this for() is different than you're used to.
{
for (int j = 0 ; j < ...; j++)
cout << stars[i][j];

cout << endl ;
}``````

I've left blanks here too for you to fill in. Here is my sample output:

``````How many stars: 1 2 3 2 1 4
*
*  *
*** *
******

How many stars: 1 4 1 4 1 4
* * *
* * *
* * *
******``````

Of coarse this can be modified to allow the user to input the size of the drawing - but you get the idea.

commented: Nice post +15
commented: Nice +13

## wakesin

well i havnt made any progress on this. it is due next wednesday by midnight and i am still lost as ever. i just spoke with my professor and they insisted i do it this way

``````#include<stdio.h>
using namespace std;

void main()

{
const int MAX = 100;
int currentrow;
int values [MAX]

for (int i=0; i < MAX; i++)``````

during lecture they brought up something about making a 2d array and using

``if (MAX – currentrow +1) < (value in array)``

## Duki 552

My code works just fine if you can fill in the 5 blanks I left you.

## prvnkmr449 -8

``````#include<iostream.h>
#include<conio.h>
void main()
{
const int MAX=6;
int  value[MAX];//for number of column
for (int i = 0; i < MAX; i++)
{
cout << "Chart " << endl;
cin >> value[i];
}
for(int r=0;r<20;r++)
{
for(int c=0;c<MAX;c++)
{
if(value[c]>=20-r)
cout<<"*";
else cout<<" ";
}
cout<<endl;
}
getch();
}``````

## packetpirate

You people are over-analyzing this. All he needs to do is use IOMANIP. He should use a for loop to print however many asterisks are specified to a line, aligned to the right after he sets a width for each row. Then he should fill in the blank spaces with a " " character.

## wakesin

after a bit of tinkering, this is what i have so far....its almost there!!!

``````#include<iostream>
using namespace std;
int main()
{
const int MAX=6;
int  value[MAX];//for number of column
for (int i = 0; i < MAX; i++)
{
cout << "Chart " << endl;
cin >> value[i];
}
for(int r=0;r<20;r++)
{
for(int c=0;c<MAX;c++)
{
if(value[c]>=20-r)
cout<<"*";
else cout<<" ";
}
cout<<endl;
}

}``````

only problem is i need an UNLIMINTED ammount of rows to be specified, and after a user inputs a 0, it will print out. IE

\$ Chart
5 4 1 0

*
**
**
**
***

any ideas?

## packetpirate

Yeah... use an IF statement... IF != 0, take the number and generate the row...

## VernonDozier 2,218

only problem is i need an UNLIMINTED ammount of rows to be specified, and after a user inputs a 0, it will print out. IE

\$ Chart
5 4 1 0

*
**
**
**
***

any ideas?

You need to get clearer on rows versus columns. Which is it? The example above matches neither. If you're talking columns, it would match "1 4 5 0", not "5 4 1 0". If the 5, 4, and 1 represent the number of asterisks in a row, it doesn't make sense at all. Is "5, 4, 1, 0" is supposed to yield THIS?

``````*
**
**
**
***``````

[EDIT]

Just looked at the post and there's some confusion on the drawing you intended that's not apparent from the postings. Your previous post looks like mine, but that's only because your formatting appears to have been stripped. With the spacing intact, your post looked like this:

``````*
**
**
**
***``````

i.e. right justified, not left. Which did you intend?

## Duki 552

His original post explains what he wants, quite understandably.

>>You people are over-analyzing this. All he needs to do is use IOMANIP. He should use a for loop to print however many asterisks are specified to a line, aligned to the right after he sets a width for each row. Then he should fill in the blank spaces with a " " character.

Wrong. You're thinking he needs rows, not columns. (again, the original post is very handy when answering questions).

What you're explaining would produce

``````Enter the numbers: 1 2 3 4 3 7

*
**
***
****
***
*******``````

When in fact, he needs it to display

``````*
*
*
* *
****
*****
******``````

>>
only problem is i need an UNLIMINTED ammount of rows to be specified, and after a user inputs a 0, it will print out.

Use a vector instead of an array.

## VernonDozier 2,218

>> His original post explains what he wants, quite understandably.

>> Wrong. You're thinking he needs rows, not columns. (again, the original post is very handy when answering questions).

No, his first post does not explain things well, He says "rows" in words, but the PICTURE clearly reflects "columns", not "rows". The description does not match the drawing. Hence the confusion/request for clarification.

We need to create a program that will print out rows of asterisks as tall as defined by the user.

``````For example, if the user entered, 1 2 3 4 3 2 1 10 0, my program should print:

*
*
*
*
*
*
*   *
***  *
***** *
********``````

1 star in the first row, 2 in the 2nd, and so on.

The picture is actually 1 star in the first row, 1 in the second row, 1 in the third row, 1 in the fourth, 1 in the fifth, 1 in the sixth, 2 in the seventh, 4 in the eight, 6 in the ninth, 8 in the tenth.

I imagine we're supposed to go by the picture, which means swapping the word "row" for "column" in his original post and vice-versa, but who knows?

## Duki 552

I see where that could be confusing now. In general, I tend to associate height with columns and length with rows. :)

## wakesin

ahahha maybe i was unclear

the user must input numbers, 1 2 3 5 6 8 9 0

the program will then print one column for each number specified, and stop printing at 0

i think the if statement should be something like...

``if (j!=0; j>=100;j (i duno what should go here)``

basically it needs to be an if statment with the conditions that if its equal to 0, or more than 100, the loop ends

any thoughts?

## VernonDozier 2,218

i think the if statement should be something like...

``if (j!=0; j>=100;j (i duno what should go here)``

basically it needs to be an if statment with the conditions that if its equal to 0, or more than 100, the loop ends

It's either an if statement or it's a loop or it's an if statement inside of a loop, but an if statement isn't a loop and while a for-loop can have a condition in it, it wouldn't be the one you have.

If you want an if statement:

``````if (i > 100 || i == 0)
{
// do whatever
}
else
{
// do something else
}``````

If you want a loop, I imagine it's something like this:

``````while(!(i > 100 || i == 0))
{
// do something
}``````

If you want it as a for loop, perhaps it's something like this:

``````for(int i = 0; i <= 100 && someVariable != 0; i++)
{
// do something
}``````

Not sure what the 100 represents here.

## Duki 552

You're referring to this part of my code, right

``````cout << "How many stars: " << flush ;

for (int i = 0 ; i < ... ; i++)    //For every column...
{
cin >> temp;
for (int j = ... ; j >= ... ; j--)   //...we need to adjust the rows...
stars[j][i] = ' ';   //...removing '*' where necessary.
}``````

Here you need to index very carefully. I've given you the last part of the for() loop you're asking about - it should be j-- (that's not a blank). j--, as compared to j++, does exactly the opposite. Instead of incrementing it decrements. So think of it as, you're starting at MAX_ROWS (whatever you want that to be) and you're removing stars all the way down to your Temp variable (the value the user inputs for that column). Does that help?

## prvnkmr449 -8

``````#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
const int MAX=6;
int  value[MAX];//for number of column
for (int i = 0; i < MAX; i++)
{
clrscr();
cout << "Enter Chart value for(1-20) "<<i <<" :"<< endl;
cin >> value[i];
}
for(int r=0;r<20;r++)
{
for(int c=0;c<MAX;c++)
{
if(value[c]>=20-r)
cout<<" ** ";
else cout<<"    ";
}
cout<<endl;
}
cout<<setw(3)<<value[0]<<" "<<setw(3)<<value[1]<<" "<<setw(3)<<value[2]<<" "<<setw(3)<<value[3]<<" "<<setw(3)<<value[4]<<" "<<setw(3)<<value[5];
getch();
}``````

## CSU CHICO

yeah im in the same class and the professor's lectures are of no help at all.. she teaches it as if we already have knowledge of c++

## Rock426

How can i write a program that uses for print statements to print 6 pattern of asterisks?