Created
May 29, 2017 06:21
-
-
Save danielhep/4f4fd587372f263902b503575abfe564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////// | |
// Author: Daniel Heppner | |
// Class: CSCI140 | |
// Assignment: Lab 7 | |
// Program Description: Generate 100 random numbers, then allow the user to sort them in ascending or descending order. | |
// Input: Menu options, a-d or A-D | |
// Processing: For each item in the array, there is a loop that checks every other item in the array to see if it is greater or less than the item. If greater/less than, it is swapped. | |
// Output: Displays the array after processing has occured. | |
/////////////////////////////////////////////////////////// | |
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
void displayMenu(int randoms[]); | |
void output(int randoms[]); | |
void sortGreatToSmall(int randoms[]); | |
void sortSmallToGreat(int randoms[]); | |
using namespace std; | |
int main() { | |
int randoms[100]; | |
srand( (unsigned) time(NULL)); // Seed our random number gen | |
cout << "Generating random numbers..." << endl; | |
for(int i = 0; i < 100; i++) { | |
randoms[i] = ( rand() % 20 ) + 1; | |
} | |
output(randoms); | |
displayMenu(randoms); | |
} | |
// Display a menu | |
void displayMenu(int randoms[]) { | |
char choice; | |
cout << endl; | |
cout << "------------- Options -------------" << endl; | |
cout << "| A) Do not sort; output as is. |" << endl; | |
cout << "| B) Sort greatest to smallest |" << endl; | |
cout << "| C) Sort smallest to greatest |" << endl; | |
cout << "| D) Exit |" << endl; | |
cout << "-----------------------------------" << endl; | |
cout << "Enter your choice: "; | |
cin >> choice; | |
switch (choice) { | |
case 'A': | |
case 'a': output(randoms); | |
displayMenu(randoms); | |
break; | |
case 'B': | |
case 'b': sortGreatToSmall(randoms); | |
break; | |
case 'C': | |
case 'c': sortSmallToGreat(randoms); | |
break; | |
case 'D': | |
case 'd': break; | |
default: displayMenu(randoms); | |
} | |
} | |
// Display the array of random number | |
void output(int randoms[]) { | |
cout << endl; | |
for(int i = 0; i < 100; i++) { | |
cout << randoms[i] << " "; | |
} | |
cout << endl; | |
} | |
// Sort the array from greatest to smallest, then output it and display the menu. | |
void sortGreatToSmall(int randoms[]) { | |
int temp; | |
// iterate through each item | |
for(int i = 0; i < 100; i++) { | |
// iterate through every OTHER item | |
for(int j = i+1; j < 100; j++) { | |
if(randoms[i] < randoms[j]) { | |
// swap them | |
temp = randoms[i]; | |
randoms[i] = randoms[j]; | |
randoms[j] = temp; | |
} | |
} | |
} | |
output(randoms); | |
displayMenu(randoms); | |
} | |
// Sort the array from small to greatest, then output it and display the menu. | |
void sortSmallToGreat(int randoms[]) { | |
int temp; | |
// iterate through each item | |
for(int i = 0; i < 100; i++) { | |
// iterate through every OTHER item | |
for(int j = i+1; j < 100; j++) { | |
if(randoms[i] > randoms[j]) { | |
// swap them | |
temp = randoms[i]; | |
randoms[i] = randoms[j]; | |
randoms[j] = temp; | |
} | |
} | |
} | |
output(randoms); | |
displayMenu(randoms); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment