Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Program name: evans_final.cpp - Final Project for CIS5 13SPR
- * Program description: Multiple problem definitions. See comments below.
- NOTE TO INSTRUCTOR: Array size is stored as the first integer
- in files generated by this program, in case you are using
- your own files.
- * Programmer: Bradley Evans
- * Date: April 22nd, 2013
- */
- #include <iostream>
- #include <fstream>
- #include <cstdlib>
- #include <string>
- using namespace std;
- void displayMenu(char& choice); // Display menu to user. Collect his menu choice.
- void createArray(int*& arr, int& arraySize); // Generate a random array of user-specified size.
- void saveArrayToFile(int arr[], int& arraySize); // Save the array to a user-specified file name
- void displayArray(int*& arr, int& arraySize); // Display the current array in memory, if an array-generating function has been run.
- void loadArrayFromFile(int*& arr, int& arraySize); // Load a previously saved array.
- void sortArray(int*& arr, int& arraySize); // Sort an array in descending order.
- void awesome(); // a special surprise
- int main()
- {
- // Problem 01: Declare and Initialize Arrays / Variables
- int * arr = 0; // Dynamic, pointer-based array.
- int arraySize = 0; // Size of the array in memory.
- char choice; // Defined by user menu selection.
- bool exitCondition = false; // User exit condition
- bool arrayInMemory = false; // validates that one of the array-generating functions has been run.
- // Introduction.
- cout << "Final Cumulative Project for Bradley Evans [2465773]" << endl
- << "Developed for Riverside City College" << endl
- << "for fulfillment of requirements in CIS-5 (Programming Logic in C++)" << endl
- << "Instructor: Prof. Cristian Racataian" << endl
- << "Compiled June 2nd, 2013." << endl << endl
- << "NOTE TO USERS USING THEIR OWN FILES:" << endl
- << "This program stores array size as the first integer" << endl
- << "of new files." << endl << endl;
- do {
- displayMenu( choice );
- switch( choice )
- {
- case '1':
- cout << endl;
- createArray(arr, arraySize);
- arrayInMemory = true;
- cout << endl;
- break;
- case '2':
- cout << endl;
- loadArrayFromFile(arr, arraySize);
- arrayInMemory = true;
- cout << endl;
- break;
- case '3':
- cout << endl;
- if (arrayInMemory)
- {
- sortArray(arr, arraySize);
- } else {
- cout << "No array in memory." << endl;
- }
- cout << endl;
- break;
- case '4':
- cout << endl;
- if (arrayInMemory)
- {
- displayArray(arr, arraySize);
- } else {
- cout << "No array in memory." << endl;
- }
- cout << endl;
- break;
- case '5':
- cout << endl;
- exitCondition = true;
- awesome();
- cout << "Thank you for using this program." << endl;
- system("PAUSE");
- }
- } while(!exitCondition);
- exit(0);
- }
- void displayMenu ( char& choice ) // Display menu. 10 points.
- {
- bool menuInputValid = false;
- do {
- cout << "Please select from the following options." << endl
- << "[1] - Create an array of random elements, save to a file." << endl
- << "[2] - Load a previous array from file." << endl
- << "[3] - Sort the array." << endl
- << "[4] - Display the array." << endl
- << "[5] - Exit the program and give Brad Evans an 'A'." << endl
- << "Selection: ";
- cin >> choice;
- if ( choice == '1' ) // input validation
- {
- menuInputValid = true;
- } else if (choice == '2' ) {
- menuInputValid = true;
- } else if (choice == '3' ) {
- menuInputValid = true;
- } else if (choice == '4' ) {
- menuInputValid = true;
- } else if (choice == '5' ) {
- menuInputValid = true;
- } else {
- cout << endl << "Input invalid. Please try again." << endl;
- menuInputValid = false;
- }
- } while ( !menuInputValid );
- }
- void createArray(int*& arr, int& arraySize) // Populate the array. 30 points.
- {
- int i; // iterator
- int seed; // random seed specified by user
- cout << "Specify the size of the array." << endl;
- cin >> arraySize;
- cout << endl << "Please enter a random seed. A seed is any positive integer." << endl;
- cin >> seed;
- cout << endl;
- arr = new int[arraySize];
- srand(seed); // send the seed to the rand() function
- // Populate the array.
- for ( i = 0; i < arraySize; i++ )
- {
- arr[i] = ( rand()%20 + 1 ) *5;
- }
- saveArrayToFile(arr, arraySize);
- }
- void saveArrayToFile (int arr[], int& arraySize) // Save the array to a file.
- {
- string filename; // User defined filename.
- ofstream out;
- int i;
- cout << "Please enter a filename to save the array: ";
- cin >> filename;
- out.open(filename);
- out << arraySize << endl; // Make the first line of the file the arraySize for later reference.
- for (i = 0; i < arraySize; i++)
- {
- out << arr[i] << endl;
- }
- cout << endl << "The file has been saved as " << filename << "." << endl
- << "The array saved is: " << endl;
- for ( i = 0; i < arraySize; i++ )
- {
- cout << "[" << arr[i] << "] ";
- }
- cout << endl;
- out.close();
- }
- void displayArray(int*& arr, int& arraySize) // Display the array on a single line.
- {
- int i = 0;
- cout << arr[0];
- for (i = 1; i < arraySize; i++)
- {
- cout << " | " << arr[i];
- }
- cout << endl;
- }
- void loadArrayFromFile(int*& arr, int& arraySize)
- {
- int i = 0;
- int infileNum;
- string filename;
- ifstream myArray;
- arraySize = 0;
- cout << "Please enter the file name of the array you wish to view: " << endl;
- cin >> filename;
- // Open the user specified file, display contents.
- myArray.open( filename );
- myArray >> arraySize;
- arr = new int [arraySize];
- while ( !myArray.eof() )
- {
- myArray >> infileNum;
- arr[i++] = infileNum;
- }
- myArray.close();
- }
- void sortArray(int*& arr, int& arraySize) // Sort the array in descending order
- {
- int i,
- j,
- max, // placeholder for largest value remaining
- temp; // for swapping
- char saveChoice; // user decision to save
- // show you the current array
- cout << "Old Array: " << endl;
- displayArray(arr, arraySize);
- // sort this sucker
- for ( j = 0; j < arraySize; j++ )
- {
- for ( i = j+1; i < arraySize; i++ )
- {
- if (arr[i] > arr[j])
- {
- max = arr[i];
- temp = arr[j];
- arr[j] = max;
- arr[i] = temp;
- }
- }
- }
- // show you the new array
- cout << "Sorted Array: " << endl;
- displayArray(arr, arraySize);
- cout << endl;
- // save the new array, in case you want to. Wasn't in the problem
- // definition, but here you go anyway.
- cout << "Would you like to save the new array? [y/n]";
- cin >> saveChoice;
- if (saveChoice == 'y' || saveChoice == 'Y')
- {
- saveArrayToFile(arr, arraySize);
- }
- }
- void awesome() // enjoy this masterpiece of artwork
- {
- cout << "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" << endl
- << "MMMMMMMMMMMM===============MMMMMMMMMMMMM" << endl
- << "MMMMMMM=========================MMMMMMMM" << endl
- << "MMMM==MMMM=============7MMMM=======MMMMM" << endl
- << "MMM=MM MMM=========MM MMM======MMMM" << endl
- << "MMM=M MMMM=======M MMMMM=====MMMM" << endl
- << "MM=M M M=======M M M======MMM" << endl
- << "M==M M=======M M=======MM" << endl
- << "M=====================================MM" << endl
- << "M=====================================MM" << endl
- << "M===MMMMMMMMMMMMMMMMMMMMMMMMMMM=======MM" << endl
- << "MM====MOOOOOOOOOOOOOOOOOOOOOOOM======MMM" << endl
- << "MMM====MMOOOOOOOOOOOOOOOOOOOOOM=====MMMM" << endl
- << "MMMM====MMOOOOOOOO:::::::OOOOM=====MMMMM" << endl
- << "MMMMMM===MMOOOOO:::::::::::MM====MMMMMMM" << endl
- << "MMMMMMM====MMOO::::::::::MM=====MMMMMMMM" << endl
- << "MMMMMMMMM=====MMM::::MMM======MMMMMMMMMM" << endl
- << "MMMMMMMMMMMM===============MMMMMMMMMMMMM" << endl
- << "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" << endl
- << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement