0

Hi, I'm trying to write a console program that will sort and find the median of numbers that are given in a text file. The text file can have ints or doubles.

The problem I'm having is outputting the median and the insertionSort functions. I think that they are failing because I did something wrong with either the vector or templates or both as it is my first time writing code using both of those.

Any insight is greatly appreciated.

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

template <typename T>
void median(const vector<T> & values) ;

template <typename T>
void insertionSort(ifstream& in_file, vector<T>& a_values);

template <typename T>
void fill_vector(ifstream& in_file, vector<T> & values) 
{

	T a_value;
	while (!in_file.eof()) 
	{
		in_file >> a_value;
		values.push_back(a_value);
	}
}
int main() 
{
	vector<double> values;
	char filename[16];
	cout << "Enter a file name: ";
	cin >> filename;
	cout << endl;

	ifstream fin(filename);
	if (!fin) 
	{
		cerr << "Could not open the file" << endl;
		return 1;
	}

	fill_vector(fin,values);
	cout << "Values entered: ";

	vector<double>::iterator p;
	for (p = values.begin(); p != values.end(); p++) 
	{
		cout << *p << ",";
	}
	cout << endl;


	cout << "Sorted values are: " << insertionSort << endl;
	cout << "Median = " << median << endl;

} //end main




template <typename T>
void insertionSort(vector<T>& values)
{
	
	int i, j, n = values.size(); //i is the pseudo-Code Index and j is the scanIndex
	T temp;
	// Select List[Index] as element to be inserted into sorted sublist.
	// place v[i] into the sublistv[0] ... v[i-1], 1 <= i < n,
	// so it is in the correct position
	for (i = 1; i < n; i++)
	{
		// index j scans down list from v[i] looking for correct position to locate temp assigns it to v[j]
		j = i;
		temp= values[ i];
		// find where to insert in the sorted sublistand shift elements to right while scanning
		//from right-to-left
		while ( j > 0 && target < values[ j-1 ])
		{
			// shift elements up list to make room for insertion
			values[ j ] = values[ j-1];
			j--;
		}
		// the location is found; insert temp
		values[j] = temp;
		
	}
}
template <typename T>
void median(const vector<T> & values)
{
	double product = 0;


	if( values.size % 2 )
	{
		product = values[values.size() / 2]
	}
	else
	{
		product = ( values[values.size() / 2- 1] + [values.size / 2] ) / 2;
	}

	cout << product << endl;
	return 0;
}
2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by prokek
-1

Hi, I'm trying to write a console program that will sort and find the median of numbers that are given in a text file. The text file can have ints or doubles.

The problem I'm having is outputting the median and the insertionSort functions. I think that they are failing because I did something wrong with either the vector or templates or both as it is my first time writing code using both of those.

Any insight is greatly appreciated.

template <typename T>
void median(const vector<T> & values) ;

template <typename T>
void insertionSort(ifstream& in_file, vector<T>& a_values);
...
	cout << "Sorted values are: " << insertionSort << endl;
	cout << "Median = " << median << endl;

Your assumption is wrong. Your insertionSort and median are functions. You never call them. You just output their respective addresses.

0

Your assumption is wrong. Your insertionSort and median are functions. You never call them. You just output their respective addresses.

I'm unsure what you mean, can you explain more?

0

I'm unsure what you mean, can you explain more?

First, explain me what, in your opinion happens at the lines 49 and 50 of your code:

cout << "Sorted values are: " << insertionSort << endl;
	cout << "Median = " << median << endl;
0

From what I've done before vectors and templates when you call the function using the name of the function I've declared. For example median. After i call it then it should execute and return the product.

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.