Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Joel Bares
- CIS 22A Winter 2017
- Assignment I
- Problem I2
- This program receives 4 numbers
- from the user and calculates
- the sum and then displays it.
- The functions make use of arrays
- */
- #include <iostream>
- using namespace std;
- // ***************************
- // * RECEIVES DATA FROM USER *
- // ***************************
- void getData(int i[], int);
- // ***************************
- // * COMPUTES SUM FOR USER *
- // ***************************
- int computeTotal(int i[], int);
- // ***************************
- // * DISPLAYS SUM FOR USER *
- // **************************
- void printAll(int i[], int, int);
- int main()
- {
- const int ARRAY_SIZE = 4;
- int i[ARRAY_SIZE];
- int sum;
- getData(i, ARRAY_SIZE);
- sum = computeTotal(i, ARRAY_SIZE);
- printAll(i, ARRAY_SIZE, sum);
- return 0;
- }
- // ************************************
- // * FOR LOOP USES A COUNT TO ASK USER*
- // * FOR A NUMBER THEN INPUTS IT INTO *
- // * THE ARRAY AND SENDS IT BACK TO *
- // * MAIN *
- // ***********************************
- void getData(int i[], int size)
- {
- for(int count = 0; count < size; count++)
- {
- cout << "Please enter a number" << endl;
- cin >> i[count];
- }
- }
- // **************************************
- // * FOR LOOP GOES THROUGH EACH ELEMENT *
- // * OF THE ARRAY AND CALCULATES THE *
- // * SUM FOR THE USER *
- // **************************************
- int computeTotal(int i[], int size)
- {
- int total = 0;
- for(int count = 0; count < size; count++)
- {
- total += i[count];
- }
- return total;
- }
- // ****************************************
- // * FOR LOOP GOES THROUGH EACH ELEMENT *
- // * OF THE ARRAY AND PRINTS THEM IN THE *
- // * FORM REQUESTED BY THE TEACHER FOR *
- // * THE USER *
- // ****************************************
- void printAll(int i[], int size, int sum)
- {
- for(int count = 0; count < size; count++)
- {
- cout << i[count];
- if(count <= 2)
- {
- cout << " + ";
- }
- if(count == 3)
- {
- cout << " = " << sum << endl;
- }
- }
- }
- /* EXECUTION RESULTS:
- Please enter a number
- 4
- Please enter a number
- 6
- Please enter a number
- 9
- Please enter a number
- 12
- 4 + 6 + 9 + 12 = 31
- Program ended with exit code: 0
- Please enter a number
- 1
- Please enter a number
- 7
- Please enter a number
- 9
- Please enter a number
- 15
- 1 + 7 + 9 + 15 = 32
- Program ended with exit code: 0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement