from the file Sum.cpp file. Modify this program by adding a function called sum
ID: 3641807 • Letter: F
Question
from the file Sum.cpp file. Modify this program by adding a function called sum that calculates the sum of four variables and then returns the value of their sum. The main function should call the sum function (passing the four needed arguments by sum) and should save the result in the total variable. The main function should also print the value of total on the screenthis is the sum.cpp file from
#include <iostream>
using namespace std;
// Define the Function prototype here
int main()
{
int value1 = 20, // The first value
value2 = 40, // The second value
value3 = 70, // The third value
value4 = 15, // The fourth value
total; // To hold the total
// Call the sum function, passing the contents of
// the four values as arguments. Assign the return
// value to the total variable.
// Display the sum of the values.
return 0;
}
//*****************************************************
// Definition of function sum. This function returns *
// the sum of its two parameters. *
//*****************************************************
Explanation / Answer
//See below for working program written as simply as possible:
#include <iostream>
using namespace std;
// Define the Function prototype here
int sum (int a, int b, int c, int d);
int main()
{
int value1 = 20, // The first value
value2 = 40, // The second value
value3 = 70, // The third value
value4 = 15, // The fourth value
total = 0; // To hold the total
// Call the sum function, passing the contents of
// the four values as arguments. Assign the return
// value to the total variable.
total = sum(value1, value2, value3, value4);
// Display the sum of the values.
cout << "The sum is: " << total << " ";
return 0;
}
//*****************************************************
// Definition of function sum. This function returns *
// the sum of its two parameters. *
//*************************************************
int sum (int a, int b, int c, int d)
{
int ans = 0;
ans = a + b + c + d;
return ans;
}
lt;iostream
#include
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.