Get a number n from a user. Generate n random numbers and store in the list. Display the list. Compute summation and average of all numbers in the list then display the result on screen.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int i=0, n, sum = 0;
float average;
cout << "Enter the number of random numbers: ";
cin >> n;
int arr[n];
srand(static_cast<unsigned int>(time(nullptr)));
for(i = 0; i < n; i++){
arr[i] = (rand() % 10) + 1;
cout << arr[i] << "\t";
sum += arr[i];
}
cout << "\n\nSum: \t\t" << sum << endl;
cout << "Average: \t" << (float)sum/n << endl;
return 0;
}
Comments
Leave a comment