homework problem

hello all; and thanks in advance for any help that comes my way.

i have an assignment in my C++ class that i'm having a problem with. it deals with bar chart plottting.

write a program that generates 5 random numbers between 1 and 30 and for each number generated, the program should print a line containing that number in adjacent asterisks. (ie: for the number six: ******). a for loop should be used to generate the 5 random numbers and a nested for loop to print the asterisk. I already have the random numbers done but I'm having problems with the asterisks. here is my code so far:

int main()

#include <iostream>
using namespace std;

int main ()
{
int randNumber; // random number
int i;

for(i = 0; i <= 5; i++)
{
cout << rand () % 30 +1 << endl;
}


i'm not sure how to print the asterisk that match the random number generated.
thank you, again.
To generate a random number, you need to #include a library to use it. I use #include <cmath>

You also need to seed the random number generator at the beginning of main - srand(time(0));

To do your loop, I would do something like this in pseudo-code:
1
2
3
4
5
for int x = 0; x < 5; x++ //5 integers
    set an int variable with a random number
    for int y = 0; y < variable; y++ //nested for loop
        print an asterisk
    print a newline





Topic archived. No new replies allowed.