Question

In: Computer Science

Even Odd Average (C++ LANGUAGE) Write the following functions: Function #1 Write the function Print. The...

Even Odd Average (C++ LANGUAGE)

Write the following functions:

Function #1

Write the function Print. The function will have one int 1D array n and one int size as parameters. The function will display all the values of n on one line.

Function #2

Write the function AverageEvenOdd. The function will have one int 1D array n and one int size as parameters. The size of the array is given by the parameter int size. Therefore, the function should work on the array of any size. The function will display the sum of all even numbers and odd numbers in n and return the average of all even values and odd numbs in n using two reference parameters.
int main()
{
    int a[7] = {1, 3, 2, 9, 6, 4, 2};
    double aE, aO;
 
    cout<<"a = ";
    Print(a, 7);
    AverageEvenOdd(a, 7, aE, aO);
    cout<<"Average Even: "<<aE<<endl;
    cout<<"Average Odd: "<<aO<<endl<<endl;
 
    return 0;
}

A=1 3 2 9 6 4 2

SUM EVEN=14

SUM ODD=13

AVERAGE EVEN =3.5

AVERAGE ODD=4.33333

 

Solutions

Expert Solution

Code:

Code as text:

#include <iostream>

using namespace std;

// display values of array on one line

void Print(int n[], int size) {

for (int i = 0; i < size; i++) {

cout << n[i] << " ";

}

cout << endl;

}

// calculate average of even and odd numbers

void AverageEvenOdd(int n[], int size, double &aE, double &aO) {

double sE = 0, sO = 0; // variable to store sum of even and odd numbers

int cE = 0, cO = 0; // variable to store count of even and odd numbers

for (int i = 0; i < size; i++) {

if (n[i] % 2 == 0){

sE += n[i];

cE++;

}

else {

sO += n[i];

cO++;

}

}

aE = sE / cE;

aO = sO / cO;

}

int main() {

int a[7] = {1, 3, 2, 9, 6, 4, 2};

double aE, aO;

cout << "A=";

Print(a, 7);

AverageEvenOdd(a, 7, aE, aO);

cout << "Average Even: " << aE << endl;

cout << "Average Odd: " << aO << endl << endl;

return 0;

}

Sample run:

P.s. Ask any doubts in comments and don't forget to rate the answer.


Related Solutions

Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number....
Write a function name as 'checkEvenOrOdd' that checks the input value is odd or even number. The main function is given: int main(){     int number;     cout << "Check number input: ";     cin >> number;     cout << "The input number " << number << " is " << checkEvenOrOdd(number) << endl;     return 0; } simple c++ code please
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an...
ONLY IN C LANGUAGE Write a C program to print all the unique elements of an array. Print all unique elements of an array Enter the number of elements to be stored in the array: 4 Input 4 elements in the arrangement: element [0]: 3 element [1]: 2 element [2]: 2 element [3]: 5 Expected output: The only items found in the array are: 3 5
A function is odd function if f (-x) = - f(x). A function is even function...
A function is odd function if f (-x) = - f(x). A function is even function if f(-x) = f(x). f(x) = sin (x) and f(x) = x are examples of odd functions and f(x) = cos x and f(x) = e^ (-x)^2 are examples of even functions. Give two more examples of even functions and two more examples of odd functions. Show that for odd functions f (x), integral of f(x) from negative infinity to infinity = 0 if...
determine whether the given function is even, odd, or neither. Please write a code in MatLab...
determine whether the given function is even, odd, or neither. Please write a code in MatLab to solve this problem below: 1.f(x) = sin 3x please only use Matlab to solve this problem
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters 2 ) ask the user for 2 numbers (should be in main) 3 ) Calculate the sum and product of the numbers using reference parameters (should be in getAnswer function) 4 ) Print the sum and product of the numbers in the following format: (should be in printmessage function) Here is the function you need        Function name | Parameters                       | Return Type    heading |...
Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT