Can anyone help me how to program in C++ which determines the median of five input numbers?
The median of five input numbers!!!
Collapse
X
-
Tags: None
-
Originally posted by suhdominicCan anyone help me how to program in C++ which determines the median of five input numbers? -
Originally posted by suhdominicCan anyone help me how to program in C++ which determines the median of five input numbers?
41.52 27 96.01 12.1 103.55
then the program should output
the median of 41.52 27 96.01 12.1 and 103.55 is 41.52Comment
-
The median of five input numbers!!!
Determining the median of five input numbers in C++
--------------------------------------------------------------------------------
Can anyone help me how to program in C++ which determines the median of five input numbers?
the median is the middle number when the five numbers are arranged in order. the user can input the values in any order. so your program must determine which value is the other four for example, if the user enters
41.52 27 96.01 12.1 103.55
then the program should output
the median of 41.52 27 96.01 12.1 and 103.55 is 41.52
i don't know where to start with im assuming to use if functionComment
-
Originally posted by suhdominicthe median is the middle number when the five numbers are arranged in order. the user can input the values in any order. so your program must determine which value is the other four for example, if the user enters
41.52 27 96.01 12.1 103.55
then the program should output
the median of 41.52 27 96.01 12.1 and 103.55 is 41.52
hi i am giving a easy method for calculating , see that whether you can implement it
Example 1: To find the median of 4,5,7,2,1 [ODD].
Step 1: Count the total numbers given.
There are 5 elements or numbers in the distribution.
Step 2: Arrange the numbers in ascending order.
1,2,4,5,7
Step 3: The total elements in the distribution (5) is odd.
The middle position can be calculated using the formula. (n+1)/2
So the middle position is (5+1)/2 = 6/2 = 3
The number at 3rd position is = Median = 4
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
just i am summarising the steps you need to implement
1)Declare a array of 5 elements
2)Sort the contents of the array in ascending order
3)Calculate middle position using formula (n+1)/2
4)store (n+1)/2 in result.
5) array[result] is your required Answer
Implement this in c++.Comment
-
hi i am giving a easy method for calculating , see that whether you can implement it
Example 1: To find the median of 4,5,7,2,1 [ODD].
Step 1: Count the total numbers given.
There are 5 elements or numbers in the distribution.
Step 2: Arrange the numbers in ascending order.
1,2,4,5,7
Step 3: The total elements in the distribution (5) is odd.
The middle position can be calculated using the formula. (n+1)/2
So the middle position is (5+1)/2 = 6/2 = 3
The number at 3rd position is = Median = 4
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ ~~~~~~~
just i am summarising the steps you need to implement
1)Declare a array of 5 elements
2)Sort the contents of the array in ascending order
3)Calculate middle position using formula (n+1)/2
4)store (n+1)/2 in result.
5) array[result] is your required Answer
Implement this in c++.Comment
-
Try it out . I have the solution ready , i.e Program is up and giving results. I want you to try and program it. if u find any dificulties i am there to help you out.Comment
-
Thank you for a great help but my course haven't went to array thing yet so is there any other way just using if to figure out the median?thank youComment
-
#include <iostream>
using namespace std;
int main()
{
float a,b,c,d,e;
float Median;
int i;
cout << "Pleaes enter five float numbers separated by space: " << endl;
cin >> a >> b >> c >> d >> e;
i = a + b + c + d + e;
if (i % 2 == 0)
{
Median = i/5;
cout << "The median of " << a << ", " << b << ", " << c << ", " << d << " and " << e << " is " << Median << endl;
}
else
{
this is what i worked so far but it gives the answer when the sum of the values are even but i don't know how to do odd one without using array i don't know what that is and i don't think i supposed to use thatComment
-
Originally posted by suhdominic#include <iostream>
using namespace std;
int main()
{
float a,b,c,d,e;
float Median;
int i;
cout << "Pleaes enter five float numbers separated by space: " << endl;
cin >> a >> b >> c >> d >> e;
i = a + b + c + d + e;
if (i % 2 == 0)
{
Median = i/5;
cout << "The median of " << a << ", " << b << ", " << c << ", " << d << " and " << e << " is " << Median << endl;
}
else
{
this is what i worked so far but it gives the answer when the sum of the values are even but i don't know how to do odd one without using array i don't know what that is and i don't think i supposed to use that
Follow the logic mentioned below and u can solve ur problem.
Example 1: To find the median of 4,5,7,2,1 [ODD].
Step 1: Count the total numbers given.
There are 5 elements or numbers in the distribution.
Step 2: Arrange the numbers in ascending order.
1,2,4,5,7
Step 3: The total elements in the distribution (5) is odd.
The middle position can be calculated using the formula. (n+1)/2
So the middle position is (5+1)/2 = 6/2 = 3
The number at 3rd position is = Median = 4Comment
Comment