I am writing a function and need to calculate the median...I am gonna b honest I absolutely have no idea how to start this but it is pretty much one of the last things I need to do with my program. Any advice would br greatly appreciated.....

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

const int MAX_SIZE = 100;

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

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

	Fill_Array(Size, count);
	Print_Array(Size, count);
	average = Calc_Average(Size, average);
	Sort(Size, numbers_used);
	Calc_Median(median);
	Print_Array_and_Calculations(median, average);

	return 0;
}

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

	cout << "Enter the file to read in:";
	cin >> text_file;
	cout << endl << "The numbers in the array are:" << endl << endl;

	if(in_size.fail())
	{
		cerr  << "Error opening file";
		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(int Size[], int count)
{
	int number_used = 0;
	for(int index = 0; index < number_used; index++)
		cout << Size[index] << " ";
}

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

	return average;
}

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

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

int index_of_smallest(const int Size[], int start_index, int number_used)
{
	int 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;
}

int Calc_Median(int median)
{

}

void Print_Array_and_Calculations(int median, double average)
{
	cout << "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
}

Brief tutorial in mean, median and mode. The median is the value in the center of the data, after the data has been sorted. So if you have the values 10, 20, 30, 40 and 50 the median is 30 because there are two values below it and two above it.

If the array is already sorted then finding the median is simple -- just divide the number of elements in the array by 2 and use that number to index into the array.

so it pretty much is just median = Size[] / 2

not quite. median = Size [ NumElements / 2 ]; Assuming Size[] has been sorted.

One last question I have...I dunno how to explain this but I have two input files....I need to type the file to read in from the cout << "Enter the file to read in:"; and one file is data1.txt and the other is data2.txt

Does anyone understand what I mean...kinda difficult to explain

string filename;
cout << "Enter file name\n";
getline(cin, filename);

The above will let you enter a file name even if it contains spaces.

sorry one final thing and this is completed I am getting this error:

error LNK2019: unresolved external symbol "int __cdecl Calc_Median(int,int const * const)" (?Calc_Median@@YAHHQBH@Z) referenced in function _main
1>P06.obj : error LNK2019: unresolved external symbol "void __cdecl Fill_Array(int * const,int &)" (?Fill_Array@@YAXQAHAAH@Z) referenced in function _main
1>C:\Documents and Settings\Don & Diane Kruep\Desktop\Project 6\Debug\Project 6.exe : fatal error LNK1120: 2 unresolved externals

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

const int MAX_SIZE = 100;

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

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

	Fill_Array(Size, count);
	Print_Array(Size, count);
	average = Calc_Average(Size, average);
	Sort(Size, numbers_used);
	Calc_Median(median, Size);
	Print_Array_and_Calculations(median, average);

	return 0;
}

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

	cout << "Enter the file to read in:";
	getline(cin, text_file);
	cout << endl << "The numbers in the array are:" << endl << endl;

	if(in_size.fail())
	{
		cerr  << "Error opening file";
		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(int Size[], int count)
{
	int number_used = 0;
	for(int index = 0; index < number_used; index++)
		cout << Size[index] << " ";
}

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

	return average;
}

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

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

int index_of_smallest(const int Size[], int start_index, int number_used)
{
	int 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;
}

int Calc_Median(int median, int Size[])
{
	median = Size [ MAX_SIZE / 2 ];
	return median;
}

void Print_Array_and_Calculations(int median, double average)
{
	cout << "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
}

It's C++ syntax, not Java, and no spaces, so this:

// code

should be this:

[code=cplusplus] // code

[/code]

This'll post the code with line numbers. When you post the error, make sure it points to the line number listed in the code you post here and it'll be easier for us to help you (actually, I'm not seeing the line number in the error you posted since it looks like a linker error). Anyway, please repost with proper code tags.

Pay attention to argument types. The prototype line 16 uses the const keyword, but the function on line 114 does not. The two are not the same.

There is a mismatch with the Fill_Array() function,
prototype is

void Fill_Array(int Size[], int& count);

whereas the implementation is

void Fill_Array(int Size[], int& count, [B]int& number_used[/B])

In main(), you are using it in the manner which implies that you should remove the
'int& number_used' argument from the implementation.

Can anyone see what I am doing wrong in my code...I know this is not one of my text files...my two text files are data1.txt and data2.txt but when I tried that I get the same output


Enter the file to read in: jdjkd

The numbers in the array are:


The average of the numbers is -0.8
The median of the numbers is 0

Press any key to continue . . .

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

const int MAX_SIZE = 100;

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

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

	Fill_Array(Size, count, numbers_used);
	Print_Array(Size, count);
	average = Calc_Average(Size, average);
	Sort(Size, numbers_used);
	Calc_Median(median, Size);
	Print_Array_and_Calculations(median, average);

	return 0;
}

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

	cout << "Enter the file to read in: ";
	getline(cin, text_file);
	cout << endl << "The numbers in the array are:" << endl << endl;

	if(in_size.fail())
	{
		cerr  << "Error opening file";
		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(int Size[], int count)
{
	int number_used = 0;
	for(int index = 0; index < number_used; index++)
		cout << Size[index] << " ";
}

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

	return average;
}

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

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

