Simple C++ Program

Pages: 123... 5
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#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)
{
	//cout << "Sorted 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;
		//cout << insertionSort << endl;
	}
}
template <typename T>
void median(const vector<T> & values)
{
	double product = 0;


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

	cout << product << endl;
	return 0;
}

this may be a stupid reply by me. But I thought when using templates you couldnt do prototypes. If this is the case i would put all of the code for the templates before the main declaration. OR a much cleaner approach would be to put the templates in there own file and just include that file.

Im not sure if this will solve your problem but we just touched templates at the end of the semester.
I think they way i have the prototypes should be fine. After I get the program to run ill put the templates in a header file but I need to get it to work first.

Thanks though.
I doubt this is a fix to your problem but on line 49 it looks like you are calling the insertion sort method incorrectly.

also the same for line 50 but you have that commented out.
Last edited on
How do I call it correctly?
comment out line 49 and see if it compiles without any errors.

You arnt calling it correctly because it is a function that you made but you are using it like a variable

say I had the following function
1
2
3
4
5
int print_num(int num)
{
return num;
}


in main you could do something like this

cout << "The number is: " << print_num(10) << endl;

you cant do this
cout << "The number is: " << print_num << endl;

Im not sure what you are trying to do exactly.

Because
cout << "Sorted values are: " << insertionSort << endl;
doesnt make since when when you think about it and what the insertionsort function does. Anyways you would call insertionSort(takes a vector as a paramater)

So to test if everything else works correctly delete line 49 and compile to see if it works then go from there.

Also please write a description of what you are trying to accomplish

Also your media function returns and integer "0" even though its return type is void. You will have problems there as well.

I would suggest getting better with functions before attempting templates
Last edited on
I just commented out line 49 and it worked and printed out the number that were read in from the file i created etc.

Now you just need to fix the errors in those templates and learn how to call them correctly. If I knew what exactly you needed I could help you but im not going to shell out free answers. A key step to learning is trying over and over and failing a couple of times
Ok, I see what you saying... The program does compile without those lines. Sorry for not giving a description earlier.

The program is supposed to output first, the numbers in the text file, then sort the numbers and output them in a sorted manner, then finaly get the median and output that.

I did change the median to return product right after i posted this version here.

I also added m = median (v) I do have significantly less errors now and I feel I'm close to getting this to run.

The updated code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

template <typename T>
double median(const vector<T> & v) ;

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

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

	T a_value;
	while (!in_file.eof()) 
	{
		in_file >> a_value;
		v.push_back(a_value);
	}
}

int main() // begin main
{
	vector<double> v;
	char filename[16];
	double m = median(v);
	double is = insertionSort(v);


	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,v);
	cout << "Values entered: ";

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


	cout << "Sorted values are: " << is << endl;
	cout << "Median = " << m << endl;

} //end main




template <typename T>
double insertionSort(ifstream& in_file, vector<T>& a_value) //vector is used as an implementation of a List
{
	int i, j, n = v.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 = a_value[ i];
		// find where to insert in the sorted sublistand shift elements to right while scanning
		//from right-to-left
		while ( j > 0 && target < a_value[ j-1 ])
		{
			// shift elements up list to make room for insertion
			a_value[ j ] = a_value[ j-1];
			j--;
		}
		// the location is found; insert temp
		a_value[j] = temp;
	}
}
template <typename T>
double median(const vector<T> & v)
{
	double product = 0;


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

	
	return product;
}

You could, you know, post the errors.
Of course, sorry i didnt.

Im getting one error.


1>c:\users\z\documents\visual studio 2008\projects\p8220\p8220\p8220.cpp(34) : error C2780: 'double insertionSort(std::ifstream &,std::vector<T> &)' : expects 2 arguments - 1 provided

1> c:\users\z\documents\visual studio 2008\projects\p8220\p8220\p8220.cpp(15) : see declaration of 'insertionSort'
 
You could, you know, post the errors.

Yes that would be helpful lol.

The main portion of the code looks fine now.

The fill template works

the median has an issue on line 93 you for got () after size should be
v.size() // bcause size is a function of the vector class

also im not sure if on line 95 and 99 you can do
product = v[v.size() / 2]

i think it should be like this
product = v * (v.size() / 2);

and the same goes for line 99

after those corrections i think the median function will work

Now for the insertion sort you have it as returning a double which it doesnt return anything. I think it would be better as void and just make sure it sorts them correctly.

I would just worry about printing the sorted vector in main or make a separate function to do that. Its up to you.

Because as an outsider when i see an insertion sort method I wouldnt expect it to print the vector afterwards.

Fix those issues then recompile and let us know

I just saw your post and it says insertion sort expects 2 arguments. It does expect a file name and a vector. I would move it until after the filename has been verified and opened.
Last edited on
Ok, im working on it right now. Ill update you as soon as I get the median working.

Also i really appreciate the help.
Im getting these errors now :

1>c:\users\z\documents\visual studio 2008\projects\p8220\p8220\p8220.cpp(102) : error C2676: binary '*' : 'const std::vector<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> _Ty=double
1> ]
1> c:\users\z\documents\visual studio 2008\projects\p8220\p8220\p8220.cpp(33) : see reference to function template instantiation 'double median<double>(const std::vector<_Ty> &)' being compiled
1> with
1> [
1> _Ty=double
1> ]
1>c:\users\z\documents\visual studio 2008\projects\p8220\p8220\p8220.cpp(106) : error C2676: binary '*' : 'const std::vector<_Ty>' does not define this operator or a conversion to a type acceptable to the predefined operator
1> with
1> [
1> _Ty=double
1> ]


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <typename T>
double median(const vector<T> & v)
{
	double product = 0;


	if( v.size() % 2 )
	{
		product = v * (v.size() / 2); 
	}
	else
	{
		product = (product = v * (v.size() / 2) + [v.size / 2] ) / 2;
	}

	
	return product;
}



Do i need to do something with [v.size / 2] also? use () instead of []?
now that I think about it I dont think you can multiply a vector time something. If you are wanting to get the median (http://en.wikipedia.org/wiki/Median).

Read that and then try to see what you did wrong in the function
Median is the middle number pretty much. so you are going to want to return the middle of the number that represents the middle of the vector. You can use a vector like an array. so say you do things like this
product = v[i]; // i could be the middle number I would also change the variable product -> median

it will make more since that way
This median was originally from an array program i wrote, all i did was change the variables to fit into a vector form. Ill read that site and see what i can do. The more important part is getting the program to output the sorted numbers. What do I need to know get that to output correctly?
here should be the process
1. create variables etc
2. open file and make sure its open etc
3. Here you call your fill array // after this the arrray has vector in it
4. Print the unorderd vecotr to the console// you did this correctly
5. Now is when you call the insert sort
6. Now the vector has the sorted values in it so you can just go through the vector and print the elements like you did in step 4.

Another thing is that your insertion sort method has 2 paramaters a filestream and a vector by reference. Why is that??

Are you supposed to read out of a file and insert the contents of the file into the vector in a sorted fashion??

If so your function doesnt do that yet. Your function takes an existing vector and reorders them which is good. But it doesnt do what i think it is supposed to.

If you wanted to read from a file and then sort and put into an array.
you would have to do all of that in the insertsort function which wouldnt be to bad.

You get what im saying
Ya i understand what your saying but I don't think thats necessary. All I need to do is print out whats in the file, sort that and print it sorted, then find the median. So if I dont need to put it into an array I would just print it out as i printed the values out right?

Also if i did need to put the sorted values in an array can you explain how?
Should i get rid of the ifstream& in_file in:
template <typename T>
double insertionSort(ifstream& in_file, vector<T>& a_values);
yea i dont think you need that. just get rid of that part and change it to void like this
void insertionSort(vector<T>& a_values);
because it has already been filled.
so read it in print it like you already have. then do
insertionSort(v);
then print out like you did the first time
I'm getting a little tired so I may be making a few trivial mistakes now but I need to at least get insertion sort working.

The error im getting now is:

error C2780: 'void insertionSort(std::vector<T> &)' : expects 1 arguments - 2 provided


1
2
3
4
5
6
7
8
9
insertionSort(fin, v);
cout << "Sorted values: ";
  
   vector<double>::iterator k;
   for (k = v.begin(); k != v.end(); k++)
   {
	   cout << *k << ",";
   }
   cout << endl;

Pages: 123... 5