In: Computer Science
) Use functions and arrays to complete the following programs. Requirements: • 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.
Program:
#include <iostream>
using namespace std;
const int first_Size = 4; //constant variable to hold size of
first sequence
const int second_Size = 8; //constant variable to hold size of
second sequence
//method to take input values from user for sequence
void getSequence(int n, int array[]);
//method to find average of sequence values
double getAverage(int size, int arr[]);
//method to print result which sequence has greater average
void printResult(double first, double second);
int main()
{
int first[first_Size];
int second[second_Size];
cout<<"Enter four numbers to form first sequence: ";
getSequence(first_Size,first);
cout<<endl<<"Enter eight numbers to form second
sequence: ";
getSequence(second_Size,second);
double avgFirst = getAverage(first_Size,first);
double avgSecond = getAverage(second_Size,second);
cout<<endl<<"Average of Sequence 1:
"<<avgFirst;
cout<<endl<<"Average of Sequence 2:
"<<avgSecond;
printResult(avgFirst,avgSecond);
return 0;
}
void getSequence(int n, int array[])
{
int i=0;
while(i<n)
{
cin>>array[i];
i++;
}
}
double getAverage(int size, int arr[])
{
int j=0;
double sum = 0;
while(j<size)
{
sum = sum + arr[j];
j++;
}
double average = sum/size;
return average;
}
void printResult(double first, double second)
{
if(first>=second)
{
cout<<endl<<endl<<"Sequence 1 has larger average
than Sequence 2";
}
else
{
cout<<endl<<endl<<"Sequence 1 has larger average
than Sequence 2";
}
}
Output: