Write a C++ program that
1) generates a vector containing 10 different random integers with values between 1 and 100, then
2) calculates the average value in that vector in floating point format with 1 decimal place. Output the vector values and the average value to cout.
Your program output should look like this (your numbers will be different):
Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1
Average value: 39.6
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; int main() { srand(time(NULL)); vector<int> v; for (int i = 0; i < 10; ++i) { v.push_back(1 + (rand() % 100)); } cout << "Vector values: "; double avg = 0; for (int i = 0; i < 10; ++i) { cout << v[i]; avg += v[i]; if (i != 9) { cout << ", "; } } cout << endl << "Average value: " << setprecision(1) << fixed << avg << endl; return 0; }
Get Answers For Free
Most questions answered within 1 hours.