Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Recall Assignment 4:
- Recall Assignment 1:
- Write a C++ program that takes in five integers from user input. After reading in the
- numbers, you should prompt the user for one of five options:
- 1) Find the smallest integer
- 2) Find the largest integer
- 3) Find the average of the integers
- 4) Find the sum of all numbers
- 5) Find the product of all five numbers.
- After taking in the user’s choice, your program should perform the corresponding option,
- print out the result and terminate.
- Modify Assignment 1 as follows:
- 1. Your program should utilize a loop that allows the user to enter options 3 times, and
- print out the results of those options.
- 2. You will need to read in a variable amount of numbers for the each of the five options.
- Continue to take input from the user until they enter a 999 for each of these.
- 3. Your average option needs to be able to display a decimal result.
- 4. You will need to incorporate for loops and switch statements in this assignment.
- Your assignment is to write three functions – menu, findResult, printResult.
- The menu function doesn’t take in any arguments. It prints out to the screen the five
- options that the user can make: smallest, largest, average, sum, and product (just as with
- Assignment 4). It takes input from the user, indicating his/her choice. This choice is
- returned.
- The findResult function doesn’t return anything. It takes a single argument, an integer,
- which represents the user’s choice of calculation to perform. The function takes in input
- until the user enters 999 and calculates the result. The option selected, the number of
- inputs, and the result is sent on to the printResult function.
- The printResult function doesn’t return anything, and it takes three arguments – an
- integer representing the user’s choice, an integer representing the number of inputs, and a
- double, representing the result of the calculations/comparisons. The printResult function
- prints the results of the user’s choice and however many inputs.
- You are supplied with a main and the prototypes for this assignment. Your job is to write
- the three functions that will allow the program to work. Use the sample output as a guide
- for the expected output for this assignment.
- For expected output, you can refer back to the expected output for Assignment 4.
- Please select from the following options:
- 1) Find the smallest number.
- 2) Find the largest number.
- 3) Find the average of numbers.
- 4) Find the sum of numbers.
- 5) Find the product of numbers.
- Your choice: 3
- Enter a number (or 999 to quit): 1
- Enter a number (or 999 to quit): 2
- Enter a number (or 999 to quit): 3
- Enter a number (or 999 to quit): 4
- Enter a number (or 999 to quit): 5
- Enter a number (or 999 to quit): 6
- Enter a number (or 999 to quit): 999
- The average of the 6 numbers is: 3.5
- Please select from the following options:
- 1) Find the smallest number.
- 2) Find the largest number.
- 3) Find the average of numbers.
- 4) Find the sum of numbers.
- 5) Find the product of numbers.
- Your choice: 4
- Enter a number (or 999 to quit): 15
- Enter a number (or 999 to quit): 16
- Enter a number (or 999 to quit): 17
- Enter a number (or 999 to quit): 999
- The sum of the 3 numbers is: 48
- Please select from the following options:
- 1) Find the smallest number.
- 2) Find the largest number.
- 3) Find the average of numbers.
- 4) Find the sum of numbers.
- 5) Find the product of numbers.
- Your choice: 5
- Enter a number (or 999 to quit): 6
- Enter a number (or 999 to quit): 7
- Enter a number (or 999 to quit): 21
- Enter a number (or 999 to quit): 1
- Enter a number (or 999 to quit): 999
- The product of the 4 numbers is: 882
- A second run:
- Please select from the following options:
- 1) Find the smallest number.
- 2) Find the largest number.
- 3) Find the average of numbers.
- 4) Find the sum of numbers.
- 5) Find the product of numbers.
- Your choice: 1
- Enter a number (or 999 to quit): 12
- Enter a number (or 999 to quit): -1
- Enter a number (or 999 to quit): -19
- Enter a number (or 999 to quit): 25
- Enter a number (or 999 to quit): -25
- Enter a number (or 999 to quit): -2
- Enter a number (or 999 to quit): -6
- Enter a number (or 999 to quit): 999
- The smallest of the 7 numbers is: -25
- Please select from the following options:
- 1) Find the smallest number.
- 2) Find the largest number.
- 3) Find the average of numbers.
- 4) Find the sum of numbers.
- 5) Find the product of numbers.
- Your choice: 2
- Enter a number (or 999 to quit): -86
- Enter a number (or 999 to quit): -16
- Enter a number (or 999 to quit): -1
- Enter a number (or 999 to quit): -19
- Enter a number (or 999 to quit): -2
- Enter a number (or 999 to quit): 0
- Enter a number (or 999 to quit): 999
- The largest of the 6 numbers is: 0
- Please select from the following options:
- 1) Find the smallest number.
- 2) Find the largest number.
- 3) Find the average of numbers.
- 4) Find the sum of numbers.
- 5) Find the product of numbers.
- Your choice: 3
- Enter a number (or 999 to quit): 1
- Enter a number (or 999 to quit): 2
- Enter a number (or 999 to quit): 3
- Enter a number (or 999 to quit): 4
- Enter a number (or 999 to quit): 5
- Enter a number (or 999 to quit): 6
- Enter a number (or 999 to quit): 7
- Enter a number (or 999 to quit): 8
- Enter a number (or 999 to quit): 9
- Enter a number (or 999 to quit): 10
- Enter a number (or 999 to quit): 999
- The average of the 10 numbers is: 5.5
- The .cpp file is due Tuesday, October 19, 2010, by 9am.
- Assignment 1
- //Your information here
- //Solution - Assignment 1
- #include<iostream>
- using namespace std;
- int main(){
- //variable declarations
- int number1, number2, number3, number4, number5; //five integers from user
- double result; //result of a calculation/comparison
- int choice; //variable receiving the user's menu choice.
- //prompting for user input:
- cout<<"Enter your five numbers"<<endl;
- //input:
- cin>>number1>>number2>>number3>>number4>>number5;
- /*Also acceptable:
- cin>>number1;
- cin>>number2;
- cin>>number3;
- cin>>number4;
- cin>>number5;
- */
- //print out the menu:
- cout<<"Please select from the following options:"<<endl;
- cout<<"1) Find the smallest number."<<endl;
- cout<<"2) Find the largest number."<<endl;
- cout<<"3) Find the average of the five numbers."<<endl;
- cout<<"4) Find the sum of the five numbers."<<endl;
- cout<<"5) Find the product of the five numbers."<<endl;
- cout<<"Your choice: ";
- //get the user's choice:
- cin>>choice;
- result = number1; //necessary for comparisons; options 3-5, it will be overwritten
- if (choice == 1){//smallest
- if (number2<result){//compare between number1 and number2
- result = number2;
- }
- if (number3<result){//compare between smallest and number3
- result = number3;
- }
- if (number4<result){//compare between smallest and number4
- result = number4;
- }
- if (number5<result){//compare between smallest and number5
- result = number5;
- }
- cout<<"The smallest number is: "<<result<<endl;
- }
- if (choice == 2){//largest
- if (number2>result){//compare between number1 and number2
- result = number2;
- }
- if (number3>result){//compare between largest and number3
- result = number3;
- }
- if (number4>result){//compare between largest and number4
- result = number4;
- }
- if (number5>result){//compare between largest and number5
- result = number5;
- }
- cout<<"The largest number is: "<<result<<endl;
- }
- if (choice == 3){//average
- result = (number1+number2+number3+number4+number5)/5.0;
- cout<<"The average of the five numbers is: "<<result<<endl;
- }
- if (choice == 4){//sum
- result = number1+number2+number3+number4+number5;
- cout<<"The sum of the five numbers is: "<<result<<endl;
- }
- if (choice == 5){//product
- result = number1*number2*number3*number4*number5;
- cout<<"The product of the five numbers is: "<<result<<endl;
- }
- system("PAUSE");
- return 0;
- }
- Assignment 4
- //Your information here
- //Solution - Assignment 1
- #include<iostream>
- using namespace std;
- int main(){
- //variable declarations
- int number;
- double result; //result of a calculation/comparison
- int choice; //variable receiving the user's menu choice.
- int input, loopcounter = 0;
- //print out the menu:
- for (int counter = 0; counter<3; counter++){
- cout<<"Please select from the following options:"<<endl;
- cout<<"1) Find the smallest number."<<endl;
- cout<<"2) Find the largest number."<<endl;
- cout<<"3) Find the average of numbers."<<endl;
- cout<<"4) Find the sum of numbers."<<endl;
- cout<<"5) Find the product of numbers."<<endl;
- cout<<"Your choice: ";
- //get the user's choice:
- cin>>choice;
- switch(choice){
- case 1: //smallest
- loopcounter = 0;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>number;
- while(number!=999){
- if (loopcounter == 0) result = number;
- else if (number<result) result = number;
- loopcounter++;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>number;
- }
- cout<<"The smallest of the "<<loopcounter<<" numbers is: "<<result<<endl;
- break;
- case 2: //largest
- loopcounter = 0;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>number;
- while(number!=999){
- if (loopcounter == 0) result = number;
- else if (number>result) result = number;
- loopcounter++;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>number;
- }
- cout<<"The largest of the "<<loopcounter<<" numbers is: "<<result<<endl;
- break;
- //for cases 3, 4, and 5, this is similar to the sentinel-controlled example - p. 157-158
- case 3: //average
- result = 0; //running total, as with option 4
- loopcounter = 0;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>input;
- while(input != 999){
- result += input;
- loopcounter++;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>input;
- }
- result /= loopcounter;
- cout<<"The average of the "<<loopcounter<<" numbers is: "<<result<<endl;
- break;
- case 4: //sum
- result = 0;
- loopcounter = 0;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>input;
- while(input != 999){
- result += input;
- loopcounter++;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>input;
- }
- cout<<"The sum of the " <<loopcounter<<" numbers is: "<<result<<endl;
- break;
- case 5://product - instead of adding, as in 3 and 4, keep a running "multiplication"
- result = 1; //identity idea in math - anything * 1 gives me that value; multiply by zero, get 0...
- loopcounter = 0;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>input;
- while(input != 999){
- result *= input;
- loopcounter++;
- cout<<"Enter a number (or 999 to quit): ";
- cin>>input;
- }
- cout<<"The product of the " <<loopcounter<<" numbers is: "<<result<<endl;
- break;
- }
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement