Write a C++ program using arrays that will; Read in data from a data file of ten
ID: 3870207 • Letter: W
Question
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”.
NOTE: Use functions for your output, sort and search routines.
Original Data
Name Year Tuition
Aaron 2011 1582.38
Michael 2012 728.82
Joseph 1992 0
: : : : : : : : :
Sorted Data
Name Year Tuition
Joseph 1992 0.00
Michael 2011 1582.38
Aaron 2012 728.82
: : : : : : : : :
Enter year: 2011
Record found.
Aaron 2011 1582.38
___________________________________
This is what I have
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
typedef struct Student
{
string name;
int year;
double fee;
}Student;
void bubbleSort(Student Array[], int n)
{
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (Array[j].year > Array[j + 1].year)
{
Student temp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = temp;
}
}
int main()
{
ifstream fin;
fin.open("TuitionFee.txt");
Student Array[10];
int count = 0;
for (int i = 0; i < 10; i++)
{
if (fin.eof())
break;
fin >> Array[i].name;
fin >> Array[i].year;
fin >> Array[i].fee;
count++;
}
for (int i = 0; i < count; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
cout << endl << endl;
bubbleSort(Array, count);
for (int i = 0; i < count; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
}
_____________________________________________________________
all of it is correct! My teacher said I need this part sorted in a get function
int main()
{
ifstream fin;
fin.open("TuitionFee.txt");
Student Array[10];
int count = 0;
for (int i = 0; i < 10; i++)
{
if (fin.eof())
break;
fin >> Array[i].name;
fin >> Array[i].year;
fin >> Array[i].fee;
count++;
}
for (int i = 0; i < count; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
cout << endl << endl;
bubbleSort(Array, count);
for (int i = 0; i < count; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
}
__________________________________________________________
FYI I did this in Visual Studios in C++
Explanation / Answer
Here is the code for you:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
typedef struct Student
{
string name;
int year;
double fee;
}Student;
//NOTE: Use functions for your output, sort and search routines.
//Sorts the array of structures in increasing order based on year.
void bubbleSort(Student Array[], int n)
{
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (Array[j].year > Array[j + 1].year)
{
Student temp = Array[j];
Array[j] = Array[j + 1];
Array[j + 1] = temp;
}
}
//Prints the array of structures to screen.
void printRecords(Student Array[], int n)
{
for (int i = 0; i < n; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
}
//Linear Search which takes the array of structures, and the year as input, and return
//the index in which the year is found, or -1 if the year is not in the array.
int linearSearch(Student Array[], int n, int year)
{
for(int i = 0; i < n; i++)
if(Array[i].year == year)
return i;
return -1;
}
int main()
{
ifstream fin;
fin.open("TuitionFee.txt");
Student Array[10];
int count = 0;
for (int i = 0; i < 10; i++) //For a total of 10 records, reads the data from file to structure.
{
if (fin.eof())
break;
fin >> Array[i].name;
fin >> Array[i].year;
fin >> Array[i].fee;
count++;
}
printRecords(Array, count); //Prints the raw data i.e., in original order.
cout << endl << endl;
bubbleSort(Array, count); //Sorts the array.
printRecords(Array, count); //Prints the sorted data.
int year;
cout << "Enter the year to fetch the record: "; //Prompt the user to enter a year.
cin >> year;
//Use the Linear Search algorithm to find the record with that year
int index = linearSearch(Array, count, year);
//Then, display the found record, if record not found, print “Not Found”.
if(index == -1)
cout << "Not Found." << endl;
else
cout << Array[index].name << " " << Array[index].year << " " << Array[index].fee << endl;
}
/*_____________________________________________________________
all of it is correct! My teacher said I need this part sorted in a get function
int main()
{
ifstream fin;
fin.open("TuitionFee.txt");
Student Array[10];
int count = 0;
for (int i = 0; i < 10; i++)
{
if (fin.eof())
break;
fin >> Array[i].name;
fin >> Array[i].year;
fin >> Array[i].fee;
count++;
}
for (int i = 0; i < count; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
cout << endl << endl;
bubbleSort(Array, count);
for (int i = 0; i < count; i++)
cout << Array[i].name << " " << Array[i].year << " " << Array[i].fee << endl;
}
___*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.