Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Data Bubble Sort
- //Jennifer Burgess
- //CSCI 194
- //9/28/14
- /* This program will read data from a data file of ten records and print the original data.
- Then it will use the Bubble Sort algorithm, sort the data by year, and print the sorted data.
- Next it will prompt the user to enter a year, and use the Linear Search algorithm to find the record
- with that year and display the found record */
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- void displaySorted(string[], int[], double[], const int);
- void displayShow(string[], int[], double[], const int);
- int searchList(string[], int[], double*, const int);
- int main()
- {
- const int num_Records = 10;
- string name[num_Records];
- int year[num_Records];
- double tuition[num_Records];
- int count = 0;
- ifstream inputfile;
- inputfile.open("records.txt");
- cout << "Reading data from the file." << endl;
- cout << endl;
- while (count < num_Records && inputfile >> name[count] >> year[count] >> tuition[count])
- count++;
- inputfile.close();
- cout << "The unsorted values from file are:" << endl;
- cout << " Names " << "Years " << "Tuition " << endl;
- cout << endl;
- for (count = 0; count < num_Records; count++)
- cout << setprecision(2) << fixed << setw(2) << (count + 1) << " " << name[count] << " " << year[count] << " " << tuition[count] << endl;
- cout << endl;
- cout << "Now using Bubble sort to sort data" << endl;
- displaySorted(name, year, tuition, num_Records);
- displayShow(name, year, tuition, num_Records);
- searchList(name, year, tuition, num_Records);
- return 0;
- }
- void displaySorted(string name[], int year[], double tuition[], const int num_Records)
- {
- bool swap;
- string names;
- int years;
- double tuitions;
- do
- {
- swap = false;
- for (int count = 0; count < (num_Records - 1); count++)
- {
- if (year[count]> year[count + 1])
- {
- names = name[count];
- name[count] = name[count + 1];
- name[count + 1] = names;
- years = year[count];
- year[count] = year[count + 1];
- year[count + 1] = years;
- tuitions = tuition[count];
- tuition[count] = tuition[count + 1];
- tuition[count + 1] = tuitions;
- swap = true;
- }
- }
- } while (swap);
- }
- void displayShow(string name[], int year[], double tuition[], const int num_Records)
- {
- cout << " Names " << " Years " << " Tuition " << endl;
- cout << endl;
- for (int count = 0; count < num_Records; count++)
- cout << setprecision(2) << fixed << setw(8) << name[count] << " " << setw(6) << year[count] << " " << setw(8) << tuition[count] << " " << endl;
- cout << endl;
- }
- int searchList(string name[], int year[], double tuition[], const int num_Records)
- {
- int index = 0;
- int position = -1;
- int user_input = 0;
- bool found = false;
- cout << "Now using Linear Search to find year information entered below." << endl;
- cout << endl;
- cout << "Please enter a year: ";
- cin >> user_input;
- cout << endl;
- while (index < num_Records && !found)
- {
- if (year[index] == user_input)
- {
- found = true;
- position = index;
- }
- index++;
- }
- if (found == true)
- {
- cout << "Record found:" << endl;
- cout << endl;
- cout << *(name+position) << " " << *(year+position) << " ";
- cout << fixed << showpoint << setprecision(2);
- cout << "$" << *(tuition+position) << endl;
- cout << endl;
- }
- else
- cout << "Sorry, Record not found" << endl;
- cout << endl;
- return position;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement