943,513 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 16483
  • C++ RSS
Oct 13th, 2004
0

Need help calculating Median when array is even

Expand Post »
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]);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
somer2412 is offline Offline
6 posts
since Oct 2004
Oct 14th, 2004
0

Re: Need help calculating Median when array is even

>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].
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 14th, 2004
0

Re: Need help calculating Median when array is even

>>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]?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 14th, 2004
0

Re: Need help calculating Median when array is even

Quote 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 15th, 2004
0

Re: Need help calculating Median when array is even

((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;
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Oct 15th, 2004
0

Re: Need help calculating Median when array is even

Quote 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
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Oct 15th, 2004
0

Re: Need help calculating Median when array is even

Quote originally posted by matika ...
sorry again it will b ((x[n]/2)+(x[n+1])/2)/2
Really?
C++ Syntax (Toggle Plain Text)
  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.  */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Oct 20th, 2004
0

Re: Need help calculating Median when array is even

float median(int* x, int n)
{
return ((x[n/2])+(x[(n/2)+1]))/2;
}
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Oct 20th, 2004
0

Re: Need help calculating Median when array is even

tell me when it works
Reputation Points: 15
Solved Threads: 0
Newbie Poster
matika is offline Offline
19 posts
since May 2004
Oct 20th, 2004
0

Re: Need help calculating Median when array is even

Uh... look up.

Oh nevermind.
float median(int* x, int n)
 {
    return ( x [ n / 2 ] + x [ n / 2 - 1] ) / 2.0F;
 }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: A little help please? thanks.
Next Thread in C++ Forum Timeline: Counting characters and dispalying numbers





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC