Question 1 (6 marks) Purpose: To practice using loops; to practice accumulating
ID: 3675868 • Letter: Q
Question
Question 1 (6 marks)
Purpose: To practice using loops; to practice accumulating values using loops
Degree of Difficulty: Easy
For this question, you're going to experiment some more with random numbers.
Write a program that uses a while loop to generate 100 different random numbers (the random numbers
will range from 1 to 100). The program should keep track of how many of the random numbers fall
within the following 3 ranges:
• "Small": the number is less than 10
• "Big": the number is greater than 90
• "Medium": the number is between 10 and 90 (inclusive)
Once the loop is done, then program should of course print out the number of random numbers that
fell within each range. The following is an example of the output of your program.
It's time to make some random numbers! There were 8 small numbers.
There were 13 big numbers. There were 79 medium numbers.
Once you are sure your program is working, run your program 3 times to test it and copy the results
to a testing file that you will hand in.
Random Numbers Reminder
As a reminder from Assignment 3, you need the following infrastructure to use random numbers in
your program.
You need the following includes at the top:
#include <cstdlib>
#include <time.h>
The very first line of your main() function should be:
srand(time(NULL));
Finally, the following line of code will generate a random number from 1 to 100 and store it in a
variable called number.
int number = (rand() % 100) + 1;
Explanation / Answer
/**C++ program that generates 100 random numbres and count the values
within the range. If random number is less than 10, small
if random number greater than 90, big
if random number is greater than or equal to 10 and less than
or eqaul to 90 then medium . Then print the count of smakk,
big and medium count of random numbers*/
//header files
#include<iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
//set SMALL and BIG values
int SMALL=10;
int BIG=90;
//set counters to zer
int smallCount=0;
int bigCount=0;
int mediumCount=0;
//set lenght value
int LENGHT=100;
//seed a random number generator
srand(time(NULL));
//set index to zero
int index=0;
//run while loop from 1 to 100
while(index<LENGHT)
{
//generate a random number in a range of 1 to 100.
int number = (rand() % 100) + 1;
//Check if numbre is less than SMALL
if(number<SMALL)
//increment the smallCount by one
smallCount++;
//Check if numbre is greater than BIG
else if(number>BIG)
//increment the bigCount by one
bigCount++;
//Check if numbre is greater than or equal
//SMALL and less than or equal to BIG
else if(number>=SMALL && number<=BIG)
//increment the mediumCount by one
mediumCount++;
//incrememt the index by one
index++;
}
cout<<"It's time to make some random numbers!"<<endl;
//print the smallCount
cout<<"There were "<< smallCount<<" small numbers."<<endl;
//print the bigCount
cout<<"There were "<<bigCount<<" big numbers."<<endl;
//print the mediumCount
cout<<"There were "<<mediumCount<<" medium numbers."<<endl;
//pause the program output on console
system("pause");
return 0;
}
sample output:
Run1:
It's time to make some random numbers!
There were 10 small numbers.
There were 1 big numbers.
There were 89 medium numbers.
Run2:
It's time to make some random numbers!
There were 9 small numbers.
There were 11 big numbers.
There were 80 medium numbers.
Run3:
It's time to make some random numbers!
There were 7 small numbers.
There were 12 big numbers.
There were 81 medium numbers.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.