Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. /* Joel Bares
  2.  CIS 22A Winter 2017
  3.  Assignment I
  4.  Problem I2
  5.  
  6.  This program receives 4 numbers
  7.  from the user and calculates
  8.  the sum and then displays it.
  9.  The functions make use of arrays
  10. */
  11.  
  12.  
  13. #include <iostream>
  14.  
  15. using namespace std;
  16.  
  17. // ***************************
  18. // * RECEIVES DATA FROM USER *
  19. // ***************************
  20.  
  21. void getData(int i[], int);
  22.  
  23. // ***************************
  24. // * COMPUTES SUM FOR USER   *
  25. // ***************************
  26. int computeTotal(int i[], int);
  27.  
  28. // ***************************
  29. // *  DISPLAYS SUM FOR USER  *
  30. // **************************
  31. void printAll(int i[], int, int);
  32.  
  33.  
  34. int main()
  35. {
  36.     const int ARRAY_SIZE = 4;
  37.     int i[ARRAY_SIZE];
  38.     int sum;
  39.    
  40.     getData(i, ARRAY_SIZE);
  41.     sum = computeTotal(i, ARRAY_SIZE);
  42.     printAll(i, ARRAY_SIZE, sum);
  43.    
  44.              
  45.     return 0;
  46. }
  47.  
  48. // ************************************
  49. // * FOR LOOP USES A COUNT TO ASK USER*
  50. // * FOR A NUMBER THEN INPUTS IT INTO *
  51. // * THE ARRAY AND SENDS IT BACK TO   *
  52. // * MAIN                             *
  53. // ***********************************
  54. void getData(int i[], int size)
  55. {
  56.     for(int count = 0; count < size; count++)
  57.     {
  58.     cout << "Please enter a number" << endl;
  59.     cin >> i[count];
  60.     }
  61.    
  62. }
  63.  
  64. // **************************************
  65. // * FOR LOOP GOES THROUGH EACH ELEMENT *
  66. // * OF THE ARRAY AND CALCULATES THE    *
  67. // * SUM FOR THE USER                   *
  68. // **************************************
  69. int computeTotal(int i[], int size)
  70. {
  71.     int total = 0;
  72.     for(int count = 0; count < size; count++)
  73.     {
  74.         total += i[count];
  75.     }
  76.     return total;
  77. }
  78.  
  79.  
  80. // ****************************************
  81. // * FOR LOOP GOES THROUGH EACH ELEMENT   *
  82. // * OF THE ARRAY AND PRINTS THEM IN THE  *
  83. // * FORM REQUESTED BY THE TEACHER FOR    *
  84. // * THE USER                             *
  85. // ****************************************
  86. void printAll(int i[], int size, int sum)
  87. {
  88.     for(int count = 0; count < size; count++)
  89.     {
  90.         cout << i[count];
  91.         if(count <= 2)
  92.         {
  93.             cout << " + ";
  94.         }
  95.        
  96.         if(count == 3)
  97.         {
  98.             cout << " = " << sum << endl;
  99.         }
  100.     }
  101. }
  102.  
  103. /* EXECUTION RESULTS:
  104.  Please enter a number
  105.  4
  106.  Please enter a number
  107.  6
  108.  Please enter a number
  109.  9
  110.  Please enter a number
  111.  12
  112.  4 + 6 + 9 + 12 = 31
  113.  Program ended with exit code: 0
  114.  
  115.  Please enter a number
  116.  1
  117.  Please enter a number
  118.  7
  119.  Please enter a number
  120.  9
  121.  Please enter a number
  122.  15
  123.  1 + 7 + 9 + 15 = 32
  124.  Program ended with exit code: 0
  125.  
  126.  
  127. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement