using for loop for random numbers

have to user for loop to generate 5 random numbers 1-30 and print them

Page 1 of 1

11 Replies - 13677 Views - Last Post: 07 April 2010 - 05:51 AM Rate Topic: -----

#1 Guest_random*


Reputation:

using for loop for random numbers

Posted 06 April 2010 - 03:35 PM

Quote

Write a program that generates five random numbers between 1-30, for each nunber generated, your program should print a line containing that number of adjacent asterisks. Your code must your a for loop to generate five numbers and a for loop nested inside this loop to print the asterisks.



#include <iostream>

using namespace std;

int main()
{
    int random;
    int number;

    for (int random=1; random <= 30; random++)
    cin << number;
    
    
    
    
    return 0;


Is This A Good Question/Topic? 0

Replies To: using for loop for random numbers

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: using for loop for random numbers

Posted 06 April 2010 - 03:38 PM

And your question is?
probably you missed our first rule for posting in the forums:

Quote

We will not do your homework for you! Do not ask us to give you code!

However, once you have written some code, and may got some specific errors\problems, feel free to post again.
Was This Post Helpful? 0
  • +
  • -

#3 PatTheGamer  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 13
  • Joined: 01-April 10

Re: using for loop for random numbers

Posted 06 April 2010 - 03:38 PM

Work on the problem some more until you get stuck. The instructions tell you how to lay out the code. Start there.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_random*


Reputation:

Re: using for loop for random numbers

Posted 06 April 2010 - 03:50 PM

View Postjapanir, on 06 April 2010 - 02:38 PM, said:

And your question is?
probably you missed our first rule for posting in the forums:

Quote

We will not do your homework for you! Do not ask us to give you code!

However, once you have written some code, and may got some specific errors\problems, feel free to post again.


#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int random;

    srand(time(0));
{
    for(int random=1; random <= 30; random=*++)
    cout << "random" << endl;
}


    return 0;

Was This Post Helpful? 0

#5 Guest_random*


Reputation:

Re: using for loop for random numbers

Posted 06 April 2010 - 03:53 PM

View PostPatTheGamer, on 06 April 2010 - 02:38 PM, said:

Work on the problem some more until you get stuck. The instructions tell you how to lay out the code. Start there.

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int random;

    srand(time(0));
{
    for(int random=1; random <= 30; random=*++)
    cout << "random" << endl;
}


    return 0;

Quote

I don't know how to get random numbers within a loop. Or actually nest a for loop within the for loop.

Was This Post Helpful? 0

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: using for loop for random numbers

Posted 06 April 2010 - 03:56 PM

well.. just some guidelines..
you should generate 5 random numbers.
so the loop should loop 5 times. for each iteration, generate a new random number, and print that number of asterisks.
 for(int random=1; random <= 5; random++) 


also, you declare twice on the random variable. (the second time inside the for loop). no need to declare twice.
and as for that:
random=*++

you probably meant:
random++

to raise random's value by 1.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_random*


Reputation:

Re: using for loop for random numbers

Posted 06 April 2010 - 04:04 PM

View Postjapanir, on 06 April 2010 - 02:56 PM, said:

well.. just some guidelines..
you should generate 5 random numbers.
so the loop should loop 5 times. for each iteration, generate a new random number, and print that number of asterisks.
 for(int random=1; random <= 5; random++) 


also, you declare twice on the random variable. (the second time inside the for loop). no need to declare twice.
and as for that:
random=*++

you probably meant:
random++

to raise random's value by 1.

Quote

Yeah that gives me 12345 but I need 5 random numbers between 1-30 and show it with asterisks. That's what I don't get. How to generate random numbers through a for loop and assign it to asterisks.

Was This Post Helpful? 0

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: using for loop for random numbers

Posted 06 April 2010 - 05:38 PM

the main loop iterate 5 times (5 numbers to be generated).
use the rand function.
you need a number between 1 - 30, so:
int randNum = rand() % 30 + 1;

here is the nested loops:
//main loop to iterate 5 numbers
for(int i = 1; i <= 5; i++){
  //generate random number
  int randNum = rand() % 30 + 1;
  //the nested loop to print asterisks the amount of the randNum
  for(int j = 0; j < randNum; j++){
       cout<<"*";
  }
  //new line for each number
  cout<<endl;
}

Was This Post Helpful? 0
  • +
  • -

#9 Guest_random*


Reputation:

Re: using for loop for random numbers

Posted 06 April 2010 - 05:49 PM

View Postjapanir, on 06 April 2010 - 04:38 PM, said:

the main loop iterate 5 times (5 numbers to be generated).
use the rand function.
you need a number between 1 - 30, so:
int randNum = rand() % 30 + 1;

here is the nested loops:
//main loop to iterate 5 numbers
for(int i = 1; i <= 5; i++){
  //generate random number
  int randNum = rand() % 30 + 1;
  //the nested loop to print asterisks the amount of the randNum
  for(int j = 0; j < randNum; j++){
       cout<<"*";
  }
  //new line for each number
  cout<<endl;
}


#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int random;

    srand(time(0));
{
    int random = rand() % 30 + 1;
    for(int random=1; random <=5; random++)
{
    cout << "*" << endl;


    return 0;
}


Quote

The errors are unused random variable and expected } at end of input.

Was This Post Helpful? 0

#10 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: using for loop for random numbers

Posted 06 April 2010 - 06:32 PM

Did you take a look at my prev posts?
I already said, that it's not a good idea to declare the same variable twice, but now you declare the same variable 3 times?
int random; 
...
int random = rand() % 30 + 1; 
...
    for(int random=1;
..


you have misplaced curly braces. once here:
    srand(time(0)); 
{ //<<why do you need those?

and here:
  for(int random=1; random <=5; random++) 
{//if you open curly braces, why not close them? 


However, my prev post had all the code. just insert it inside a main..
Was This Post Helpful? 0
  • +
  • -

#11 Guest_random*


Reputation:

Re: using for loop for random numbers

Posted 06 April 2010 - 08:01 PM

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    int random;

    int randNum = rand() % 30 + 1;
    {
        for(int random=1; random <=5; random++)
        {
            cout << "*";
        }
    }
    
    cout << endl;

    return 0;
}



Quote

Only producing one number.

Was This Post Helpful? 0

#12 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: using for loop for random numbers

Posted 07 April 2010 - 05:51 AM

if you looked at my code carefully, it should have solved the problem.
you should generate the random number inside the for loop.
and have a nested loop to "draw" the asterisks.
take a look at that program:
#include <iostream>
using namespace std;

int main(){
//main loop to iterate 5 numbers 
for(int i = 1; i <= 5; i++){ 
  //generate random number 
  int randNum = rand() % 30 + 1; 
  //the nested loop to print asterisks the amount of the randNum 
  for(int j = 0; j < randNum; j++){ 
       cout<<"*"; 
  } 
  //new line for each number 
  cout<<endl; 
}
return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1