Sorting with Arrays and Vectors help

So, for my current class we currently need to write two programs, both for sorting in different ways, one with Arrays and one with Vectors. I've been fighting it for hours and getting nowhere. I'll post the assignments and my mess of a attempt at the second one. Any help is much appreciated so I can get this done by Thursday.

Write a C++ program using arrays that will;
 Read in data from a data file of ten(10) records,
 Print the original data,
 Using the Bubble Sort algorithm, sort the data by year,
 Print the sorted data,
 Then, prompt the user to enter a year,
 Use the Linear Search algorithm to find the record with that year
 Then, display the found record, if record not found, print “Not Found”

Write a C++ program using vectors that will;
 Read in data from a data file of ten(10) records,
 Print the original data,
 Using the Selection Sort algorithm, sort the data by year,
 Print the sorted data,
 Prompt the user to enter a year,
 Use the Binary Search algorithm to find the record with that year
 Then, display the found record, if record not found, print “Not Found”.

Original Data
Name Year Tuition
David 2011 1582.38
Sylvester 2012 728.82
Ben 1992 0
: : : : : : : : :
Sorted Data
Name Year Tuition
Ben 1992 0.00
David 2011 1582.38
Sylvester 2012 728.82
: : : : : : : : :
Enter year: 2011
Record found.
David 2011 1582.38


Here's what I was trying for my Vector program. It was actually way better than this before, then I went and fulled with it and made it worse.
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
  #include<fstream>
#include<string>
#include<iomanip>
#include "Header.h"

void selectionSort(string name[10], int year[10], double tuition[10], int i)

{
	string tempS;

	int tempD;

	double tempI;

	for (int j = i - 1; j>0; j--)

	{
		for (int k = 0; k<j; k++)
		{
			if (year[k]>year[k + 1])
			{
				tempD = year[k];

				year[k] = year[k + 1];

				year[k + 1] = tempD;


				tempS = name[k];

				name[k] = name[k + 1];

				name[k + 1] = tempS;


				tempI = tuition[k];

				tuition[k] = tuition[k + 1];

				tuition[k + 1] = tempI;
			}
		}
	}

	void display(string name[10], int year[10], double tuition[10], int i);

	{
		for (int j = 0; j<i; j++)

		{

			cout << left << setw(20) << name[j] << right << setw(10) << year[j] << setw(10) << tuition[j] << endl;

		}



	}

	int binarySearch(string name[10], double scores[10], int year[10], int i, int y);



		for (int j = 0; j<i; j++)

			if (year[j] == y)

				return i;

}

int main()

{

	head();

	string name[10];

	double scores[10];

	int year[10];



	string fileName = "D:\\Program32.txt";


	ifstream in(fileName.c_str());

	int i = 0;

	in.close();

	cout << "Unsorted record :" << endl;
	head();
	cout << (name, year, tuition, i) << endl; 

	selectionSort(name, year, tuition, i);

	cout << "\nSorted record :" << endl;

	cout << (name, scores, year, i) << endl;


	int y;


	cout << "Enter the year to perform search: " << endl;

	cin >> y;

	i = binarySearch(name, scores, year, i, y);

	if (i)

		cout << "Record found: " << endl;

	else

		cout << "Record not found \n";

	return 0;
}
All you gave us was code and your assignment descriptions. What problem is your code having? What do you want it to do instead? Help us to help you.
Sorry, I get what you mean. It's my first time doing this with an assignment.

Basically anything to show me how this is done would help. This stuff just isn't clicking, but as far as what my problem was, I was getting an output (before I fooled around with the code and last what I had) but it wouldn't prompt to enter a year, even though it had been asking me to do so before I had the output working. Basically I couldn't get the output to display how I wanted, or give me the option to search for a specific record.

I hope this helps.

Edit: I just wanted to add that I feel like there is a 100x easier way to do this than I was, and if you think so please let me know. I'm just clueless here as of now.
Last edited on
I was getting an output

What output? Does this refer to the couts on lines 101 and 103?

it wouldn't prompt to enter a year

So you did not see the cout from line 109? Did the program pause for the input on line 111?
The output I am referring to is lines 95-103, where it should be displaying the names, years, and tuitions stored within the data file. I was able to get that to display.

But then, as you said, line 109 was not being displayed. It did not pause for the line 111 input.
I still really need help with this. Been trying to work on it and struggling a lot.
It kind of looks like you want to call the display function in line 97 and 103, but you forgot to add the function name before the arguments.

Also, in the display function, you call cout, so there is no reason to call this function as if it will return a value that should be written to the console (cout)

Try changing:
cout << (name, year, tuition, i) << endl;
to
display(name, year, tuition, i);
Why are the void display(...) and int binarySearch(...) functions contained inside of void selectionSort(...)?

Honestly I do not know how you managed to run this code. I struggled just to get it to compile. I had to add #include <iostream> , using namespace std;(which I am guessing is in header.h?), declare double tuition[10]; at the beginning of main, and I reindented everything so that I could make display and binarySearch into their own functions. Beyond that there are still semantic issues with the code. (In other words, just because I compiled it does not mean the program does what it is supposed to)

Also now is probably a good time to teach you about limitations of cout and the comma operator.

1. Limitations of cout: C++ cannot just take an array of values and print them. You will only see the memory address of the array be printed. You could add a function that loops across an array to do this for you though. (Hint: this is what your display function already does)

2. Comma operator: http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work
http://www.geeksforgeeks.org/comna-in-c-and-c/

Try adding these changes to see if you can reach the prompt for what year to search when the program is running.
Okay, I'll see what I can do with all of this.

I will say that even with what I got, if starting from the ground up seems like a better option I'm not at all opposed to that, too.
Topic archived. No new replies allowed.