Advertisement
jenniferleigh52

DataBubbleSort/Burgess

Oct 2nd, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. //Data Bubble Sort
  2. //Jennifer Burgess
  3. //CSCI 194
  4. //9/28/14
  5. /* This program will read data from a data file of ten records and print the original data.
  6. Then it will use the Bubble Sort algorithm, sort the data by year, and print the sorted data.
  7. Next it will prompt the user to enter a year, and use the Linear Search algorithm to find the record
  8. with that year and display the found record */
  9. #include "stdafx.h"
  10. #include <iostream>
  11. #include <fstream>
  12. #include <string>
  13. #include <iomanip>
  14.  
  15. using namespace std;
  16.  
  17. void displaySorted(string[], int[], double[], const int);
  18. void displayShow(string[], int[], double[], const int);
  19. int searchList(string[], int[], double*, const int);
  20.  
  21. int main()
  22. {
  23.     const int num_Records = 10;
  24.     string name[num_Records];
  25.     int year[num_Records];
  26.     double tuition[num_Records];
  27.     int count = 0;
  28.     ifstream inputfile;
  29.  
  30.  
  31.     inputfile.open("records.txt");
  32.     cout << "Reading data from the file." << endl;
  33.     cout << endl;
  34.  
  35.     while (count < num_Records && inputfile >> name[count] >> year[count] >> tuition[count])
  36.         count++;
  37.  
  38.     inputfile.close();
  39.     cout << "The unsorted values from file are:" << endl;
  40.  
  41.     cout << " Names  " << "Years  " << "Tuition   " << endl;
  42.     cout << endl;
  43.  
  44.     for (count = 0; count < num_Records; count++)
  45.         cout << setprecision(2) << fixed << setw(2) << (count + 1) << " " << name[count] << " " << year[count] << " " << tuition[count] << endl;
  46.     cout << endl;
  47.  
  48.     cout << "Now using Bubble sort to sort data" << endl;
  49.  
  50.     displaySorted(name, year, tuition, num_Records);
  51.     displayShow(name, year, tuition, num_Records);
  52.     searchList(name, year, tuition, num_Records);
  53.  
  54.  
  55.     return 0;
  56. }
  57.  
  58. void displaySorted(string name[], int year[], double tuition[], const int num_Records)
  59. {
  60.     bool swap;
  61.     string names;
  62.     int years;
  63.     double tuitions;
  64.  
  65.     do
  66.     {
  67.         swap = false;
  68.         for (int count = 0; count < (num_Records - 1); count++)
  69.         {
  70.             if (year[count]> year[count + 1])
  71.             {
  72.                 names = name[count];
  73.                 name[count] = name[count + 1];
  74.                 name[count + 1] = names;
  75.                 years = year[count];
  76.                 year[count] = year[count + 1];
  77.                 year[count + 1] = years;
  78.                 tuitions = tuition[count];
  79.                 tuition[count] = tuition[count + 1];
  80.                 tuition[count + 1] = tuitions;
  81.  
  82.                 swap = true;
  83.             }
  84.         }
  85.     } while (swap);
  86.  
  87.    
  88. }
  89.  
  90. void displayShow(string name[], int year[], double tuition[], const int num_Records)
  91. {
  92.     cout << "   Names   " << "  Years   " << "  Tuition   " << endl;
  93.     cout << endl;
  94.     for (int count = 0; count < num_Records; count++)
  95.         cout << setprecision(2) << fixed << setw(8) << name[count] << "   " << setw(6) << year[count] << "   " << setw(8) << tuition[count] << " " << endl;
  96.     cout << endl;
  97. }
  98.  
  99. int searchList(string name[], int year[], double tuition[], const int num_Records)
  100. {
  101.     int index = 0;
  102.     int position = -1;
  103.     int user_input = 0;
  104.     bool found = false;
  105.     cout << "Now using Linear Search to find year information entered below." << endl;
  106.     cout << endl;
  107.     cout << "Please enter a year: ";
  108.     cin >> user_input;
  109.     cout << endl;
  110.  
  111.     while (index < num_Records && !found)
  112.     {
  113.         if (year[index] == user_input)
  114.         {
  115.             found = true;
  116.             position = index;
  117.         }
  118.         index++;
  119.     }
  120.  
  121.     if (found == true)
  122.     {
  123.         cout << "Record found:" << endl;
  124.         cout << endl;
  125.         cout << *(name+position) << "  " << *(year+position) << "  ";
  126.         cout << fixed << showpoint << setprecision(2);
  127.         cout << "$" << *(tuition+position) << endl;
  128.         cout << endl;
  129.  
  130.     }
  131.     else
  132.         cout << "Sorry, Record not found" << endl;
  133.     cout << endl;
  134.  
  135.     return position;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement