c++ - Need help finding the largest number and the smallest number of a group of numbers -
this question has answer here:
as title says, need finding largest number , smallest number in group of numbers display @ end. group of numbers randomly generated every time. appreciate if explain how make displays random numbers chosen , not totals while still calculating totals (for average). :)
p.s sorry if indentation weird, first post here.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int total = 0; // total of numbers int aot; //amount of times int i; cout << "how many random numbers should machine make?" << endl; cin >> aot; cout << endl; srand(time(0)); for(i=1;i<=aot;i++) { //makes random number , sets total total = total + (rand()%10); //just fancy text cout << "the total after " << << " random number/s "; cout << total << endl; } cout << endl; cout << endl; // data on numbers cout << "the amount of numbers there " << aot << endl; cout << "the average random numbers " << total / aot << endl; }
assign random numbers generated , print temporary number show group of number users. initialize variables, store smallest , largest number of group, temporary number.
compare number random number generated find whether smaller 'smallest' or larger 'largest' , assign them variables accordingly.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int smallest, largest, temp; int aot; //amount of times int i; cout << "how many random numbers should machine make?" << endl; cin >> aot; cout << endl; srand(time(0)); cout<<" random numbers are:"; smallest = rand()%10; cout<<smallest<<'\t'; largest = smallest; for(i=1;i<aot;i++) { temp = (rand()%10); cout<<temp<<'\t'; if(temp < smallest) { smallest = temp; } else if(temp > largest) { largest = temp; } } cout << endl; cout << endl; // data on numbers cout << "the smalles number " << smallest << endl; cout << "the largest number " << largest << endl; }
Comments
Post a Comment