the program below generates random numbers, tallys them, adds them, and displays min and max as well, im having one slight problem, if i put for it to only print 2 random numbers, from time to time the maximum will be incorrect, any idea why? thanks in advance.
#include<iostream>
#include<cstdlib>
usingnamespace std;
int main() {
constint LENGTH = 10;
int tallyArray[LENGTH] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // all elements 0
int totalValues;
int sum = 0;
int min = 9;
int max = 0;
cout << "This program analyzes the c++ rand() function\n\n";
cout << "Enter the number of random values: ";
cin >> totalValues;
srand(time(0));
for ( int i = 0; i < totalValues; i++) {
int randNumber = rand() % 10;
tallyArray[randNumber]++;
sum = sum + randNumber;
if (min > randNumber or randNumber == 0) {
min = randNumber;
}elseif (randNumber > max){
max = randNumber;
}
}
for (int i = 0; i < LENGTH; i++) {
cout << i << ':' << tallyArray[i] << "\n";
}
cout << "summation: " << sum << endl;
cout << "Maximum: " << max << endl;
cout << "Minimum: " << min << endl;
return 0;
}
it seems to be having a similar problem, for example one of the runs i did , i got one 4 and one 7
and for some reason maxwas set to 0, although min was set correctly at 4
gunnerfunner, unfortunately we have not yet covered the use of std:: cout and many of the other things that you mentioned even though it would make everything so much easier, we are not allowed to use them :( , Although i was able to fix it by evaluating both conditions on every iteration like Iwave mentioned above! :D it seems to be giving me the correct min and max every single time now , thanks for your input guys :D