Problem description
Write a menu-driven program that allows a user to enter five numbers and then choose between finding the
smallest, largest, sum, or average. Use a switch statement to determine what action to take. Provide an
error message if invalid data is entered. If invalid data is entered, your main function should return the integer
value 1. Otherwise, return the integer value 0.
Sample interaction
$ ./a.out
Enter five numbers, separated with spaces: 18 21 7 54 Z
Sorry. You entered something I don’t understand.
$ echo $?
1
$ ./a.out
Enter five numbers, separated with spaces: 18 21 7 54 9
a. Smallest value
b. Largest value
c. Sum of values
d. Average of values
Enter choice? 1
Sorry. You entered something I don’t understand.
$ echo $?
1
$ ./a.out
Enter five numbers, separated with spaces: 18 21 7 54 9
a. Smallest value
b. Largest value
c. Sum of values
d. Average of values
Enter choice? A
The smallest value is 7.
$ echo $?
0
What I have tried:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int option;
int small(int u, int w, int x, int y, int z);
int large(int u, int w, int x, int y, int z);
int sum(int u, int w, int x, int y, int z);
int avg(int u, int w, int x, int y, int z);
int one;
int two;
int three;
int four;
int five;
int main(void)
{
printf("\n Enter five numbers, separated with spaces:");
scanf ("%d %d %d %d %d", &one, &two, &three, &four, &five);
printf("\n\t a. Smallest value ");
printf("\n\t b. Largest value ");
printf("\n\t c. Sum of values ");
printf("\n\t d. Average of values ");
printf("\n Enter choice? ");
scanf ("%d", &option);
switch(option)
{
case 'A': cout<<"The smallest value is " <<small(one, two, three, four, five);
break;
case 'B': cout<<"The largest value is " <<large(one, two, three, four, five);
break;
case 'C': cout<<"The sum value is " <<sum(one, two, three, four, five);
break;
case 'D': cout<<"The avergae value is " <<avg(one, two, three, four, five);
break;
default : cout<<"\n Sorry. You entered something I don’t understand.";
}
return 0;
}
int small(int u, int w, int x, int y, int z)
{
if (one<two && one<three && one<four && one<five)
printf("%d", one);
else if (two<one && two<three && two<four && two<five)
printf("%d", two);
else if (three<one && three<two && three<four && three<five)
printf("%d", three);
else if (four<one && four<two && four<three && four<five)
printf("%d", four);
else
printf("%d", five);
return 0;
}
int large(int u, int w, int x, int y, int z)
{
if (one>two && one>three && one>four && one>five)
printf("%d", one);
else if (two>one && two>three && two>four && two>five)
printf("%d", two);
else if (three>one && three>two && three>four && three>five)
printf("%d", three);
else if (four>one && four>two && four>three && four>five)
printf("%d", four);
else
printf("%d", five);
return 0;
}
int sum(int u, int w, int x, int y, int z)
{
return(u+w+x+y+z);
}
int avg(int u, int w, int x, int y, int z)
{
return((u+w+x+y+z)/5);
}