IDNLearn.com offers a unique blend of expert answers and community insights. Get accurate and detailed answers to your questions from our dedicated community members who are always ready to help.

Using vectors: Create a vector of integers of size 100, with random numbers ranging from 1 to 50. Print out these 100 numbers in a readable format. Find the average of these 100 numbers, print out the overall average. Then take the original list of 100 numbers, and find all the numbers that are between 1 and 30 and put them into a new vector. Do the same with all the numbers from 31 to 50 putting them into a third vector. Find the average of vectors 2 and 3 and call them avg1 and avg2. Print out avg1 and avg2. Find the average of avg1 and avg2. Write a final output line showing/comparing the average from step 3 and the average from step 7 are not the same, and hence the average of averages doesn't work. You have just proved a mathematical theorem.

Sagot :

Answer:

In C++

#include <iostream>  

#include <vector>  

using namespace std;  

int main() {  

   vector<int> Vector1, Vector2, Vector3; int total2 = 0, total3 = 0, count2 = 0, count3 =0;

   srand((unsigned)time(NULL));

   int total = 0;

   for (int kt =0; kt < 100; kt++){

       int b = rand() % 50 + 1;

       Vector1.push_back(b);

       cout << Vector1[kt] << " ";

       total+=b;    }

   double avg = (total * 1.0)/100.0;

   cout<<endl<<"Overall Average: "<<avg<<endl;

   for (int kt =0; kt < 100; kt++){

       if(Vector1[kt]>=1 && Vector1[kt]<=30){

           Vector2.push_back(Vector1[kt]); total2+=Vector1[kt]; count2++;        }

       else{

           Vector2.push_back(Vector1[kt]); total3+=Vector1[kt]; count3++;        }    }

       double avg1 = (total2 * 1.0)/count2, avg2 = (total3 * 1.0)/count3;

       cout<<"Average 1 - 30: "<<avg1<<endl;

       cout<<"Average 31 - 50: "<<avg2<<endl;

       double avg3 = (avg1 + avg2)/2;

       cout<<"Average of Average: "<<avg3<<endl;

       if(avg == avg3){            cout<<"Both average are equal";        }

       else{        cout<<"Both average are not equal";        }

   return 0;}

Explanation:

See attachment for complete question where comments were used to explain each line

View image MrRoyal