Question

In: Computer Science

• You should use the divide-and-conquer strategy and write multiple functions. • You should always use...

• 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.

Solutions

Expert Solution

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 -


Related Solutions

A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
1. If we view the selection sort as an application of "divide and conquer" strategy, what...
1. If we view the selection sort as an application of "divide and conquer" strategy, what is the two-part partitioning of the index range [0, n)? Is it a balanced partition? 2. For the "divide and conquer" strategy for developing algorithm, do we pursue balanced partitioning or unbalanced partitioning? Why? 3. For the quicksort, the selection of pivot determines the quality of partitioning. Do we have any easy or cheap way for selecting a good pivot that always leads to...
Step (D) of the divide-and-conquer strategy (i.e. combine the solutions to smaller instances of the problem...
Step (D) of the divide-and-conquer strategy (i.e. combine the solutions to smaller instances of the problem to obtain the solution of the original instance) is not a necessary step for this design strategy. Mergesort is an example of such cases. Select one: True False
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element...
1a. Write pseudocode for a divide-and-conquer algorithm for finding the po- sition of the largest element in an array of n numbers. 5. Find the order of growth for solutions of the following recurrences. a . T(n)=4T(n/2)+n,T(1)=1 b. T(n)=4T(n/2)+n2,T(1)=1 c. T(n)=4T(n/2)+n3,T(1)=1
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal...
Exercise 1: (Divide and Conquer: Binary tree traversal) Write pseudocode for one of the classic traversal algorithms (preorder, inorder, and postorder) for binary trees. Assuming that your algorithm is recursive, find the number of recursive calls made.
Write a program using multiple functions. Make use of an array to store data Make use...
Write a program using multiple functions. Make use of an array to store data Make use of searching techniques Read data from a file Write data to a file Instructions: In this lab, you will be examining a set of stock collected over a twenty four day period. Be sure to make use of an array to store these stocks. You will be required to read in the data points from a file. Write a function to read in the...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads. ParallelMergeSort.c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define SIZE 100 int array[SIZE]; void fillArrayWithRandomNumbers(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) array[i] = rand()%100; } void printArray(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]); printf("\n"); } typedef struct...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal...
Using the template given in ParallelMergeSort.c write the functions to divide the original array into equal fractions given the number of threads and perform Parallel MergeSort pThreads. Your algorithm should work for 2 threads. ParallelMergeSort.c #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <time.h> #include <unistd.h> #define SIZE 100 int array[SIZE]; void fillArrayWithRandomNumbers(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) array[i] = rand()%100; } void printArray(int arr[SIZE]) { for(int i = 0; i<SIZE; i++) printf("%5d", arr[i]); printf("\n"); } typedef struct...
) Use functions and arrays to complete the following programs. Requirements: • You should use the...
) 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,...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT