Simple C++ Program

Pages: 12345
Ok will do.
Now im getting a nuch of errors. Can you link what you did please? I dont think i screwed it up cause theres really not much to screw up so im not sure what i did wrong.
Never mind that, I got it. Sorry.
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
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

template <typename T>
void insertionSort(vector<T>& a_value) //vector is used as an implementation of a List
{
	int i, j, n = a_value.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>
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];

	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: ";

	
	for (int p = 0; p < v.size(); p++) 
	{
		cout << v[p] << ",";
	}
	cout << endl;


insertionSort(v);
cout << "Sorted values: ";
  
   
for (int k = 0; k < v.size(); k++)
   {
	   cout << v[k] << ",";
   }
   cout << endl;

} //end main

//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;
}*/


heres the errors

\main.cpp(21) : error C2065: 'target' : undeclared identifier
t\main.cpp(21) : fatal error C1903: unable to recover from previous error(s); stopping compilation

i think both of these will go away if you figure out what target was supposed to be. Because it is never created. I think after that you should be fine........Hopefully. Im gonna goto bed now. But ill check here when I get some time tomorrow
Sweet, insertionSort works! haha thats good. Now I have to work on the median. Thanks so much for your help!
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

// Vector and median
// December 3rd 2009

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

	vector<double> v;

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

	template <typename T>
	void insertionSort(vector<T>& v)
	{
		int i,j, n = v.size(); // index of last exchange
		bool exchangeOccurs= true;
		T temp;
		// iis the index of last element in the current sublist
		i= n-1;
		// continue the process until we make no exchanges or
		// we have made n-1 passes
		while (i> 0 && exchangeOccurs)
		{
			// assume no exchange occurs
			exchangeOccurs= false;
			// scan the sublistv[0] to v[i]
			for (j = 0; j < i; j++)
				// exchange a pair and assign true to exchangeOccurs
				if (v[j+1] < v[j])
				{
					temp = v[j];
					v[j] = v[j+1];
					v[j+1] = temp;
					exchangeOccurs= true;
				}
				// move idownward one element
				i--;
		}
	}

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



	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;


insertionSort(v);
cout << "Sorted values: ";
  
   vector<double>::iterator k;
   for (k = v.begin(); k != v.end(); k++)
   {
	   cout << *k << ",";
   }
   cout << endl;
	
	//cout << "Median = " << m << endl;

} //end main





//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;
}*/
HAHAHAHAHAHAAHA it works now. I changed target to temp and here is the output i got with the file i used


Enter a file name: file.txt

Values entered: 2.4,2.5,1.98,34.6,0.25,
Sorted values: 0.25,1.98,2.4,2.5,34.6,
Press any key to continue . . .



here is the working code
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 insertionSort(vector<T>& a_value) //vector is used as an implementation of a List
{
	int i, j, n = a_value.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 && temp < 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>
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];

	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: ";

	
	for (int p = 0; p < v.size(); p++) 
	{
		cout << v[p] << ",";
	}
	cout << endl;


insertionSort(v);
cout << "Sorted values: ";
  
   
for (int k = 0; k < v.size(); k++)
   {
	   cout << v[k] << ",";
   }
   cout << endl;
   system("pause");

} //end main

//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;
}*/
haha yay for stupid mistakes on my part.
im working on the median one now just for fun
haha cool, thanks.
So i think i have the correct function for this but for some reason I cant call it from double to double.......

error C2440: 'initializing' : cannot convert from 'double (__cdecl *)(std::vector<T>)' to 'double'
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
106
107
108
109
110
111
112
// Zack Kryzak
// Project 8
// Vector and median
// December 3rd 2009

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

template <typename T>
void insertionSort(vector<T>& a_value) //vector is used as an implementation of a List
{
	int i, j, n = a_value.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 && temp < 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>
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);
	}
}

template <typename T>
double median( vector<T> v) 
{
	int size = v.size();
	T median;
	T middleOne;
	T middleTwo;
	if( ( size % 2 ) == 0 )
	{
		middleOne = ( size / 2 );
		middleTwo = ( size / 2 ) + 1;
		median = ( v[middleOne - 1] + v[middleTwo - 1] ) / 2;
	} 
	else
	{
		middleOne = ( size / 2 ) + 1;
		median = v[middleOne - 1];
	}

	return median;
}
int main() // begin main
{
	vector<double> v;
	char filename[16];
	cout << "Enter a file name: ";
	cin >> filename;
	cout << endl;

	double m = median;

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

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


	for (int p = 0; p < v.size(); p++) 
	{
		cout << v[p] << " ,";
	}
	cout << endl;


	insertionSort(v);
	cout << "Sorted values: ";


	for (int k = 0; k < v.size(); k++)
	{
		cout << v[k] << " ,";
	}
	cout << endl;

	cout << "Median = " << m << endl;

} //end main


delete line 79 in your code

and change line 109 to this

cout << "Median = " << median(v) << endl;

im gonna post my code too with too examples with different numbers in the file

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

template <typename T>
void insertionSort(vector<T>& a_value) //vector is used as an implementation of a List
{
	int i, j, n = a_value.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 && temp < 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>
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);
	}
}
template <typename T>
double median(const vector<T> & v)
{
	int length = v.size();
	double median;
	if(length <= 1)
		return v[length];
	else if( length % 2 == 0 ) // even
	{
		median = (v[length/2] + v[(length/2)-1]) / 2; 
	}
	else // odd
	{
		median = v[length/2];
	}
	return median;
}

int main() // begin main
{
	vector<double> v;
	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,v);
	cout << "Values entered: ";

	
	for (int p = 0; p < v.size(); p++) 
	{
		cout << v[p] << ",";
	}
	cout << endl;


insertionSort(v);
cout << "Sorted values: ";
  
   
for (int k = 0; k < v.size(); k++)
   {
	   cout << v[k] << ",";
   }
   cout << endl;

   cout << "Median Value: " << median(v) << endl;
   system("pause");

} //end main
/*====================OUTPUT==========================
Enter a file name: file.txt

Values entered: 2.3,2,
Sorted values: 2,2.3,
Median Value: 2.15
Press any key to continue . . .

Enter a file name: file.txt

Values entered: 2.3,2,7,8,100,-2,3,2,2.5,19,3,
Sorted values: -2,2,2,2.3,2.5,3,3,7,8,19,100,
Median Value: 3
Press any key to continue . . .

Enter a file name: file.txt

Values entered: 1,2,3,4,5,6,7,8,9,
Sorted values: 1,2,3,4,5,6,7,8,9,
Median Value: 5
Press any key to continue . . .

====================OUTPUT==========================*/


I got bad news for you though. Try entering 1 number into the file or no numbers in the file and see what happens
Sorry, my Internet crashed. Looking at the problem now. Thanks.
Curious... why do you have a system pause? I've never used it and have not learned what it does yet.. i took it out and it works so thats why im asking.
apparently on this site it is frowned upon to use system("pause"): But I use it in order to pause the screen so I can see the results etc. If I dont have that the console screen dissappears so fast that I cant see the output.
Hmm. Thats wierd.. My console stays open..
So do you have any clue why it just errors out like that?

Btw i really appreciate you taking this much time to help.
I think the problem is from line 82 - 86 and 93 - 97. There is nothing protecting those from a one or no number statement in the text file.
Im guessing something like this before it executes the cout << "values are:"
1
2
3
4
		if (v <= 0)
			cout << "Values entered: " << a_value;
		cout << "Sorted values: " << a_value;
		cout << "Median = " << a_value;

Pages: 12345