Need help calculating Median when array is even

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 6
Reputation: somer2412 is an unknown quantity at this point 
Solved Threads: 0
somer2412 somer2412 is offline Offline
Newbie Poster

Need help calculating Median when array is even

 
0
  #1
Oct 13th, 2004
Please help I am writing a program that calculates the mean and median of a sequence of integers. My program works fine as long as the array is odd. Can someone please help me with a statement to calculate the median when the array is even. I have included my code for the program.

#include <iostream>

float mean (const int* A, size_t size); //calculates mean of data in A
float median (int* A, size_t size); // calculates median of data in A
void swap (int& x, int& y); // interchanges values of x and y
void sort (int*A, size_t size); // sorts the data in A
void bubblesort (int, int);

int main()
{
int n;
size_t size;
int A [100]; size = 0;
std::cout << "Enter data to calculate the mean and median: ";
while((std::cin >> n) && (size < 100))
{
A[size] = n;
++size;
}

for (size_t i = 0; i < size; i++)
std::cout << A[i] << ' ';
std::cout << '\n';

std::cout << "The mean of the data entered is: " << mean(A,size) << '\n';
std::cout << "The median of the data entered is: " << median(A,size)<< '\n';

for (size_t i = 0; i < size; i++)
std::cout << A[i] << ' ';
std::cout << '\n';
return 0;
}

float mean(const int* A, size_t size)
{
float total = 0;
for (size_t i = 0; i < size; i++)
total += A[i];
return total / size;
}

float median(int* A, size_t size)
{
sort(A,size);
return A[size/2];
}

void swap(int& x, int& y)
{
int hold = x;
x = y;
y = hold;
}

void sort(int* A, size_t size)
// bubble sort
{
for (size_t pass = 0; pass < size - 1; pass ++)
for (size_t j = 0; j < size - 1; j++)
if (A[j] > A[j + 1])
swap(A[j], A[j+1]);
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help calculating Median when array is even

 
0
  #2
Oct 14th, 2004
>Can someone please help me with a statement to calculate the median when the array is even.
Take the median of the two middle items: a[size / 2] and a[size / 2 + 1].
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help calculating Median when array is even

 
0
  #3
Oct 14th, 2004
>>Can someone please help me with a statement to calculate the median when the array is even.
>Take the median of the two middle items: a[size / 2] and a[size / 2 + 1].
Wouldn't that be the mean of a[size / 2] and a[size / 2 - 1]?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,740
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help calculating Median when array is even

 
0
  #4
Oct 14th, 2004
Originally Posted by Dave Sinkula
>>Can someone please help me with a statement to calculate the median when the array is even.
>Take the median of the two middle items: a[size / 2] and a[size / 2 + 1].
Wouldn't that be the mean of a[size / 2] and a[size / 2 - 1]?
Feh, you're no fun.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 19
Reputation: matika is an unknown quantity at this point 
Solved Threads: 0
matika matika is offline Offline
Newbie Poster

Re: Need help calculating Median when array is even

 
0
  #5
Oct 15th, 2004
((n/2)+(n+1)/2)/2

n is the nmber of elements in the array

i e
if yr array was x[n];
median = ((x[n]/2)+(x[n]+1)/2)/2;
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 19
Reputation: matika is an unknown quantity at this point 
Solved Threads: 0
matika matika is offline Offline
Newbie Poster

Re: Need help calculating Median when array is even

 
0
  #6
Oct 15th, 2004
Originally Posted by matika
((n/2)+(n+1)/2)/2

n is the nmber of elements in the array

i e
if yr array was x[n];
median = ((x[n]/2)+(x[n]+1)/2)/2;
sorry again it will b ((x[n]/2)+(x[n+1])/2)/2
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help calculating Median when array is even

 
0
  #7
Oct 15th, 2004
Originally Posted by matika
sorry again it will b ((x[n]/2)+(x[n+1])/2)/2
Really?
  1. #include <iostream>
  2.  
  3. float median(int* x, int n)
  4. {
  5. return ((x[n]/2)+(x[n+1])/2)/2;
  6. }
  7.  
  8. int main()
  9. {
  10. int i, A[] = {1,2,3,4,5,6,7,8,9,10}, size = sizeof A / sizeof *A;
  11. float result = median(A, size);
  12. for ( i = 0; i < size; i++ )
  13. {
  14. std::cout << A[i] << ' ';
  15. }
  16. std::cout << "\nThe median of the data entered is: " << result << '\n';
  17. return 0;
  18. }
  19.  
  20. /* my output
  21.  1 2 3 4 5 6 7 8 9 10
  22.  The median of the data entered is: 1.38446e+06
  23.  */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 19
Reputation: matika is an unknown quantity at this point 
Solved Threads: 0
matika matika is offline Offline
Newbie Poster

Re: Need help calculating Median when array is even

 
0
  #8
Oct 20th, 2004
float median(int* x, int n)
{
return ((x[n/2])+(x[(n/2)+1]))/2;
}
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 19
Reputation: matika is an unknown quantity at this point 
Solved Threads: 0
matika matika is offline Offline
Newbie Poster

Re: Need help calculating Median when array is even

 
0
  #9
Oct 20th, 2004
tell me when it works
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,377
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Need help calculating Median when array is even

 
0
  #10
Oct 20th, 2004
Uh... look up.

Oh nevermind.
float median(int* x, int n)
 {
    return ( x [ n / 2 ] + x [ n / 2 - 1] ) / 2.0F;
 }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC