Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C++
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
 
//Function Declarations
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)
{
  //Local Declarations
  printf("\n Enter five numbers, separated with spaces:");
  scanf ("%d %d %d %d %d", &one, &two, &three, &four, &five);
 
  //Statements
  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);

  // return 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);
}
Posted
Updated 2-Oct-17 18:58pm
v2
Comments
OriginalGriff 2-Oct-17 5:07am    
And?
WHat does it do that you didn't expect, or not do that you did?
What help do you need?
Patrice T 2-Oct-17 5:09am    
What is the problem?
Do you have a question?

Why aren't you using arrays and iterations?
Why aren't you using C++ features? For instance, you might write:
#include <iostream>
#include <array>
using namespace std;

constexpr size_t NUMBERS = 5;

int main()
{
  array <int, NUMBERS> a;
  cout << "enter five numbers" << endl;

  for (size_t n=0; n<NUMBERS; ++n)
  {
    cin >> a[n];
    if (cin.fail())
    {
      cout << "invalid input" << endl;
      return 1;
    }
  }
  // ...
}

A similar approach might be used in computation functions like average
C++
double average( const array <int , NUMBERS> & a)
{
  double sum = 0;
  for (auto i : a)
    sum += i;

  return (sum / NUMBERS);
}



[update]
This is compiler-error-free version of your code. You should fill the TODO: places appropriately.

C++
#include <iostream>
#include <array>
#include <algorithm>

using namespace std;
constexpr size_t NUMBERS = 5;
typedef array<int, NUMBERS> Array5;

//Function Declarations
char option;
int small(const Array5 & a);
int large(const Array5 & a );
int sum(const Array5 & a);
double avg(const Array5 & a);


int main()
{
  array <int, NUMBERS> a;
  cout << "enter five numbers" << endl;

for (size_t n=0; n<NUMBERS; ++n)
{
  cin >> a[n];
  if (cin.fail())
  {
    cout << "invalid input" << endl;
    return 1;
  }
}

  //Statements
  cout <<"\n\t a. Smallest value ";
  cout <<"\n\t b. Largest value ";
  cout <<"\n\t c. Sum of values ";
  cout <<"\n\t d. Average of values ";

  cout <<"\n Enter choice? ";
  cin >> option;

  // return option;
  switch(option)
  {
  case 'a': cout<<"The smallest value is " <<small(a);
    break;
  case 'b': cout<<"The largest value is " <<large(a);
    break;
  case 'c': cout<<"The sum value is " <<sum(a);
    break;
  case 'd': cout<<"The avergae value is " <<avg(a);
    break;
  default : cout<<"\n Sorry. You entered something I don’t understand.";
  }

  return 0;
}

int small(const Array5 & a)
{
  int result = a[0];
  for (size_t n=1; n<a.size(); ++n)
    if (result > a[n] )
        result = a[n];
  return result;
}

// Bonus: an alterantive implementation of 'small', using the sort method of algorithm
int small_alternative( Array5  a)
{
  sort(a.begin(), a.end());
  return a[0];
}

int large(const Array5 & a)
{
  int result = a[0];
  // TODO: find the largest item
  return result;
}


int sum(const Array5 & a)
{
  int result = 0;
  //TODO: compute the sum of the items  
  return result;
}

double avg( const Array5  & a)
{
  double s = sum (a);

  return (s / NUMBERS);
}

[/update]
 
Share this answer
 
v2
Comments
Member 13442137 3-Oct-17 1:05am    
#include <iostream>
#include <array>

using namespace std;

//Function Declarations
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);

constexpr size_t NUMBERS = 5;

int main()
{
array <int, NUMBERS> a;
cout << "enter five numbers" << endl;

for (size_t n=0; n<NUMBERS; ++n)
{
cin >> a[n];
if (cin.fail())
{
cout << "invalid input" << endl;
return 1;
}
}

//Statements
cout <<"\n\t a. Smallest value ";
cout <<"\n\t b. Largest value ";
cout <<"\n\t c. Sum of values ";
cout <<"\n\t d. Average of values ";

cout <<"\n Enter choice? ";
cin >> "%d", &option;

// return option;
switch(option)
{
case 'A': cout<<"The smallest value is " <<small;
break;
case 'B': cout<<"The largest value is " <<large;
break;
case 'C': cout<<"The sum value is " <<sum;
break;
case 'D': cout<<"The avergae value is " <<avg;
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)
cout <<"%d", one;
else if (two<one && two<three && two<four && two<five)
cout <<"%d", two;
else if (three<one && three<two && three<four && three<five)
cout <<"%d", three;
else if (four<one && four<two && four<three && four<five)
cout <<"%d", four;
else
cout <<"%d", five;

}

int large(int u, int w, int x, int y, int z)
{
if (one>two && one>three && one>four && one>five)
cout << "%d", one;
else if (two>one && two>three && two>four && two>five)
cout << "%d", two;
else if (three>one && three>two && three>four && three>five)
cout << "%d", three;
else if (four>one && four>two && four>three && four>five)
cout << "%d", four;
else
cout << "%d", five;

}

int sum(int u, int w, int x, int y, int z)
{
return(u+w+x+y+z);
}

double average( const array <int , NUMBERS> & a)
{
double sum = 0;
for (auto i : a)
sum += i;

return (sum / NUMBERS);
}

SO this is what I tried but it still gives me error. Sorry a beginner to C++
CPallini 3-Oct-17 4:43am    
I have updated my solution. Have a look at it.
Patrice T 3-Oct-17 5:05am    
May be you should tell which error messages and position of error.
Use Improve question to update your question.
So that everyone can pay attention to this information.
You have several structural problems with your code. The first problem you have is that you are attempting to use C++ as though it's C. Don't use printf, use cout. Don't use scanf, use cin. Your use of global variables (the one, two, etc, bits) makes no sense when you're passing these values into methods, and then ignoring the method parameters to use the global variables. What do you think happens if I put in a as an option, thinking I'm going to get the smallest value?
 
Share this answer
 
Comments
Member 13442137 3-Oct-17 1:07am    
I know the code has a problem, the question is can you help me fix it? I am new to C++.
Pete O'Hanlon 3-Oct-17 1:54am    
I would suggest that you take a step back and start learning the basics of C++. We aren't going to solve your homework for you; it's set so that you can show what you know, and identify any gaps in your knowledge so that you and your tutor know what you need to concentrate on. As you're doing C++, you should be looking to use classes here; if I were doing this I would break my code down into a minimum of a Menu and a Maths class, with each part being responsible for ALL of the functionality for that area.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900