1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Jonathan Rogers
// 11/23/15
// Project 4
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <time.h>
#include <fstream>
using namespace std;
int randomizer(int random[]);
void selectionSort(int random[], int arraySize);
int binary_search(int random[], int low, int high, int search);
int main(){
const int arraySize = 100;
int random[arraySize];
randomizer(random);
system("pause");
}
void selectionSort(int array[], int arraySize)
{
for (int i = 0; i < arraySize; i++)
{
for (int j = i + 1; j < arraySize; j++)
{
if (array[j] > array[i])
{
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
int binary_search(int random[], int low, int high, int search)
{
int mid;
if (low > high)
return -1;
while (low <= high){
{
mid = (low + high) / 2;
if (search == random[mid])
return mid;
else
if (search < random[mid])
return binary_search(random, low, mid - 1, search);
else
return binary_search(random, mid + 1, high, search);
}
} // end if
return -1;
}// end binarySearch
int randomizer(int random[]){
srand((unsigned)time(NULL));
ofstream PRINT("P4Output.txt");
const int arraySize = 100; //size of the array
int high = 100; // high for random numbers
int low = -100; // low for random numbers
int size = 0;
int out = 0;
random[arraySize]; // giving the array random 100 elements
int first = 0;
int last = 0;
int index = 0;
int search = 0;
cout << endl << " RANDOM: " << endl;
cout << " *************************************************" << endl << endl << endl;
PRINT << endl << " RANDOM: " << endl;
PRINT << " ************************************************" << endl << endl << endl;
for (int i = 0; i < 100; i++){ // Start of loop to insert 100 random values into array
random[i] = rand() % (high - low + 1) + low; //Inserting random values into array
cout << setw(5) << random[i]; // outputs the random integer with spacing.
PRINT << setw(5) << random[i];
if ((i + 1) % 10 == 0) {
cout << endl << endl << endl; // end of print line for set of 10 values
PRINT << endl << endl << endl;
}// end of if statement
} // End for-loop
cout << " SELECTION SORT: " << endl;
cout << " *************************************************" << endl << endl << endl;
PRINT << " SELECTION SORT: " << endl;
PRINT << " ************************************************" << endl << endl << endl;
selectionSort(random, arraySize); // calling selection sort
for (int j = 0; j < arraySize; j++){ // for-loop
cout << setw(5) << random[j]; //outputs integers w/ spacing
if ((j + 1) % 10 == 0){ // sets rows of ten
cout << endl << endl;
}
}// end of for-loop
cout << " BINARY SEARCH " << endl;
cout << " *************************************************" << endl << endl << endl;
PRINT << " BINARY SEARCH " << endl;
PRINT << " *************************************************" << endl << endl << endl;
binary_search(random, -100, 100 - 1, search);
int choose = 0;
for (int i = 0; i < arraySize; i++){
while (choose != 101){
cout << "Enter number to search for: ";
cin >> choose;
if (choose == search){
cout << "Your number was found. " << endl;
}
else{
cout << " Your number was not found. " << endl;
}
}
}
return out;
}
|