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