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 a program to print random numbers and whether they were even or odd. Ask the...
Write a program to print random numbers and whether they were even or odd. Ask the user to enter a positive number to indicate how many random numbers to pull (verify number is positive). Zero is positive. Declare two variables and initialized to zero. int evens = 0, odds = 0; Use them to count how many evens and how many odds were pulled. Enter how many random numbers to pull : 3 41           odd 18467    odd 6334    even 1...
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
Write a Python function which finds the smallest even and odd numbers in a given list....
Write a Python function which finds the smallest even and odd numbers in a given list. (Use for loop and if conditions for 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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT