In: Computer Science
• You should use the divide-and-conquer strategy and write multiple functions. • You should always use arrays for the data sequences. • You should always use const int to define the sizes of your arrays. a. Write a C++ program. The program first asks the user to enter 4 numbers to form Sequence 1. Then the program asks the user to enter 8 numbers to form Sequence 2. Finally, the program shall tell which sequence has a larger average. For example, if the user enters 3 4 5 6 for Sequence 1, and 0 1 2 3 4 5 6 7 for Sequence 2. Your program should know the average of Sequence 1 is 4.5, and the average of Sequence 2 is 3.5. Therefore, your program should conclude that Sequence 1 has a larger average.
Code -
#include <iostream>
using namespace std;
float calcAverage(int a[],int size){
int sum = 0;
for(int i = 0 ; i < size ; i++){
sum+=a[i];
}
return (float)sum/size;
}
int main()
{
const int seq1 = 4;
const int seq2 = 8;
int array1[seq1],array2[seq2];
cout<<"Enter sequence 1 ";
int i;
for(int i = 0 ; i < seq1 ; i++){
cin>>array1[i];
}
cout<<"Enter sequence 2 ";
for(int i = 0 ; i < seq2 ; i++){
cin>>array2[i];
}
float avg1 = calcAverage(array1,seq1);
float avg2 = calcAverage(array2,seq2);
cout<<"Average first is "<<avg1<<endl;
cout<<"Average second is "<<avg2<<endl;
if(avg1>avg2){
cout<<"\n Sequence 1 has larger average";
}
else{
cout<<"\n Sequence 2 has larger average";
}
return 0;
}
Screenshots -