0

I got the median finally...so thats a start...now with the average do I do just the same thing with median????

#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);

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);
	cout << Size[small_index] << endl;
	average = Calc_Average(Size, count);
	Sort(Size, count);
	median = Calc_Median(count, Size);
	Print_Array_and_Calculations(median, average);
	
	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;
			cout << setw(15) << "" << setw(18) << "" << setw(21) << "" << setw(26) << "";
			cout.setf(ios::right);
		}
		else
		tempcount++;
	}
}

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

	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;
	
	median = Size[ count / 2 ];

	return (median);
}

void Print_Array_and_Calculations(double median, double average)
{
	cout << endl << endl << endl << "The numbers in the array are:" << endl;
	cout << endl << "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
	cout << endl << endl;
}
0

ok that did work when I tried the same thing so I have average, median, and the first loop finished...the only thing I have left is 1. line up the widths so each line has four numbers which i have coded but what I do not have coded is how they line up....the other thing is i am confused on how to code this program from the instructions to where the 2nd array from smallest to largest is printed out.....

#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);

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);
	cout << Size[small_index] << endl;
	average = Calc_Average(Size, count);
	Sort(Size, count);
	median = Calc_Median(count, Size);
	Print_Array_and_Calculations(median, average);
	
	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;
			cout << setw(15) << "" << setw(18) << "" << setw(21) << "" << setw(26) << "";
			cout.setf(ios::right);
		}
		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;
	
	median = Size[ count / 2 ];

	return (median);
}

void Print_Array_and_Calculations(double median, double average)
{
	cout << endl << endl << endl << "The numbers in the array are:" << endl;
	cout << endl << "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
	cout << endl << endl;
}
0

one other issue I am realizing...my median doesnot take into account if the count is even

0

figured out the sort array so there are only two things I am not sure of...one is the median...If the numbers are odd I have it to nor problem but if the numbers are even I do not know how to calculate in c++ terms of the 2 middle numbers

I know it must be something liek divide by 2 then count + 1 and find the avg or something....

the only other issue is setting the widths of the decimal points to look like this.....

45.3 57.4 23.6 34.2
234.6 34.9 54.8 934.9
8432.5 9809.3 539.7 43.9
12.0


its kind of hard to tell but pretty much need all the decimal points to line up and I am finally done with C++

#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;
	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;
}
0

do you guys know what i mean when i say my median when its an even set... I judt dont know how in coding terms to compute this

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.