Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // fileIO
- //
- // Created by Diego Gonzalez on 2/24/17.
- // Copyright © 2017 diego. All rights reserved.
- //
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include <string>
- #include <cstdlib>
- #include <fstream>
- using namespace std;
- /* Desc: Displays vector with no sorting
- * @param: list vector to be displayed
- */
- void displayUnsortedList( vector <int> vec){
- for(int i = 0; i< vec.size(); i ++){
- if( (i) % 10 == 0 && i > 0){
- cout << "\n";
- }
- cout << vec[i] << " ";
- }
- };
- /* Desc: Displays freq of values
- * @param: list vector to be displayed
- */
- void displayFreq(vector<int> list){
- int val[100];
- for(int i = 0; i <= 100; i++){
- val[i] = 0;
- }
- for(int i = 0; i< list.size(); i ++){
- val[list[i]]+=1;
- }
- for(int i = 1; i <= 100; i++){
- cout << i << " Frequency: " << val[i] << "\n";
- }
- }
- /* Dec: Sort and displays
- * @param: list vector to be displayed
- */
- vector<int> sortList(vector <int> list){
- for(int i = 0; i < list.size()-1; i++){
- for(int b = 0; b < list.size()-1-i; b++ ){
- if(list[b] > list[b + 1]){
- //swap
- int temp = list[b];
- list[b] = list[b+1];
- list[b+1] = temp;
- }
- }
- }
- return list;
- };
- /* Displays menu
- *
- */
- void displayMenu(){
- cout << "\n1) Display Unsorted List\n"
- << "2) Display Sorted List\n"
- << "3) Display Frequinces\n"
- << "4) Quit \n";
- }
- /*
- * @returns integer input
- */
- int checkUserInput(){
- int error = 0;
- int number;
- do{
- if(cin >> number ){
- error = 1;
- }else{
- cin.clear();
- cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- cout << "Bad Input try again :";
- }
- }while(error == 0);
- return number;
- };
- /*
- * Desc: get input within bounds
- * @param: upper upper bound
- * @param: lower lower bouned
- * @returns input within bounds
- */
- int checkIntInRange(int lower, int upper){
- int num = checkUserInput();
- while(num < lower || num > upper){
- cout << "Bad Input: ";
- num = checkUserInput();
- }
- return num;
- };
- int main(int argc, const char * argv[]) {
- fstream file;
- srand(time(NULL));
- int input;
- vector<int> values;
- file.open("values.txt", ios::in);
- if(file){
- while(file >> input){
- values.push_back(input);
- }
- file.close();
- }else{
- cout << "errror";
- }
- int exit = 0;
- do{
- displayMenu();
- int number = checkIntInRange(1,10);
- switch (number) {
- case 1:
- cout << "Unsorted list: \n";
- displayUnsortedList(values);
- break;
- case 2:
- cout << "Sorted list: \n";
- displayUnsortedList(sortList(values));
- break;
- case 3:
- displayFreq(values );
- break;
- case 4:
- exit = 1;
- break;
- }
- }while(exit == 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement