In: Computer Science
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: Vector values: 3, 78, 55, 37, 8, 17, 43, 60, 94, 1 Average value: 39.6
Code:
#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<iomanip>
using namespace std;
// function to calculate average and return float
float average(int a[]) {
    float ave=0.0;
    for(int i=0;i<10;i++)
        ave += a[i];
    return ave/10;
}
int main() {
    int vector[10];
    // seed random number
    srand((unsigned) time(0));
    // generate 10 random numbers and insert into
array
    for(int i=0;i<10;i++)
           
vector[i] = (rand() % 100) + 1;
    // display the contents of the array
    cout << "The elements of the vector are:-
" << endl;
    for(int i=0;i<10;i++)
        cout << vector[i]
<< "\t" ;
    cout << endl;
    // get the average of the vector
    float a = average(vector);
    // display average to 1 floating-point
    cout << "Average : " << fixed
<< setprecision(1) << a << endl;
    return 0;
}

Output:
