For this lab you should write a program that contains a main
For this lab you should write a program that contains a main function and one additional function (to generate random numbers and determine if the random number is equal to, below or above a given number). Your main function should prompt the user to enter a whole number between 0 and 100 not including 0 and 100 hundred. (You may assume the user enters a valid number or check it within main as practice). Your main should then call the function. After the function has completed, your main function should output how many of the generated numbers were equal to the entered value, how many of the generated numbers were less than the entered value, and how many of the generated numbers were greater than the entered value.
Your function should use a loop to generate 100 random numbers between 0 and 100 including 0 and 100, then count how many of the generated numbers were equal to the entered value, how many were less than the entered value, and how many were greater than the entered value. (Review pages 127 – 129 of your text book for random number generation. Your function should send how many were equal to, less than, and greater than the entered value back to the function call in main.
Do not use global variables and place your function after main.
Remember to write comments immediately before your function that specifies the purpose of the function, input to the function (both what is passed to the function and any input generated within the function), output from the function (both what is sent back to the function call and what is printed to the screen) and processing needed. The comments for your function should be in addition to the overall comments for the program.
Solution
#include <iostream>
#include <stdlib.h>
using namespace std;
/*
Generates Stats
Input : Number entered by user,reference variables greater count which holds the count
of random numbers greater than the user given number,equal count which holds the count of
random numbers equal to the user given number and same with less_count reference variable.
*/
void generateStats(int number,int &greater_count,int &equal_count,int &less_count)
{
//Holds the random number in each iteration
int temp;
int i;
//Generates 100 numbers and count the number of numbers less than,greater than or equal to given number
for(i=0;i<100;i++)
{
temp = rand() % 101; // Generate number between 0 to 100
//If number equal to random number
if(temp==number)
equal_count++;
//If random number less than number
else if(temp<number)
less_count++;
//If random number greater than number
else
greater_count++;
}
}
int main(){
//Holds user given number
int number;
//Get the number from user
cout << \"Input a number between 0 and 100 not including 0,100 : \";
cin >> number;
int greater_count = 0 , equal_count = 0,less_count = 0;
//If not valid then input again
while(number<=0 || number>=100)
{
cout << \"Number should be in specified range.Please Enter again.\";
cin >> number;
}
//Get the counts
generateStats(number,greater_count,equal_count,less_count);
//Output the result
cout << \"Count of random numbers greater than \" << number << \" is : \" << greater_count << endl;
cout << \"Count of random numbers less than \" << number << \" is : \" << less_count << endl;
cout << \"Count of random numbers equal to \" << number << \" is : \" << equal_count << endl;
return 0;
}
OUTPUT:
Input a number between 0 and 100 not including 0,100 : 50
Count of random numbers greater than 50 is : 48
Count of random numbers less than 50 is : 52
Count of random numbers equal to 50 is : 0

