I'm new here and this forum has been a great help! Unfortunately, I'm not able to find the answer to my issue here or anywhere else on the web. I was hoping some of you can give me some help or tips on how to go about this.
The program will generate random numbers based on the max limit and the amount of random numbers that will generate.
I'm also required to find the smallest, largest number, as well as the average from all the numbers generated in the loop. The average I can find using the sum/MAX_COUNT_NUM. Unfortunately, I am stuck finding the smallest and largest number. Been at this for the past 6 hours. Please help anyway you can. Thank you.
Place the randomly generated numbers into an array, or I guess vector, since you don't know how many you will have to start with. Then do something like this:
1 2 3 4 5 6 7
int myArray[10]; //Array containing all the randomly generated numbers
int smallestNumber = UP_MAX + 1; //Holds the smallest number
for(int i = 0; i < 10; i++)
{
if(myArray[i] < smallestNumber) { smallestNumber = myArray[i] }
}
Or, if you're bold, vectors. I don't know how much about c++ you know, so if this is too far just say so.
1 2 3 4 5 6 7 8 9
std::vector<int> myVector; //vector containing RNG numbers
myVector.push_back(some integer here); //How to put items in the vector
int smallestNumber = UP_MAX + 1; //the smallest number found
for(std::vector<int>::iterator i = myVector.begin();
i != myVector.end(); i++)
{
if((*i) < smallestNumber) { smallestNumber = (*i); }
}
That all looks right, though I'm prone to making stupid mistakes.
WAIT A SECOND, I am prone to stupid mistakes. What you did should have worked, but set SMALL = UP_MAX + 1.
Unfortunately, I need to do this without arrays and vectors. In my head, I'm thinking it should work as well but it doesn't. The largest number comes out fine, but the smallest comes out as 0 which makes me scratch my head.
I'm taking a beginners course and this got me stumped, so my way of thinking may be off beat. If there are any other tips you can provide, I definitely appreciate it.
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <iomanip>
#include <limits>
// max vaild int values //
constint MAXINTVAL = std::numeric_limits<int>::max();
constint MININTVAL = std::numeric_limits<int>::min();
usingnamespace std;
int main() {
int MIN_COUNT_NUM = 0;
int RAND_NUM = 0;
int MAX_COUNT_NUM = 0;
int UP_MAX = 0;
cout << "This program creates random numbers" << "\n" << "\n";
cout << "Enter the upper limit of all generated random numbers: ";
cin >> UP_MAX;
cout << "\n" << "\n";
cout << "Enter the count of random numbers: ";
cin >> MAX_COUNT_NUM;
cout << "\n" << "\n";
cout << "Creating " << MAX_COUNT_NUM << " random numbers from 1 to " << UP_MAX << ": " << "\n" << "\n";
MIN_COUNT_NUM = 1;
// init these values with opposites to
// keep the below "test" in the domian of int values on this machine
// if randnum is larger than the smallest number possible //
int LARGE = MININTVAL;
// if randnum is smaller than the largest number possible //
int SMALL = MAXINTVAL;
int average_denom = 0;
double sum_of_input = 0;
for (; MAX_COUNT_NUM >= MIN_COUNT_NUM; MIN_COUNT_NUM++) {
RAND_NUM = rand() % UP_MAX + 1;
cout << setw(8) << RAND_NUM;
if (RAND_NUM < SMALL) {
SMALL = RAND_NUM;
}
if (RAND_NUM > LARGE) {
LARGE = RAND_NUM;
}
average_denom++;
sum_of_input+= RAND_NUM;
}
// extra new lines //
cout << endl << endl;
cout << "Largest number is: " << LARGE << endl;
cout << "Smallest number is: " << SMALL << endl;
cout << std::fixed;
cout << std::setprecision(2);
cout << "Average number is: " << sum_of_input/average_denom << endl;
return 0;
}