Jump to content
  • Advertisement
Sign in to follow this  
mlukowski

please help

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well I am tryin to make a proram to take 5 numbers given by the user, and out of those 5 numbers find the median. So far here is the code
 #include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout << "Enter five numbers:"<<endl;
int a,b,c,d,e;
cout << "First"; cin >> a;
cout << "Second"; cin >> b;
cout << "Third"; cin >> c;
cout << "Fourth"; cin >> d;
cout << "Last"; cin >> e;
int max = a;
if (max<b) max = b;
if (max<c) max = c;
if (max<d) max = d;
if (max<e) max = e;
cout <<"Max is: " << max <<endl;
int min = a;
if (min>b) min = b;
if (min>c) min = c;
if (min>d) min = d;
if (min>e) min = e;
cout <<"Min is: "<< min <<endl;
// now I have the min and max found , and displayed.
// I do not know how I would get the median







system("PAUSE");
return EXIT_SUCCESS;
}[\source]

Share this post


Link to post
Share on other sites
Advertisement
Hmm, just sort them into order and select the 3rd one in the list.

Simple example (bubble sort):



int nArray[5] = {a, b, c, d, e};

bool bSorted = false;
int nCount = 0;
int nTemp = 0;

while( !bSorted )
{
bSorted = true;
while( nCount < 5 )
{
if( nArray[nCount] > nArray[nCount + 1] )
{
bSorted = false
nTemp = nArray[nCount]
nArray[nCount] = nArray[nCount + 1]
nArray[nCount + 1] = nTemp;
}
nCount++;
}
nCount = 0;
}

// The median is nArray[2]






I think that should do the job. There are far more efficient sorting algorithms than that however. In fact, that's about the simplest and least efficient sorting algorithm out there, but it does the job.

(edit) Fixed sort algorithm.

[Edited by - Leo_E_49 on August 26, 2005 3:40:31 AM]

Share this post


Link to post
Share on other sites
All you have to do is add all your numbers and then divide them by 5 (because you have 5 numbers)

In the exemple you posted the median would be:
int median = (a + b + c + d + e)/5;

The median of 2, 6, 10, is (2+6+10)/3= 6. Easy.




#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout << "Enter five numbers:"<<endl;
int a,b,c,d,e;
cout << "First"; cin >> a;
cout << "Second"; cin >> b;
cout << "Third"; cin >> c;
cout << "Fourth"; cin >> d;
cout << "Last"; cin >> e;
int max = a;
if (max<b) max = b;
if (max<c) max = c;
if (max<d) max = d;
if (max<e) max = e;
cout <<"Max is: " << max <<endl;
int min = a;
if (min>b) min = b;
if (min>c) min = c;
if (min>d) min = d;
if (min>e) min = e;
cout <<"Min is: "<< min <<endl;
int median = (a + b + c + d + e)/5;
cout << "Median is: " << median << endl;
}



[Edited by - Calin on August 26, 2005 3:52:08 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Calin
All you have to do is add all your numbers and then divide them by 5 (because you have 5 numbers)

In the exemple you posted the median would be:
int median = (a + b + c + d + e)/5;

The median of 2, 6, 10, is (2+6+10)/3= 6. Easy.

*** Source Snippet Removed ***


He's asking for a "median" value, what you are describing is a "mean" value.

Median, mean and mode are different statistical values.

Share this post


Link to post
Share on other sites
Thanks for the output. The bubble sorting I could not quite understand. Moreover, I still am a noob. If you can explain that one a little more in depth that would be appreciated.

P.S. I was wondering how do you qoute someone on the threads?

Share this post


Link to post
Share on other sites
Bubble sort is probably the simplest sorting algorithm so it shouldn't be a problem explaining it. Basically:

Let's say we have a set of 5 data elements unordered in an array called nArray[5].

5, 2, 7, 8, 1

We also need a pass bool which is set to true at the beginning of each pass:

bool bSorted;

This will tell us if the list is sorted or not.

We want to sort these in ascending order. The way bubble sort does this is it "bubbles" the highest number to the top of the list in a number of passes.

Let's work through the first pass:

First we compare the first two items

5, 2

If the first item is greater than the second, we swap them:

2, 5

Because one of the items was out of order, the list is not yet sorted so we set the pass bool to false.

Then we proceed to the next two items (keeping the first pair reordered):

5, 7

Because they are in the correct order, we don't do anything.
This carries on until the end of the list.

So now our list should look like:

2, 5, 7, 1, 8

Because the pass bool is set to false, we set the pass bool to true and redo the above process:

2, 5, 1, 7, 8

and

2, 1, 5, 7, 8

and

1, 2, 5, 7, 8

In the next pass, the pass bool will remain true right through out the set, indicating that the set is ordered.

Like I said, there are a lot of faster and more efficient sorts than the bubble sort, including heapsort, mergesort, quicksort, etc. If you feel confident in your programming experience, you might want to look them up.

P.S. Click the quote button at the top of a person's post to quote them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Leo_E_49
Quote:
Original post by Calin
All you have to do is add all your numbers and then divide them by 5 (because you have 5 numbers)

In the exemple you posted the median would be:
int median = (a + b + c + d + e)/5;

The median of 2, 6, 10, is (2+6+10)/3= 6. Easy.

*** Source Snippet Removed ***


He's asking for a "median" value, what you are describing is a "mean" value.

Median, mean and mode are different statistical values.





Sorry.My mistake.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!