Question

In: Computer Science

In C++, write a function that returns the average of all the values stored in an...

In C++, write a function that returns the average of all the values stored in an integer array (called arr) of size n. The return type of the function should be double.

Test this function in the main program.

Solutions

Expert Solution

Steps to write a function that return average of int array and call from the main and print returned value .

Approach is

1, Make a declare sum variable and initialized to 0

2. Use a loop to access each element and add it to sum

3. Return sum divide by n

4. Call it from main

C++ code is

#include <iostream>
using namespace std;

double avgArray ( int arr[],int n)
{
double sum=0;

// Loop to access each element and add it to sum
for (int i=0;i<n;i++)
{
sum=sum+arr[i];
}
// Divide by n
return sum/n;
}

int main()
{

// Declare The array
int arr[]={ 2,4,16,3,10,21,13};
// call function by passing arr and size of array
double avg=avgArray(arr,7);
// print the average
cout<<"The Average of The Array : "<<avg;
}

Code Screen Shot

Output

This is how we can write the average of integer of array and print the result in main .

Thank You

If u like the answer then Upvote it and have any doubt ask in comments


Related Solutions

C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a...
C++ Suppose  getAcctNum() returns the account number of a given Account variable.  Write a function that returns a boolean value to find if a given account number appears in an array of Account objects.
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns...
In C++ Write a function called findBestSimScore that takes a genome and a sequence and returns the highest similarity score found in the genome as a double. Note: the term genome refers to the string that represents the complete set of genes in an organism, and sequence to refer to some substring or sub-sequence in the genome. Your function MUST be named findBestSimScore Your function should take two parameters in this order: a string parameter for the genome (complete set...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all...
Write the function letter_frequencies(text) that returns a dictionary containing all of the letter frequencies of all letters occurring in the parameter text. For example: if __name__ == '__main__': d = letter_frequencies('hello, world') print(d) # show the contents of this dictionary will produce the following output: {'a': 0.0, 'b': 0.0, 'c': 0.0, 'd': 0.1, 'e': 0.1, 'f': 0.0, 'g': 0.0, 'h': 0.1, 'i': 0.0, 'j': 0.0, 'k': 0.0, 'l': 0.3, 'm': 0.0, 'n': 0.0, 'o': 0.2, 'p': 0.0, 'q': 0.0,'r': 0.1,...
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.
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
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...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT