In the main function, define an array that can contain four int values. Also define an int named sum.
Write a function named getData which asks the user for the data and puts it in the array.
Write a function named computeTotal which adds the data in the array and returns the sum.
Write a function named printAll which takes the array and the sum as arguments.
In the main function, call the getData function.
Then in the main function, call the computeTotal function.
Then in the main function, call the printAll function.
Print the results in the formats shown in the following format:
4 + 6 + 9 + 12 = 31
Test the program twice with the values:
4, 6, 9, and 12.
then with the values:
1, 7, 9, and 15.
#include <iostream>
using namespace std;
const int N=4;
void getData(int a[N]) {
for (int i=0; i<N; i++) {
cin >> a[i];
}
}
int computeTotal(int a[N]) {
int tot = 0;
for (int i=0; i<N; i++) {
tot += a[i];
}
return tot;
}
void printAll(int a[N], int tot) {
cout << a[0];
for (int i=1; i<N; i++) {
cout << " + " << a[i];
}
cout << " = " << tot << endl;
}
int main() {
int a[N];
int sum;
getData(a);
sum = computeTotal(a);
printAll(a, sum);
return 0;
}
Comments
Leave a comment