SHARE
TWEET

Untitled

a guest Jun 19th, 2017 48 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Recall Assignment 4:
  2.  
  3. Recall Assignment 1:
  4.  
  5. Write a C++ program that takes in five integers from user input. After reading in the
  6. numbers, you should prompt the user for one of five options:
  7.  
  8. 1) Find the smallest integer
  9. 2) Find the largest integer
  10. 3) Find the average of the integers
  11. 4) Find the sum of all numbers
  12. 5) Find the product of all five numbers.
  13.  
  14. After taking in the user’s choice, your program should perform the corresponding option,
  15. print out the result and terminate.
  16.  
  17. Modify Assignment 1 as follows:
  18.  
  19. 1. Your program should utilize a loop that allows the user to enter options 3 times, and
  20. print out the results of those options.
  21. 2. You will need to read in a variable amount of numbers for the each of the five options.
  22. Continue to take input from the user until they enter a 999 for each of these.
  23. 3. Your average option needs to be able to display a decimal result.
  24. 4. You will need to incorporate for loops and switch statements in this assignment.
  25. Your assignment is to write three functions – menu, findResult, printResult.
  26.  
  27. The menu function doesn’t take in any arguments. It prints out to the screen the five
  28. options that the user can make: smallest, largest, average, sum, and product (just as with
  29. Assignment 4). It takes input from the user, indicating his/her choice. This choice is
  30. returned.
  31.  
  32. The findResult function doesn’t return anything. It takes a single argument, an integer,
  33. which represents the user’s choice of calculation to perform. The function takes in input
  34. until the user enters 999 and calculates the result. The option selected, the number of
  35. inputs, and the result is sent on to the printResult function.
  36.  
  37. The printResult function doesn’t return anything, and it takes three arguments – an
  38. integer representing the user’s choice, an integer representing the number of inputs, and a
  39. double, representing the result of the calculations/comparisons. The printResult function
  40. prints the results of the user’s choice and however many inputs.
  41.  
  42. You are supplied with a main and the prototypes for this assignment. Your job is to write
  43. the three functions that will allow the program to work. Use the sample output as a guide
  44. for the expected output for this assignment.
  45.  
  46. For expected output, you can refer back to the expected output for Assignment 4.
  47.  
  48. Please select from the following options:
  49. 1) Find the smallest number.
  50. 2) Find the largest number.
  51. 3) Find the average of numbers.
  52. 4) Find the sum of numbers.
  53. 5) Find the product of numbers.
  54. Your choice: 3
  55.  
  56.  
  57. Enter a number (or 999 to quit): 1
  58. Enter a number (or 999 to quit): 2
  59. Enter a number (or 999 to quit): 3
  60. Enter a number (or 999 to quit): 4
  61. Enter a number (or 999 to quit): 5
  62. Enter a number (or 999 to quit): 6
  63. Enter a number (or 999 to quit): 999
  64. The average of the 6 numbers is: 3.5
  65.  
  66.  
  67. Please select from the following options:
  68. 1) Find the smallest number.
  69. 2) Find the largest number.
  70. 3) Find the average of numbers.
  71. 4) Find the sum of numbers.
  72.  
  73.  
  74. 5) Find the product of numbers.
  75. Your choice: 4
  76.  
  77.  
  78. Enter a number (or 999 to quit): 15
  79. Enter a number (or 999 to quit): 16
  80. Enter a number (or 999 to quit): 17
  81. Enter a number (or 999 to quit): 999
  82. The sum of the 3 numbers is: 48
  83.  
  84.  
  85. Please select from the following options:
  86. 1) Find the smallest number.
  87. 2) Find the largest number.
  88. 3) Find the average of numbers.
  89. 4) Find the sum of numbers.
  90. 5) Find the product of numbers.
  91. Your choice: 5
  92.  
  93.  
  94. Enter a number (or 999 to quit): 6
  95. Enter a number (or 999 to quit): 7
  96. Enter a number (or 999 to quit): 21
  97. Enter a number (or 999 to quit): 1
  98. Enter a number (or 999 to quit): 999
  99. The product of the 4 numbers is: 882
  100.  
  101.  
  102. A second run:
  103.  
  104. Please select from the following options:
  105. 1) Find the smallest number.
  106. 2) Find the largest number.
  107. 3) Find the average of numbers.
  108. 4) Find the sum of numbers.
  109. 5) Find the product of numbers.
  110. Your choice: 1
  111.  
  112.  
  113. Enter a number (or 999 to quit): 12
  114. Enter a number (or 999 to quit): -1
  115. Enter a number (or 999 to quit): -19
  116. Enter a number (or 999 to quit): 25
  117. Enter a number (or 999 to quit): -25
  118. Enter a number (or 999 to quit): -2
  119. Enter a number (or 999 to quit): -6
  120. Enter a number (or 999 to quit): 999
  121. The smallest of the 7 numbers is: -25
  122.  
  123.  
  124. Please select from the following options:
  125. 1) Find the smallest number.
  126. 2) Find the largest number.
  127. 3) Find the average of numbers.
  128. 4) Find the sum of numbers.
  129. 5) Find the product of numbers.
  130. Your choice: 2
  131.  
  132.  
  133. Enter a number (or 999 to quit): -86
  134. Enter a number (or 999 to quit): -16
  135. Enter a number (or 999 to quit): -1
  136. Enter a number (or 999 to quit): -19
  137. Enter a number (or 999 to quit): -2
  138. Enter a number (or 999 to quit): 0
  139. Enter a number (or 999 to quit): 999
  140. The largest of the 6 numbers is: 0
  141.  
  142.  
  143. Please select from the following options:
  144. 1) Find the smallest number.
  145. 2) Find the largest number.
  146. 3) Find the average of numbers.
  147. 4) Find the sum of numbers.
  148. 5) Find the product of numbers.
  149. Your choice: 3
  150.  
  151.  
  152. Enter a number (or 999 to quit): 1
  153. Enter a number (or 999 to quit): 2
  154. Enter a number (or 999 to quit): 3
  155. Enter a number (or 999 to quit): 4
  156. Enter a number (or 999 to quit): 5
  157. Enter a number (or 999 to quit): 6
  158. Enter a number (or 999 to quit): 7
  159. Enter a number (or 999 to quit): 8
  160. Enter a number (or 999 to quit): 9
  161. Enter a number (or 999 to quit): 10
  162. Enter a number (or 999 to quit): 999
  163. The average of the 10 numbers is: 5.5
  164.  
  165.  
  166. The .cpp file is due Tuesday, October 19, 2010, by 9am.
  167.  
  168. Assignment 1  
  169.  
  170. //Your information here
  171. //Solution - Assignment 1
  172. #include<iostream>
  173.    using namespace std;
  174.  
  175.     int main(){
  176.    //variable declarations
  177.       int number1, number2, number3, number4, number5; //five integers from user
  178.       double result; //result of a calculation/comparison
  179.       int choice; //variable receiving the user's menu choice.
  180.    
  181.    //prompting for user input:
  182.       cout<<"Enter your five numbers"<<endl;
  183.    //input:
  184.       cin>>number1>>number2>>number3>>number4>>number5;
  185.    /*Also acceptable:
  186.    cin>>number1;
  187.    cin>>number2;
  188.    cin>>number3;
  189.    cin>>number4;
  190.    cin>>number5;
  191.    */
  192.    
  193.    //print out the menu:
  194.       cout<<"Please select from the following options:"<<endl;
  195.       cout<<"1) Find the smallest number."<<endl;
  196.       cout<<"2) Find the largest number."<<endl;
  197.       cout<<"3) Find the average of the five numbers."<<endl;
  198.       cout<<"4) Find the sum of the five numbers."<<endl;
  199.       cout<<"5) Find the product of the five numbers."<<endl;
  200.       cout<<"Your choice: ";
  201.    //get the user's choice:
  202.       cin>>choice;
  203.       result = number1; //necessary for comparisons; options 3-5, it will be overwritten
  204.       if (choice == 1){//smallest
  205.          if (number2<result){//compare between number1 and number2
  206.             result = number2;
  207.          }
  208.          if (number3<result){//compare between smallest and number3
  209.             result = number3;
  210.          }
  211.          if (number4<result){//compare between smallest and number4
  212.             result = number4;
  213.          }
  214.          if (number5<result){//compare between smallest and number5
  215.             result = number5;
  216.          }
  217.          cout<<"The smallest number is: "<<result<<endl;
  218.       }
  219.       if (choice == 2){//largest
  220.          if (number2>result){//compare between number1 and number2
  221.             result = number2;
  222.          }
  223.          if (number3>result){//compare between largest and number3
  224.             result = number3;
  225.          }
  226.          if (number4>result){//compare between largest and number4
  227.             result = number4;
  228.          }
  229.          if (number5>result){//compare between largest and number5
  230.             result = number5;
  231.          }
  232.          cout<<"The largest number is: "<<result<<endl;
  233.       }
  234.       if (choice == 3){//average
  235.          result = (number1+number2+number3+number4+number5)/5.0;
  236.          cout<<"The average of the five numbers is: "<<result<<endl;
  237.       }
  238.       if (choice == 4){//sum
  239.          result = number1+number2+number3+number4+number5;
  240.          cout<<"The sum of the five numbers is: "<<result<<endl;
  241.       }
  242.       if (choice == 5){//product
  243.          result = number1*number2*number3*number4*number5;
  244.          cout<<"The product of the five numbers is: "<<result<<endl;
  245.       }
  246.       system("PAUSE");
  247.       return 0;
  248.    }
  249.    
  250.    
  251. Assignment  4
  252.  
  253. //Your information here
  254. //Solution - Assignment 1
  255. #include<iostream>
  256. using namespace std;
  257.  
  258.     int main(){
  259.       //variable declarations
  260.       int number;
  261.       double result; //result of a calculation/comparison
  262.       int choice; //variable receiving the user's menu choice.
  263.       int input, loopcounter = 0;
  264.    
  265.      
  266.       //print out the menu:
  267.       for (int counter = 0; counter<3; counter++){
  268.          cout<<"Please select from the following options:"<<endl;
  269.          cout<<"1) Find the smallest number."<<endl;
  270.          cout<<"2) Find the largest number."<<endl;
  271.          cout<<"3) Find the average of numbers."<<endl;
  272.          cout<<"4) Find the sum of numbers."<<endl;
  273.          cout<<"5) Find the product of numbers."<<endl;
  274.          cout<<"Your choice: ";
  275.          //get the user's choice:
  276.          cin>>choice;
  277.          switch(choice){
  278.             case 1: //smallest
  279.                loopcounter = 0;
  280.                cout<<"Enter a number (or 999 to quit): ";
  281.                cin>>number;
  282.                while(number!=999){
  283.                   if (loopcounter == 0) result = number;
  284.                   else if (number<result) result = number;
  285.                   loopcounter++;
  286.                   cout<<"Enter a number (or 999 to quit): ";
  287.                   cin>>number;
  288.                }
  289.                cout<<"The smallest of the "<<loopcounter<<" numbers is: "<<result<<endl;
  290.            
  291.                break;
  292.             case 2: //largest
  293.                loopcounter = 0;
  294.                cout<<"Enter a number (or 999 to quit): ";
  295.                cin>>number;
  296.                while(number!=999){
  297.                   if (loopcounter == 0) result = number;
  298.                   else if (number>result) result = number;
  299.                   loopcounter++;
  300.                   cout<<"Enter a number (or 999 to quit): ";
  301.                   cin>>number;
  302.                }              
  303.                cout<<"The largest of the "<<loopcounter<<" numbers is: "<<result<<endl;
  304.                break;
  305.             //for cases 3, 4, and 5, this is similar to the sentinel-controlled example - p. 157-158
  306.             case 3: //average
  307.                result = 0; //running total, as with option 4
  308.                loopcounter = 0;
  309.                cout<<"Enter a number (or 999 to quit): ";
  310.                cin>>input;
  311.                while(input != 999){
  312.                   result += input;
  313.                   loopcounter++;
  314.                   cout<<"Enter a number (or 999 to quit): ";
  315.                   cin>>input;
  316.                }
  317.                result /= loopcounter;
  318.                cout<<"The average of the "<<loopcounter<<" numbers is: "<<result<<endl;
  319.                break;
  320.             case 4: //sum
  321.                result = 0;
  322.                loopcounter = 0;
  323.                cout<<"Enter a number (or 999 to quit): ";
  324.                cin>>input;
  325.                while(input != 999){
  326.                   result += input;
  327.                   loopcounter++;
  328.                   cout<<"Enter a number (or 999 to quit): ";
  329.                   cin>>input;
  330.                }
  331.                cout<<"The sum of the " <<loopcounter<<" numbers is: "<<result<<endl;
  332.                break;
  333.             case 5://product - instead of adding, as in 3 and 4, keep a running "multiplication"
  334.                result = 1; //identity idea in math - anything * 1 gives me that value; multiply by zero, get 0...
  335.                loopcounter = 0;
  336.                cout<<"Enter a number (or 999 to quit): ";
  337.                cin>>input;
  338.                while(input != 999){
  339.                   result *= input;
  340.                   loopcounter++;
  341.                   cout<<"Enter a number (or 999 to quit): ";
  342.                   cin>>input;
  343.                }
  344.                cout<<"The product of the " <<loopcounter<<" numbers is: "<<result<<endl;
  345.                break;
  346.                
  347.          }
  348.       }
  349.       system("PAUSE");
  350.       return 0;
  351.    }
RAW Paste Data
Top