can someone show me how to code the median if it is an even number

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

void Fill_Array(double Size[], int& count);
void Print_Array(double Size[], int count);
double Calc_Average(double Size[], int count);
void Sort(double Size[], int count);
void Swap(double& v1, double& v2);
double index_of_smallest(const double Size[], int start_index, int number_used);
double Calc_Median(int count, double Size[]);
void Print_Array_and_Calculations(double median, double average, int count, double Size[]);

const int MAX_SIZE = 100;

int main()
{
	ifstream in_size;
	int count;
	int numbers_used = 0;
	int small_index;
	double Size[MAX_SIZE];
	double median = 0.0;
	double average = 0.0;

	Fill_Array(Size, count);
	Print_Array(Size, count);
	small_index = index_of_smallest(Size, count, numbers_used);
	
	average = Calc_Average(Size, count);
	Sort(Size, count);
	median = Calc_Median(count, Size);
	Print_Array_and_Calculations(median, average, count, Size);
	
	in_size.close();
	return 0;
}

void Fill_Array(double Size[], int& count)
{
	double size;
	ifstream in_size;
	string text_file;

	cout << "Enter the file to read in: ";
	getline(cin, text_file);
	
	cout << "The numbers in the array are:" << endl << endl;
	in_size.open (text_file.c_str ());
	if(in_size.fail())
	{
		cerr  << "Error opening file" << endl;
		exit(1);
	}
	count = 0;
	in_size >> size;
	while((!in_size.eof()) && (count <= MAX_SIZE))
	{
		Size[count] = size;
		count++;
		in_size >> size;

	}
	in_size.close();
}

void Print_Array(double Size[], int count)
{

	int tempcount = 1;
	for(int i = 0; i < count; ++i)
	{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(1);
	
	cout << Size[i] << " ";
	
	if (tempcount == 4)
	{
		cout << endl;
		tempcount = 1;
	
		}
		else
		tempcount++;
	}
}

double Calc_Average(double Size[], int count)
{
	double total = 0.0;
	double average = 0.0;
	for (int i = 0; i < count; i++)
	{
		total = total + Size[i];
	}
	average = double(total) / count;

	return average;
}

void Sort(double Size[], int count)
{
	int index_of_next_smallest;
	
	for (int index = 0; index < count - 1; index++)
	{
		index_of_next_smallest = index_of_smallest(Size, index, count);
		Swap(Size[index], Size[index_of_next_smallest]);
	}
}

void Swap(double& v1, double& v2)
{
	double temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}

double index_of_smallest(const double Size[], int start_index, int number_used)
{
	double min = Size[start_index],
		index_of_min = start_index;
	for (int index = start_index + 1; index < number_used; index++)
		if(Size[index] < min)
		{
			min = Size[index];
			index_of_min = index;
		}
		return index_of_min;
}

double Calc_Median(int count, double Size[])
{
	double median = 0.0;
	double average_median = 0.0;

	median = Size[ count / 2 ];
	
	return (median);
}

void Print_Array_and_Calculations(double median, double average, int count, double Size[])
{
	cout << endl << endl << endl << "The numbers in the array are:" << endl << endl;
	Print_Array(Size, count);
	cout << endl << endl << endl<< "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
	cout << endl << endl;
}

Recommended Answers

All 10 Replies

When the number of items is even, the median is the mean of the two middle items.

i know that but having trouble computing that in coding terms

i know that but having trouble computing that in coding terms

So if you have 4 items, you would need to find items 2 and 3. (Hint: 2 is half of 4.)

but dont I need an if statement in there somehow to determine if its even or odd numbers

>but dont I need an if statement in there somehow to determine if its even or odd numbers
Yep.

and how do I do that

and how do I do that

Use the modulus operator: %

still having problems

double Calc_Median(int count, double Size[])
{
	double median = 0.0;
	double average_median;
	
	median = count / 2;

	if ( count % 2)
	{
		average_median = ((Size[count]) + (Size[count + 1]));
	}
	else
		
		average_median = Size[count / 2];

	
	return (average_median);
}

nevermind figured that out

still having problems

double Calc_Median(int count, double Size[])
{
	double median = 0.0;
	double average_median;
	
	median = count / 2;

	if ( count % 2)
	{
		average_median = ((Size[count]) + (Size[count + 1]));
	}
	else
		
		average_median = Size[count / 2];

	
	return (average_median);
}

One, you are calculating median in line 7, but not using it in line 11. You are going past the bounds of the Size array. Two, you have it backwards. When count % 2 is true, count is odd, so you aren't taking an average, you are returning one number. When count % 2 is false, count is even, so you want to take an average. To avoid confusion, be more explicit. Don't treat count % 2 as true/false, but do this instead. It's easier for both you and the reader to follow:

if (count % 2 == 1)
{
     // count is odd, return one number.
     // code
}
else
{
     // count is even, return average of two numbers
     // code
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.