int index_of_smallest(const int Size[], int start_index, int number_used)
{
	int 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;
}

int Calc_Median(int median, int Size[])
{
	median = Size [ MAX_SIZE / 2 ];
	return median;
}

void Print_Array_and_Calculations(int median, double average)
{
	cout << endl << "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
	cout << endl << endl;
}

You never actually open the input file.

Line 40: You declare the ifstream in_size.

By Line 45, you have the filename, but you don't use it for anything.

Line 47: You test whether in_size has failed, but it hasn't yet because you've never tried to open it. You need to try to open the file in line 46:

in_size.open (text_file.c_str ());

One last thing and I should be done...I think I did it correctly but is not compiling and returning error from my fill array coding

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

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename);
void OutputHeaders(ifstream &inp, ofstream &out);
bool ReadFromFile(ifstream &inp, string &item, double &cost, int &quantity);
double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost);
void WritetoFile(ofstream &out, string item, double &cost, int &quantity, double &total_cost, double &final_cost);

int main()
{
	ifstream inp;
	ofstream out;
	double cost = 0.00,
	total_cost = 0.00,
	final_cost = 0.00;
	int quantity = 0;
	string item = " ";
	
	OpenFiles(inp, out, "P05.txt", "P05out.txt");
	
	OutputHeaders(inp, out);

	out.setf(ios::fixed);
	out.setf(ios::showpoint);
	out.precision(2);
	
	while(ReadFromFile(inp, item, cost, quantity))
	{
		CalculateCost(cost, quantity, total_cost, final_cost);
		WritetoFile(out, item, cost, quantity, total_cost, final_cost);
	}

	out << setw(78) << "-------------";
	out << endl << setw(53) << "Final Total" << setw(25) << final_cost;
	out.setf(ios::right);

	

	inp.close();
	out.close();
	
	return 0;
}

void OpenFiles(ifstream &inp, ofstream &out, string inpfilename, string outfilename)
{
	{
		inp.open(inpfilename.c_str());
		if (inp.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
	{
		out.open(outfilename.c_str());
		if (out.fail())
		{
			cerr << "Error\n";
			exit(1);
		}
	}
}

void OutputHeaders(ifstream &inp, ofstream &out)
{

	out << "Programmed by Bryan Kruep";
	out << endl << endl;
	out << setw(15) << "Item" << setw(18) << "Cost" << setw(21) << "Quantity" << setw(24) << "Total Cost" << endl;
	out.setf(ios::right);
	out << setw(15) << "----" << setw(18) << "----" << setw(21) << "--------" << setw(24) << "----------" << endl;
	out.setf(ios::right);
	
}

bool ReadFromFile(ifstream &inp, string &item, double &cost, int &quantity)
{
	item = inp.good();
	cost = inp.good();
	quantity = inp.good();
	
	return (inp >> item >> cost >> quantity);
}

double CalculateCost(double &cost, int &quantity, double &total_cost, double &final_cost)
{
	
	total_cost = quantity * cost;
	final_cost = final_cost + total_cost;




	return total_cost;


}

void WritetoFile(ofstream &out, string item, double &cost, int &quantity, double &total_cost, double &final_cost)
{
	out.setf(ios::right);
	out << setw(15) << item << setw(18) << cost << setw(21) << quantity << setw(24) << total_cost;
	out << endl;
	
}

what and where is the error?

line 88: can't do that. The function was declared to return a bool, not ifstream. But you could do something like this:

if( !(inp >> item >> cost >> quantity) )
        return false;
return true;

or this: return !(inp >> item >> cost >> quantity) ? false : true;

Ok I am a complete idiot because I just send the wrong code over...

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

const int MAX_SIZE = 100;

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

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

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

void Fill_Array(int Size[], int& count, int& numbers_used)
{
	int size;
	ifstream in_size;
	string text_file;
	in_size.open (text_file.c_str ());
	cout << "Enter the file to read in: ";
	getline(cin, text_file);
	cout << endl << "The numbers in the array are:" << endl << endl;

	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(int Size[], int count)
{
	int number_used = 0;
	for(int index = 0; index < number_used; index++)
		cout << Size[index] << " ";
}

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

	return average;
}

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

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

int index_of_smallest(const int Size[], int start_index, int number_used)
{
	int 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;
}

int Calc_Median(int median, int Size[])
{
	median = Size [ MAX_SIZE / 2 ];
	return median;
}

void Print_Array_and_Calculations(int median, double average)
{
	cout << endl << "The average of the numbers is " << average;
	cout << endl << "The median of the numbers is " << median;
	cout << endl << endl;
}

that should be better

compiles ok for me.

it works but when I type in a datafile it will not show anything...I have two data files in my program...understand what I mean?

line 48: The file is not opened. Move line 44 to after line 47. You can't open the file until after you know the value of the filename.

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.