•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,199 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,054 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 7867 | Replies: 9
![]() |
•
•
Join Date: Oct 2004
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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]);
}
#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]);
}
>>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]?
>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]?
•
•
•
•
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]?
Member of: Beautiful Code Club.
•
•
•
•
Originally Posted by matika
sorry again it will b ((x[n]/2)+(x[n+1])/2)/2
#include <iostream>
float median(int* x, int n)
{
return ((x[n]/2)+(x[n+1])/2)/2;
}
int main()
{
int i, A[] = {1,2,3,4,5,6,7,8,9,10}, size = sizeof A / sizeof *A;
float result = median(A, size);
for ( i = 0; i < size; i++ )
{
std::cout << A[i] << ' ';
}
std::cout << "\nThe median of the data entered is: " << result << '\n';
return 0;
}
/* my output
1 2 3 4 5 6 7 8 9 10
The median of the data entered is: 1.38446e+06
*/ Uh... look up.
Oh nevermind.
Oh nevermind.
float median(int* x, int n)
{
return ( x [ n / 2 ] + x [ n / 2 - 1] ) / 2.0F;
}![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Previous Thread: A little help please? thanks.
- Next Thread: Counting characters and dispalying numbers



Linear Mode