Question

In: Computer Science

) 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, 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

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:


Related Solutions

Objectives In this lab you will review passing arrays to methods and partially filled arrays. Requirements...
Objectives In this lab you will review passing arrays to methods and partially filled arrays. Requirements 1. Fill an array with data from an input file sampledata-io.txt (Attached) a. Assume no more than 100 data items, terminated by -1 for sentinel b. Note that the sample data file has other information after the sentinel; it should cause no problem c. Read and store data d. Print the number of data stored in the array e. Add a method to reverse...
3.44444444 The following are requirements of ALL programs that you write from this point on! You...
3.44444444 The following are requirements of ALL programs that you write from this point on! You must write instructions to the screen to tell the user exactly what you want him/her to do. Your instructions must be very clear and descriptive, and should not contain any spelling or grammar errors. You MUST choose variable names that describe the contents of the variable. Unless I specifically tell you to do so, single character variable names will not be acceptable unless you...
• 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...
Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the...
Skills needed to complete this assignment: functions, dynamic arrays. The mathematician John Horton Conway invented the "Game of Life". Though not a "game" in any traditional sense, it provides interesting behavior that is specified with only a few rules. This project asks you to write a program that allows you to specify an initial configuration. The program follows the rules of LIFE to show the continuing behavior of the configuration. LIFE is an organism that lives in a discrete, two-dimensional...
PHP Question: Subject: Functions and Arrays. INSTRUCTIONS: Objective: • Write functions. • Use server-side includes. •...
PHP Question: Subject: Functions and Arrays. INSTRUCTIONS: Objective: • Write functions. • Use server-side includes. • Create and utilize a numeric array. • Create and utilize an associative array. Requirements: Create a script file called functions.php, where you will be adding functions. priceCalc() function: • 2 parameters: price and quantity. • Create a numeric array of discounts with the following values: 0,0,.05,.1,.2,.25. • Get the discount percent from the array using the quantity as the index. If the quantity is...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start...
In C# A Tic Tac Toe program that uses functions and arrays. The program should start by asking the player one for their name and then player two for their name. Then should print the board and ask the user what column and then what row they would like to put their x (or o, depending on the player) . Use the clear function to make it look as though the board is never repeating.. Thanks!
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions below main. 1. Write a function harmonicMeans that repeatedly asks the user for two int values until at least one of them is 0. For each pair, the function should calculate and display the harmonic mean of the numbers. The harmonic mean of the numbers is the inverse of the average of the inverses. The harmonic mean of x and y can be calculated...
MATLAB NEEDED. This lab will use functions and arrays.   The task within is to Add, Sub,...
MATLAB NEEDED. This lab will use functions and arrays.   The task within is to Add, Sub, or Multiply 2 Matrix. See Manipulate Matrix doc for the methods. My reference to matrix below is because we are doing matrix math. The matrix are arrays. Matlab built-in functions not allowed You will write 3 functions (call those functions): 1. ADD two matrix,   2. SUBTRACT two matrix, and 3. MULTIPLY two matrix. Requirements: Write the script to cycle until user chooses to stop....
C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs...
C programming 4. Exploring Pointers and Use of Arrays [15 pts] In a shell environment, programs are often executed using command lines arguments. For example: g++ -o cm03_la01 cm03_la01.c. In C such program can be develop using a standard for which the main program has two parameters as follows: int main (int argc, char * argv[ ]) { /*program here */ } 3 Because of the equivalence between pointers and arrays, the above C code can also be written as...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT