Answer to Question #309340 in C++ for min

Question #309340

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.


1
Expert's answer
2022-03-12T04:55:45-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog