I am in an Intro to Computing class and I have been doing fairly well but this program really has me stumped. This is our assignment:

The program will take as input a worker’s first name, last name, ID number (four digit number), and unit production numbers for the last 5 weeks. Each unit production number is an integer value indicating how many pieces the employee produced that week. Next the program will call a function that calculates the mean of the number of units. Next, the program will call a function that calculates the median of the number of units. Finally, the program will call a third function that prints the employee’s last name, the mean and the median that were calculated in the other two functions. The program will continue reading in information and printing the results until the user indicates that there are no more employees.

My problem is with the median. We haven't learned anything about arrays or sorting in the class. I do not want to do 120 "else-if" statements, but it is the only solution I have come up with. Any input from you guys about how to fix the function would be great and most appreciated.

``````#include <iostream>
#include <string>
using namespace std;

float Mean (int num1, int num2, int num3, int num4, int num5)
{//PURPOSE: Calculate the mean of the five numbers
//INPUT: five integers, weekOne, weekTwo, weekThree, weekFour, weekFive
//OUTPUT: the mean of the five numbers, avg
float avg=0;
avg=((num1+num2+num3+num4+num5)/ 5.0);
return avg;
}

float Median (int a, int b, int c, int d, int e)       // variables named for easier coding
{//PURPOSE: Calculate the median of the five numbers
//INPUT: five integers, weekOne, weekTwo, weekThree, weekFour, weekFive
//OUTPUT: the median of five numbers, median
float median=0;

if (a<b && b<c && c<d && d<e)
cout<<a<<b<<c<<d<<e;

else if (a<b && b<c && d<c && e<d)
cout<<a<<b<<c<<e<<d;

else if ( <  &&  <  &&  <  &&  < )
cout<< << << << << ;

return median;
}

void Print (string last, float m, float med)      //parameters
{//PURPOSE:To print out three variables
//INPUT: the three reference parameters lastName, mn, md
//OUTPUT: the three variables, last, m, med

cout<<last<<", "<<m<<" and "<<med<<endl;
}

int main ()
{
string firstName, lastName;
char response;
int ID, weekOne, weekTwo=0, weekThree=0, weekFour=0, weekFive=0;
float mn=0, md=0;

cout<<"Do you have an employee to input? Y or N"<<endl;
cin>>response;

while(response=='Y')
{

cout<<"Enter first name, last name, ID number (four digits), and unit production numbers (for past five weeks)"<<endl;
cin>>firstName;
cin>>lastName;
cin>>ID;
cin>>weekOne;
cin>>weekTwo;
cin>>weekThree;
cin>>weekFour;
cin>>weekFive;

mn = Mean(weekOne, weekTwo, weekThree, weekFour, weekFive);  //function call

md = Median(weekOne, weekTwo, weekThree, weekFour, weekFive);

Print (lastName, mn, md);

cout<<"Do you have an employee to input? Y or N"<<endl;
cin>>response;
}
return 0;
}
``````

Edited by deceptikon: Fixed code tags

3
Contributors
6
Replies
8
Views
6 Years
Discussion Span
Last Post by Jakjakjak

Your job would be vastly simplified by storing the numbers in an array rather than as separate variables. Then you can sort the array and choose the median with ease.

I would love to use an array, however we have not gone over them in class even in the least bit. I emailed my professor and he told me that the median function can sort the numbers with only 10 - 11 if statements; I have no idea how this is possible using only what we have learned in class.

For 5 numbers, it can be done. All you have to do is place the lowest (or highest) number into a, using four if statements (compare a with b,c,d and e, then switching each time a is less (or greater) than one of those). Follow it up with comparing b to c,d and e (three if statements, making it seven total). Lastly, compare c with d and e (two statements, making it nine statements total). Since you're looking for the median in a group of five numbers, you will need the third number, which is c. You don't have to compare d and e (and even if you do, it takes just an additional one if statement, making a total of 10).

Edited by scudzilla

Thanks a lot scudzilla! I will go try that and see what happens. Each of the if statements would be nested within each other right?

No, all the if statements are individual. Nothing nested. Something like:

``````if(a<=b){
a = a + b;
b = a - b; //switches a with b
a = a - b;
}
if(a<=c){
a = a + c;
c = a - c; //switches a with c
a = a - c;
}
``````

and so on

Edited by scudzilla

Ok I just did what you said and it works like a charm, thank you so much! I really appreciate all the help. This thing has been frustrating me for the past week.