Advertisement
squelch

a crappy program

Jun 2nd, 2013
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.73 KB | None | 0 0
  1. /* Program name:            evans_final.cpp - Final Project for CIS5 13SPR
  2.  * Program description:     Multiple problem definitions. See comments below.
  3.                             NOTE TO INSTRUCTOR: Array size is stored as the first integer
  4.                             in files generated by this program, in case you are using
  5.                             your own files.
  6.  * Programmer:              Bradley Evans
  7.  * Date:                    April 22nd, 2013
  8.  */
  9.  
  10.  
  11. #include <iostream>
  12. #include <fstream>
  13. #include <cstdlib>
  14. #include <string>
  15. using namespace std;
  16.  
  17. void displayMenu(char& choice); // Display menu to user. Collect his menu choice.
  18. void createArray(int*& arr, int& arraySize); // Generate a random array of user-specified size.
  19. void saveArrayToFile(int arr[], int& arraySize); // Save the array to a user-specified file name
  20. void displayArray(int*& arr, int& arraySize); // Display the current array in memory, if an array-generating function has been run.
  21. void loadArrayFromFile(int*& arr, int& arraySize); // Load a previously saved array.
  22. void sortArray(int*& arr, int& arraySize); // Sort an array in descending order.
  23. void awesome(); // a special surprise
  24.  
  25. int main()
  26. {
  27.  // Problem 01: Declare and Initialize Arrays / Variables
  28.     int     * arr = 0;              // Dynamic, pointer-based array.
  29.     int     arraySize = 0;          // Size of the array in memory.
  30.     char    choice;                 // Defined by user menu selection.
  31.     bool    exitCondition = false;  // User exit condition
  32.     bool    arrayInMemory = false;  // validates that one of the array-generating functions has been run.
  33.  
  34. // Introduction.
  35.     cout    << "Final Cumulative Project for Bradley Evans [2465773]" << endl
  36.             << "Developed for Riverside City College" << endl
  37.             << "for fulfillment of requirements in CIS-5 (Programming Logic in C++)" << endl
  38.             << "Instructor: Prof. Cristian Racataian" << endl
  39.             << "Compiled June 2nd, 2013." << endl << endl
  40.             << "NOTE TO USERS USING THEIR OWN FILES:" << endl
  41.             << "This program stores array size as the first integer" << endl
  42.             << "of new files." << endl << endl;
  43.  
  44.     do {
  45.         displayMenu( choice );
  46.         switch( choice )
  47.         {
  48.             case '1':
  49.                 cout << endl;
  50.                 createArray(arr, arraySize);
  51.                 arrayInMemory = true;
  52.                 cout << endl;
  53.                 break;
  54.             case '2':
  55.                 cout << endl;
  56.                 loadArrayFromFile(arr, arraySize);
  57.                 arrayInMemory = true;
  58.                 cout << endl;
  59.                 break;
  60.             case '3':
  61.                 cout << endl;
  62.                 if (arrayInMemory) 
  63.                 {
  64.                     sortArray(arr, arraySize);
  65.                 } else {
  66.                     cout << "No array in memory." << endl;
  67.                 }
  68.                 cout << endl;
  69.                 break; 
  70.             case '4':
  71.                 cout << endl;
  72.                 if (arrayInMemory) 
  73.                 {
  74.                     displayArray(arr, arraySize);
  75.                 } else {
  76.                     cout << "No array in memory." << endl;
  77.                 }
  78.                 cout << endl;
  79.                 break;
  80.             case '5':
  81.                 cout << endl;
  82.                 exitCondition = true;
  83.                 awesome();
  84.                 cout << "Thank you for using this program." << endl;
  85.                 system("PAUSE");
  86.         }
  87.     } while(!exitCondition);
  88.                
  89.     exit(0);
  90. }
  91.  
  92. void displayMenu ( char& choice ) // Display menu. 10 points.
  93. {
  94.     bool menuInputValid = false;
  95.  
  96.     do {
  97.         cout    << "Please select from the following options." << endl
  98.                 << "[1] - Create an array of random elements, save to a file." << endl
  99.                 << "[2] - Load a previous array from file." << endl
  100.                 << "[3] - Sort the array." << endl
  101.                 << "[4] - Display the array." << endl
  102.                 << "[5] - Exit the program and give Brad Evans an 'A'." << endl
  103.                 << "Selection: ";
  104.         cin     >> choice;
  105.         if ( choice == '1' ) // input validation
  106.         {
  107.             menuInputValid = true;
  108.         } else if (choice == '2' ) {
  109.             menuInputValid = true;
  110.         } else if (choice == '3' ) {
  111.             menuInputValid = true;
  112.         } else if (choice == '4' ) {
  113.             menuInputValid = true;
  114.         } else if (choice == '5' ) {
  115.             menuInputValid = true;
  116.         } else {
  117.             cout << endl << "Input invalid. Please try again." << endl;
  118.             menuInputValid = false;
  119.         }
  120.     } while ( !menuInputValid );
  121.    
  122. }
  123.  
  124. void createArray(int*& arr, int& arraySize) // Populate the array. 30 points.
  125. {
  126.     int i;         // iterator
  127.     int seed;      // random seed specified by user
  128.    
  129.     cout << "Specify the size of the array." << endl;
  130.     cin  >> arraySize;
  131.     cout << endl << "Please enter a random seed. A seed is any positive integer." << endl;
  132.     cin  >> seed;
  133.     cout << endl;
  134.     arr = new int[arraySize];
  135.     srand(seed);   // send the seed to the rand() function
  136.    
  137.     // Populate the array.
  138.     for ( i = 0; i < arraySize; i++ )
  139.     {
  140.         arr[i] = ( rand()%20 + 1 ) *5;
  141.     }
  142.     saveArrayToFile(arr, arraySize);
  143. }
  144.  
  145. void saveArrayToFile (int arr[], int& arraySize) // Save the array to a file.
  146. {
  147.     string filename; // User defined filename.
  148.     ofstream out;
  149.     int i;
  150.  
  151.     cout << "Please enter a filename to save the array: ";
  152.     cin  >> filename;
  153.    
  154.     out.open(filename);
  155.     out << arraySize << endl; // Make the first line of the file the arraySize for later reference.
  156.     for (i = 0; i < arraySize; i++)
  157.     {
  158.         out << arr[i] << endl;
  159.     }
  160.     cout << endl << "The file has been saved as " << filename << "." << endl
  161.          << "The array saved is: " << endl;
  162.          for ( i = 0; i < arraySize; i++ )
  163.          {
  164.             cout << "[" << arr[i] << "] ";
  165.          }
  166.     cout << endl;
  167.     out.close();
  168. }      
  169.  
  170. void displayArray(int*& arr, int& arraySize) // Display the array on a single line.
  171. {
  172.     int i = 0;
  173.    
  174.     cout << arr[0];
  175.     for (i = 1; i < arraySize; i++)
  176.     {
  177.         cout << " | " << arr[i];
  178.     }
  179.     cout << endl;
  180. }
  181.  
  182. void loadArrayFromFile(int*& arr, int& arraySize)
  183. {
  184.     int i = 0;
  185.     int infileNum;
  186.     string filename;
  187.     ifstream myArray;
  188.  
  189.     arraySize = 0;
  190.  
  191.     cout << "Please enter the file name of the array you wish to view: " << endl;
  192.     cin  >> filename;
  193.     // Open the user specified file, display contents.
  194.     myArray.open( filename );
  195.     myArray >> arraySize;
  196.     arr = new int [arraySize];
  197.     while ( !myArray.eof() )
  198.     {
  199.         myArray >> infileNum;
  200.         arr[i++] = infileNum;
  201.     }
  202.     myArray.close();
  203. }
  204.  
  205. void sortArray(int*& arr, int& arraySize) // Sort the array in descending order
  206. {
  207.     int     i,
  208.             j,
  209.             max,    // placeholder for largest value remaining
  210.             temp;   // for swapping
  211.     char    saveChoice; // user decision to save
  212.     // show you the current array
  213.     cout << "Old Array: " << endl;
  214.     displayArray(arr, arraySize);
  215.     // sort this sucker
  216.     for ( j = 0; j < arraySize; j++ )
  217.     {
  218.         for ( i = j+1; i < arraySize; i++ )
  219.         {
  220.             if (arr[i] > arr[j])
  221.             {
  222.                 max = arr[i];
  223.                 temp = arr[j];
  224.                 arr[j] = max;
  225.                 arr[i] = temp;
  226.             }
  227.         }
  228.     }
  229.     // show you the new array
  230.     cout << "Sorted Array: " << endl;
  231.     displayArray(arr, arraySize);
  232.     cout << endl;
  233.     // save the new array, in case you want to. Wasn't in the problem
  234.     // definition, but here you go anyway.
  235.     cout << "Would you like to save the new array? [y/n]";
  236.     cin >> saveChoice;
  237.     if (saveChoice == 'y' || saveChoice == 'Y')
  238.     {
  239.         saveArrayToFile(arr, arraySize);
  240.     }
  241. }
  242.  
  243. void awesome() // enjoy this masterpiece of artwork
  244. {
  245.    
  246. cout    << "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" << endl
  247.         << "MMMMMMMMMMMM===============MMMMMMMMMMMMM" << endl
  248.         << "MMMMMMM=========================MMMMMMMM" << endl
  249.         << "MMMM==MMMM=============7MMMM=======MMMMM" << endl
  250.         << "MMM=MM   MMM=========MM    MMM======MMMM" << endl
  251.         << "MMM=M    MMMM=======M     MMMMM=====MMMM" << endl
  252.         << "MM=M      M M=======M       M M======MMM" << endl
  253.         << "M==M        M=======M         M=======MM" << endl
  254.         << "M=====================================MM" << endl
  255.         << "M=====================================MM" << endl
  256.         << "M===MMMMMMMMMMMMMMMMMMMMMMMMMMM=======MM" << endl
  257.         << "MM====MOOOOOOOOOOOOOOOOOOOOOOOM======MMM" << endl
  258.         << "MMM====MMOOOOOOOOOOOOOOOOOOOOOM=====MMMM" << endl
  259.         << "MMMM====MMOOOOOOOO:::::::OOOOM=====MMMMM" << endl
  260.         << "MMMMMM===MMOOOOO:::::::::::MM====MMMMMMM" << endl
  261.         << "MMMMMMM====MMOO::::::::::MM=====MMMMMMMM" << endl
  262.         << "MMMMMMMMM=====MMM::::MMM======MMMMMMMMMM" << endl
  263.         << "MMMMMMMMMMMM===============MMMMMMMMMMMMM" << endl
  264.         << "MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM" << endl
  265.         << endl;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement