I need to extract the middle numeric number.

It works for two order of numbers, for example:

when I enter the numbers in this order
12, 14,6 it works...when I enter in this order 12, 6, 14 it works as well, but when I enter in this order 14, 12, 6 it doesn't work.

Any suggestions?

see the code below:


int a, b, c; //user input
int d;//sum of largest and smallest
int sum; // total of all numbers entered by user
int midnum; //numeric middle number

cout << "enter three numbers \n";

cin >> a >> b >> c;

sum = a + b + c;

if ( a > b )
cout << a;
cout <<"\n";

if ( b > c )
cout << b << "\n";

if ( c < a)
cout << c << "\n" "\n";

d = b + c;

midnum = sum - d;

cout << "The middle number in numeric order is: " << midnum << "\n";

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template <typename T>
inline T mid(const T& a, const T& b, const T&c)
{
  T init[3] = {a, b, c};
  vector<T> v(init, init + 3);

  sort(v.begin(), v.end());

  return v[1];
}

int main()
{
  cout<<"The middle number is "<< mid(12, 14, 6) <<endl;
}

Or, if you're scared of the standard library:

#include <iostream>

using namespace std;

#define min(pa, pb) (*pa < *pb ? pa : pb)
#define max(pa, pb) (*pa > *pb ? pa : pb)

template <typename T>
inline void swap_two(T *a, T *b)
{
  T save = *a;
  *a = *b;
  *b = save;
}

template <typename T>
inline T mid(const T& a, const T& b, const T&c)
{
  T list[3] = {a, b, c};

  swap_two(&list[0], min(min(&list[0], &list[1]), &list[2]));
  swap_two(&list[2], max(max(&list[0], &list[1]), &list[2]));

  return list[1];
}

int main()
{
  cout<<"The middle number is "<< mid(12, 14, 6) <<endl;
}

Or if you like doing everything the hard way:

#include <iostream>

using namespace std;

template <typename T>
inline T mid(const T& a, const T& b, const T&c)
{
  if (a > b && a > c) {      // a is the biggest
    if (b > c)
      return b;
    else
      return c;
  }
  else if (a < b && b > c) { // b is the biggest
    if (a > c)
      return a;
    else
      return c;
  }
  else {                     // c is the biggest
    if (a > b)
      return a;
    else
      return b;
  }
}

int main()
{
  cout<<"The middle number is "<< mid(12, 14, 6) <<endl;
}

And of course, that can be tightened up to:

#include <iostream>

using namespace std;

template <typename T>
inline T mid(const T& a, const T& b, const T&c)
{
  if (a > b && a > c)      // a is the biggest
    return b > c ? b : c;
  else if (a < b && b > c) // b is the biggest
    return a > c ? a : c;
  else                     // c is the biggest
    return a > b ? a : b;
}

int main()
{
  cout<<"The middle number is "<< mid(12, 14, 6) <<endl;
}

But that's not nearly as interesting as the first two. :D

I am sorry. I forgot to mention that I can only used if statements. No else and no compound expressions.

>No else and no compound expressions.
Technically, else is a part of an if statement, but I fail to see how you're incapable of learning anything from all of the code I gave you out of the goodness of my heart. The third example in particular should be easy to modify to meet your restrictions, assuming a moderate amount of intelligence.

By the way, when someone takes the time not only to answer your question, but to solve your problem several ways and give you multiple perspectives, you should at least say "thank you". How helpless and ungrateful can you be?

Narue, I didn't meant to come of ungratefull at all. I do appreciate your help. I was shocked that you did take the time out to do the code, I really didn't expect that. Again, please accept my apologize. I am very grateful your and else's help. Thank you! Thank you! From the bottom of my heart.

oldroad.

This article has been dead for over six months. Start a new discussion instead.