Advertisement
Guest User

lab

a guest
Dec 3rd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.74 KB | None | 0 0
  1. /*  Remove all escape characters beginning with '\033' and ending with 'm' (ex: "\033[1;32m" )
  2.     if on any machine that does not support ANSI color output.. purpose is to output
  3.     in distinct color while debugging */
  4. // debug == uncomment to test values and debug
  5.  
  6. #include <iostream> // Standard input/output streaming through system
  7. #include <fstream>  // Allow file handling: Input/Output to and from file(s)
  8. #include <string>   // Utilize string variable
  9. #include <cstdlib>  // Required for Exit() function
  10. #include <iomanip>  // Input/Output Formatting library
  11. #include <cctype> // Using to change lowercase input to uppercase
  12. #define MAX 1000 // Pre-processor global macro to define MAX to have value of 1000
  13.  
  14. using namespace std;
  15.  
  16. // FUNCTION PROTOTYPE DECLARATION (FP Declaration)
  17. int getData(ifstream&,string [],string [],string [],double [], int [], double [], double [] );  // FP to store value for number of elements present in array specified
  18. int search(string [],int,string);  // function prototype to search for position of target item
  19. int search2(double[],int,double);  // FP to search for subscript of minimum_tuition
  20. void grab_index(string[], string[], string[], double[], int[], double[], double[],int,string);  // FP to extract and display all information of target item
  21. void grab_index2(string[],double[],int,double); // FP to extract and display university name and tuition for university w/ lowest tuition
  22. void compare_tuition(string[],double[],int,double); // FP to seek affordable universities
  23. double lowest_tuition(string[],double[],int,double&);
  24. void output(string [],string [],string [],double[], int[], double[], double[], int); // FP to output       
  25. void sortSelect(string [],string [], string [],int , double [],int [], double [],double []); // FP to sort the arrays
  26. double average_tuition(ifstream&,string [],string [],string [],double [], int [], double [], double [],int); // FP to compute the average tuition for all universities
  27. void signature(); // FP Signature
  28.  
  29. // FUNCTION MAIN
  30. int main(void)
  31. {
  32.     // declare variables
  33.     ifstream inFile;
  34.     string university[MAX];
  35.     string state[MAX];
  36.     string city[MAX];
  37.     string target;
  38.     double tuition[MAX];
  39.     int enrollment[MAX];
  40.     double retention[MAX];
  41.     double grad[MAX];
  42.     int number; int loc; double afford_tuition;
  43.     int low_val;
  44.     // open file
  45.     inFile.open("universities.txt");
  46.     // condition to check if file successfully opens/fails and alert user, otherwise continue
  47.     if (inFile.fail())
  48.     {
  49.         cerr << "No such file was found" << endl;
  50.         //      system("pause");
  51.         system("read -n 1 -s -p \"Press any key to continue...\"\n");
  52.         exit(100);
  53.     }
  54.     else
  55.         cout << "File successfully opened for read... continuing ..." << endl;
  56.     // count and store in variable number:  total elemements present in the arrays through calling function getData
  57.     number = getData(inFile, university, state, city, tuition, enrollment, retention, grad);
  58.     // output to file all original array and data information
  59.     output(university, state, city, tuition, enrollment, retention, grad, number);
  60.     // declare avg_tuition to store computed average tuition from calling function average_tuition
  61.     double avg_tuition=average_tuition(inFile,university,state,city,tuition,enrollment,retention,grad,number);
  62.     cout << setprecision(2) << fixed;
  63.     cout << "\n\033[1;31mAverage Tuition of all Universities:\033[0m\t" << "$ " << avg_tuition << endl;
  64.     double low_tuition=lowest_tuition(university,tuition,number,avg_tuition); // grab lowest tuition through calling function lowest_tuition
  65.     //debug:    cout << "LOW_TUITION VALUE:\t" << low_tuition << endl;
  66.     //  cout << "\033[1;32mUniversity with the lowest tuition:\t" << university[low_tuition] << endl;  // debug purpose (Testing) {for (int i=0; i < count;i++){    
  67.     cout << setprecision(2) << fixed;
  68.     //  cout << "\033[1;32mTuition:\033[0m\t\t" << "\033[1;33m" << "$ " << tuition[low_tuition] << "\033[0m" << endl;
  69.     system("read -n 1 -s -p \"Press any key to continue...\"\n");
  70.  
  71.     // prompt user for particular university(ies) in State input
  72.     cout << "\n\033[1;32mEnter the abbreviated State: \033[0m";
  73.     cin >> target;
  74.     for (int j=0; j < target.length(); ++j) { target[j]=toupper(target[j]); } // change lowercase to uppercase
  75.     // debug: cout << "Looking for Target **" << target << "**" << endl;
  76.  
  77.     // prompt for maximum affordable tuition to user
  78.     cout << "\n\033[1;32mEnter the affordable tuition: \033[0m";
  79.     cin >> afford_tuition;
  80.  
  81.     // call search function to find indexed position
  82.     loc = search(state, number, target);
  83.     low_val=search2(tuition,number,low_tuition);
  84.     compare_tuition(university,tuition,number,afford_tuition);  // call function compare_tution to find affordable universities with maximum tuition affordable
  85.  
  86.     // Condition test through IF-statement, search through index of array to find match target State
  87.     if (loc == -1)      // If not found alert non-existence of colleges in asked state
  88.         cout << "No colleges in " << target << " state in the list" << endl;
  89.     else{           // otherwise, if found, display all the information for found target by calling function grab_index
  90.         cout << "Found match for state, " << target << " ... continuing to write to file" << endl;
  91.         grab_index(university,state,city,tuition,enrollment,retention,grad,number,target);}
  92.         if (low_val == -1)
  93.             cout << "An error has occurred, no such information found" << endl;
  94.         else{
  95.             cout << "Found match for lowest tutition:\n" << endl;;
  96.             grab_index2(university,tuition,number,low_tuition);}
  97.             // call function sortSelect to sort universities by enrollment
  98.             sortSelect(university, state, city, number, tuition, enrollment, retention, grad);
  99.  
  100.             //Output each university's name, state, tuition, enrollment, retention, grad
  101.             //output(university, state, city, tuition, enrollment, retention, grad, number);
  102.             // for Windows machines: //system("pause");
  103.             system("read -n 1 -s -p \"Press any key to continue...\"\n");
  104.             signature();
  105.             return 0;
  106. }
  107. // FUNCTION GETDATA
  108. int getData(ifstream& inFile, string university[], string state[], string city[],double tuition[], int enrollment[], double retention[], double grad[])
  109. {
  110.     /*  Pre:        inFile - reference to input file
  111.         university[] - array of university names
  112.         state[] - array of abbreviated states
  113.         tuition[] - array of tuitions
  114.         enrollment[] - array of enrollment
  115.         retention[] =  array of retention
  116. Post:       number of items in stock
  117. Purpose:    read in information for all universities */
  118.     int count = 0;
  119.     double accept[MAX];
  120.     char ch='\n';
  121.     char blank=' ';
  122.     while (!inFile.eof() && count < MAX)
  123.     {
  124.         getline (inFile,university[count],ch);
  125.         inFile >> setw(2) >> state[count];
  126.         inFile.get(blank);
  127.         getline (inFile,city[count]);
  128.         inFile >> tuition[count] >> enrollment[count] >> accept[count] >> retention[count] >> grad[count];
  129.         inFile.get(ch);
  130.         inFile.get(ch);
  131.         count++;
  132.     }
  133.     return count;
  134. }
  135. // FUNCTION TUITION_AVERAGE
  136. double average_tuition(ifstream& inFile, string university[], string state[], string city[],double tuition[], int enrollment[], double retention[], double grad[],int num)
  137. {
  138.     /*  Pre:        inFile - input file handling passed by reference
  139.         university[] - array of name of universities
  140.         state[] - array of states
  141.         city[] - array of name of the cities
  142.         tuition[] - array of amount of tuition
  143.         enrollment[] - array of enrollment
  144.         retention[] - array of retention percentage rate
  145.         grad[] - array of percentage rate of graduates
  146.         num - number of total elements present passed by value from main - via function getData
  147. Post:       return the value for average tution
  148. Purpose:    compute the average tution for all the universities */
  149.     double total_tuition=0;
  150.     double tuition_avg=0;
  151.     for (int i=0;i<num;i++)
  152.         total_tuition += tuition[i];
  153.     tuition_avg=total_tuition/num;
  154.     // debug:        cout << "Tuition Average for all Universities:\t" << tuition_avg << endl;
  155.     return tuition_avg;
  156. }
  157. // FUNCTION SEARCH
  158. int search(string state[], int num, string target)
  159. {
  160.     /*  Pre:        state[] - array of states where target value is being searched within
  161.         num - number of values to be searched through
  162.         target - specific value being searched for
  163. Post:       index of where it was found or -1 if not found, if found, position of 'i' in array represents the location found at
  164. Purpose:    find location of value being searched for */
  165.     int loc = -1;  // -1 in array subscript will return false
  166.     for (int i = 0; i < num; i++)
  167.     {
  168.         if (state[i] == target)  // check within elements in array of states and see if any match is found
  169.             loc = i;  // return the position where match is found
  170.     }
  171.     return loc; // return index position
  172. }
  173. // FUNCTION SEARCH2
  174. int search2(double tuition[], int num, double min_val)
  175. {
  176.     /*      Pre:            tuition[] - array of tuitions where target value is being searched within
  177.         num - number of values to be searched through
  178.         min_val - target (minimum_tuition value) passed by value
  179. Post:           index of where it was found or -1 if not found, if found, position of 'y' in array represents the location found at
  180. return the subscript where found
  181. Purpose:        find location of value being searched for */
  182.     int loc = -1;  // -1 in array subscript will return false
  183.     //debug:    cout << "MIN_VAL:\t";
  184.     //debug:    cout << min_val << endl;
  185.     for (int y = 0; y < num; y++)
  186.     {  
  187.         if (tuition[y] == min_val)  // check within elements in array of tuition and see if any match is found
  188.             loc = y;  // return the position where match is found
  189.     }  
  190.     return loc; // return index position
  191. }
  192.  
  193. // FUNCTION GRAB_INDEX
  194. void grab_index(string university[], string state[], string city[], double tuition[], int enrollment[], double retention[], double grad[],int count,string target)
  195. {
  196.     /*      Pre:            university[] - array of name of universities
  197.         state[] - array of states
  198.         city[] - array of name of the cities
  199.         tuition[] - array of amount of tuition
  200.         enrollment[] - array of enrollment
  201.         retention[] - array of retention percentage rate
  202.         grad[] - array of percentage rate of graduates
  203.         target - value being searched (abbreciated state)
  204. Post:           N/A
  205. Purpose:        output all the information for matching state */
  206.  
  207.     ofstream fileOut("lab7_output_indexed.txt",ios::app);
  208.     // check success/fail of file opening for output stream, alert when fail and create new file..
  209.     if (fileOut.fail())
  210.     {
  211.         cerr << "File could not be open";
  212.         cout << "Creating a new file...";
  213.         system("touch lab7_output_indexed.txt"); // remove and manually create file if on Windows system
  214.     }
  215.     else
  216.         cout << "Successfully opened... continuing to write to file... " << endl;
  217.     char fill=' ';
  218.     fileOut << "\n\033[1;31mUniversities found in the state:\033[0m\t" << target << endl;
  219.     for (int i=0; i < count; i++){
  220.         if (state[i] == target){
  221.             fileOut << setprecision(2) << fixed;
  222.             fileOut << "\033[1;32mUniversity\t" << "\033[1;33m" << university[i] << endl;
  223.             fileOut << setw(5) << "\033[1;32mState\t\t" << "\033[1;33m" << state[i] << "\t\033[1;32mCity\t" << "\033[1;33m" << city[i] << endl;
  224.             fileOut << setw(8) << "\033[1;32mTuition\t\t" << setw(2) << fill << setw(10) << "Enrollment\t" << fill;
  225.             fileOut << setw(13) << "%Fresh Succeed\t" << setw(3) << fill << "%Graduate in six years" << "\033[1;33m\n";
  226.             fileOut << setw(8) << tuition[i] << setw(4) << fill << setw(16) << enrollment[i] << fill << setw(16) << (retention[i]*100);
  227.             fileOut << " %" << setw(19) << fill << right << (grad[i]*100) << " %" << endl;}
  228.     }
  229. }
  230. // FUNCTION GRAB_INDEX2
  231. void grab_index2(string university[], double tuition[],int count, double min_val)
  232. {
  233.     /*      Pre:            university[] - array of name of universities
  234.         tuition[] - array of amount of tuition
  235.         count - number of elements in array
  236. Post:           N/A
  237. Purpose:        display university name and tuition for lowest tuition matched */
  238.     for (int z=0;z < count;z++)
  239.     {
  240.         if (tuition[z] == min_val)
  241.         {
  242.             cout << "\nUniversity with the lowest tuition:\t";
  243.             cout << university[z] << "\n" << "Tuition:\t" << tuition[z] << endl;
  244.         }
  245.     }
  246. }
  247.  
  248. // FUNCTION COMPARE_TUITION
  249. void compare_tuition(string university[],double tuition[],int count, double target_tuition)
  250. {
  251.     /*      Pre:            university[] - array of name of universities
  252.         state[] - array of states
  253.         city[] - array of name of the cities
  254.         tuition[] - array of amount of tuition
  255.         enrollment[] - array of enrollment
  256.         retention[] - array of retention percentage rate
  257.         grad[] - array of percentage rate of graduates
  258.         target_tuition - specific amount of tuition passed as value to be searched in the array of university tuition
  259. Post:           return the list of affordable school names with their tuition
  260. Purpose:        search through list of tuitions list and find tuitions lower than target (affordable tuition) and extract related university name */
  261.  
  262.     cout << "\n\033[1;32mMaximum affordable tuition:\033[0m\t" << "$ " << target_tuition << endl;  // debug purpose (Testing)
  263.     cout << "\033[1;31mAffordable Universities:\n";
  264.     for (int i=0; i < count;i++){
  265.         if (tuition[i] <= target_tuition){
  266.             //debug:            cout << "\033[1;32mAffordable Universities:\n";
  267.             cout << "\033[1;33m" << university[i] << "\033[0m" << endl;}
  268.     }
  269. }
  270. // FUNCTION LOWEST_TUITION
  271. double lowest_tuition(string university[],double tuition[],int count,double& avg_tuition)
  272. {
  273.     /*      Pre:            university[] - array of name of universities
  274.         tuition[] - array of amount of tuition
  275.         count - total elements in array passed by value
  276.         avg_tuition - value passed by reference reoresenting average of all tuitions
  277. Post:           return the lowest tuition amount
  278. Purpose:        find the lowest tuition amount and university name */
  279.     double min_tuition=avg_tuition;
  280.     string min_university;
  281.     for (int i=0; i < count;i++){
  282.         if ((tuition[i] <= min_tuition) && (tuition[i] > 0)){
  283.             min_tuition=tuition[i];
  284.             min_university=university[i];
  285.         }
  286.         else
  287.             min_tuition=min_tuition;
  288.     }
  289.     //debug:        cout << "\033[1;32mUniversity with lowest tuition:\033[0m\t" << endl;
  290.     //debug:        cout << "\033[1;33m" << min_university << "\033[0m" << endl;
  291.     //debug     cout << "Min_Tuition=\t" << min_tuition << endl;
  292.     return min_tuition;
  293. }
  294. // FUNCTION OUTPUT
  295. void output(string university[], string state[], string city[], double tuition[], int enrollment[], double retention[], double grad[],int count)
  296. {
  297.     /*      Pre:            university[] - array of name of universities
  298.         state[] - array of states
  299.         city[] - array of name of the cities
  300.         tuition[] - array of amount of tuition
  301.         enrollment[] - array of enrollment
  302.         retention[] - array of retention percentage rate
  303.         grad[] - array of percentage rate of graduates
  304.         count - counter of total elements in array
  305. Post:           N/A
  306. Purpose:        output to file all data for each of the universities */
  307.     ofstream fileOut("lab7_output_original.txt",ios::app);
  308.     // check success/fail of file opening for output stream, alert when fail and create new file..
  309.     if (fileOut.fail())
  310.     {
  311.         cerr << "File could not be open";
  312.         cout << "Creating a new file...";
  313.         system("touch lab7_output_original.txt"); // remove and manually create file if on Windows system
  314.     }
  315.     else{
  316.         cout << "Successfully opened... continuing to write to file... " << endl;}
  317.     char fill=' ';
  318.     for (int i = 0; i < count; i++)
  319.     {
  320.         fileOut << setprecision(2) << fixed;
  321.         fileOut << "\033[1;32mUniversity\t" << "\033[1;33m" << university[i] << endl;
  322.         fileOut << setw(5) << "\033[1;32mState\t\t" << "\033[1;33m" << state[i] << "\t\033[1;32mCity\t" << "\033[1;33m" << city[i] << endl;
  323.         fileOut << setw(8) << "\033[1;32mTuition\t\t" << setw(2) << fill << setw(10) << "Enrollment\t" << fill;
  324.         fileOut << setw(13) << "%Fresh Succeed\t" << setw(3) << fill << "%Graduate in six years" << "\033[1;33m\n";
  325.         fileOut << setw(8) << tuition[i] << setw(4) << fill << setw(16) << enrollment[i] << fill << setw(16) << (retention[i]*100);
  326.         fileOut << " %" << setw(19) << fill << right << (grad[i]*100) << " %" << endl;
  327.     }
  328.     fileOut << "\033[1;31m*** END OF ORIGINAL ARRAY LIST ***\n\033[0m" << endl;
  329. }
  330. // FUNCTION SORTSELECT
  331. void sortSelect(string university[], string state[], string city[], int num, double tuition[], int enrollment[], double retention[], double grad[])
  332. {
  333.     /*      Pre:            university[] - array of name of universities
  334.         state[] - array of states
  335.         city[] - array of name of the cities
  336.         num - counter of total elements in array
  337.         tuition[] - array of amount of tuition
  338.         enrollment[] - array of enrollment
  339.         retention[] - array of retention percentage rate
  340.         grad[] - array of percentage rate of graduates
  341. Post:           N/A
  342. Purpose:        sort array by enrollment in ascending order */
  343.     int current; int walker;
  344.     int smallestIndex;
  345.     int tempI1;
  346.     double tempD1,tempD2,tempD3;
  347.     string tempS1,tempS2,tempS3;
  348.     char fill=' ';
  349.     for (current = 0; current < num - 1; current++)
  350.     {
  351.         smallestIndex = current;
  352.         for (walker = current; walker < num; walker++)
  353.         {
  354.             if (enrollment[walker] < enrollment[smallestIndex])
  355.                 smallestIndex = walker;
  356.         }
  357.         //Swap to position smallest at what is the current position
  358.         tempI1 = enrollment[current];
  359.         enrollment[current] = enrollment[smallestIndex];
  360.         enrollment[smallestIndex] = tempI1;
  361.         tempD1 = tuition[current];
  362.         tuition[current] = tuition[smallestIndex];
  363.         tuition[smallestIndex] = tempD1;
  364.         tempD2 = retention[current];
  365.         retention[current] = retention[smallestIndex];
  366.         retention[smallestIndex] = tempD2;
  367.         tempD3 = grad[current];
  368.         grad[current] = grad[smallestIndex];
  369.         grad[smallestIndex] = tempD3;
  370.         tempS1 = university[current];
  371.         university[current] = university[smallestIndex];
  372.         university[smallestIndex] = tempS1;
  373.         tempS2 = state[current];
  374.         state[current] = state[smallestIndex];
  375.         state[smallestIndex] = tempS2;
  376.         tempS3 = city[current];
  377.         city[current] = city[smallestIndex];
  378.         city[smallestIndex] = tempS3;
  379.     }
  380.     ofstream fileOut("lab7_output_sorted.txt",ios::app);
  381.     for (int x=0;x<num;x++)
  382.     {
  383.         fileOut << setprecision(2) << fixed;
  384.         fileOut << "\033[1;32mUniversity\t" << "\033[1;33m" << university[x] << endl;
  385.         fileOut << setw(5) << "\033[1;32mState\t\t" << "\033[1;33m" << state[x] << "\t\033[1;32mCity\t" << "\033[1;33m" << city[x] << endl;
  386.         fileOut << setw(8) << "\033[1;32mTuition\t\t" << setw(2) << fill << setw(10) << "Enrollment\t" << fill;
  387.         fileOut << setw(13) << "%Fresh Succeed\t" << setw(3) << fill << "%Graduate in six years" << "\033[1;33m\n";
  388.         fileOut << setw(8) << tuition[x] << setw(4) << fill << setw(16) << enrollment[x] << fill << setw(16) << (retention[x]*100);
  389.         fileOut << " %" << setw(19) << fill << right << (grad[x]*100) << " %" << endl;
  390.     }
  391.     fileOut << "\033[1;33m*** END OF SORTED ARRAY LIST ***\n\033[0m" << endl;
  392. }
  393.  
  394. // FUNCTION SIGNATURE
  395. void signature()
  396. {
  397.     string position1="Name:\t";
  398.     string position2="E-mail:\t\t";
  399.     string position3="Lab #:\t";
  400.     cout << "\n";
  401.     cout << position1+ "Julius Camata" << endl;
  402.     cout << position2+ "Email: " << endl;
  403.     cout << position3+ "Title - Choosing a University" << endl;
  404. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement