Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

add a function named averageCandy to the program. The averageCandy function shou

ID: 3624464 • Letter: A

Question

add a function named averageCandy to the program. The averageCandy function should use one of the other functions that already exists in the program.

#include
using namespace std;

double totalCandy(int kid1Loot, int kid2Loot, int kid3Loot)
{
return kid1Loot + kid2Loot + kid3Loot;
}


int getCandyCount(int kidCount)
{
int candyCount=0;
cout<<"How many pieces of candy did kid#"< cin>>candyCount;
return candyCount;
}


int main()
{
int candyCount1 = getCandyCount(1); // number of pieces of candy for kid #1
int candyCount2 = getCandyCount(2); // number of pieces of candy for kid #3
int candyCount3 = getCandyCount(3); // number of pieces of candy for kid #3

double average = averageCandy(candyCount1, candyCount2, candyCount3);
cout<<"The average number of pieces of candy is "< return 0;
}

Explanation / Answer

For the averageCandy function we need to accomplish 2 things: add all of the inputs together then divide by the number of inputs and return the value. We are also supposed to use an existing function. The totalCandy function is going to add all of the candy together so we can use it and then divide the result of that by 3. We need to set up the averageCandy function to return a double and take 3 int inputs first: double averageCandy(int candy1, int candy2, int candy3) { ... } Next we want to make a temporary variable to store the total amount of candy we get from totalCandy: double total = totalCandy(candy1, candy2, candy3); Finally we are going to calculate and return the average amount of candy: return (total / 3);