Radom Array Menu
Mar 23, 2018 at 9:19pm UTC
Write your question here.
Hello im having trouble with this code im working this is the question: Create an array with 100 random numbers and write a program that performs the following operations on it This is what i got:
1. Linear Search
2. Binary Search
3. Selection Sort
4. Bubble Sort
You need to create a function for each operation. Create a menu that shows all the options
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctime>
#include <iomanip>
#define BUBBLE 100
using namespace std;
void displayMenu();
int getChoice();
int linearSearch();
int binarySearch();
int selectionSort();
int bubbleSort();
int main()
{
int choice;
char repeat;
do
{
displayMenu();
choice = getChoice();
if (choice == 1)
{
cout << "Area = " << linearSearch() << endl;
}
else if (choice == 2)
{
cout << "Area = " << binarySearch() << endl;
}
else if (choice == 3)
{ cout << "Area = " << selectionSort() << endl;
}
else if (choice == 4)
{
cout << "Area = " << bubbleSort() << endl;
}
else if (choice != 5)
{
cout << "Quit?" << endl; repeat = 'n' ;
break ;
}
cout << "Do you want to continue?(type y or n):" << " " ;
cin >> repeat;
}
while (repeat == 'y' || repeat == 'Y' );
return 0;
}
void displayMenu()
{
cout << "Please choose from the choices listed.\n" ;
cout << "1 - Linear Search \n" ; cout << "2 - Binary search\n" ;
cout << "3 - Selection Sort\n" ;
cout << "4 - Bubble Sort\n" ;
cout << "5 - Quit\n" ;
}
int getChoice()
{
int input;
cin >> input;
while (input < 1 || input > 4)
{
cout << "Invalid input. Enter an integer between 1 and 4: " ;
cin >> input;
}
return input;
}
int linearSearch(){
int linearSearch(int array[], int size, int searchValue)
{
for (int i = 0; i < size; i++)
{
if (searchValue == array[i])
{
return i;
}
}
return -1;
}
int main()
{
int num;
unsigned seed = time(0);
srand(seed);
for (int i = 0; i < 100; i ++)
{
num = rand() % 99 + 1; cout << num << endl;
}
return 0;
}
}
int binarySearch();
int binarySearch(int array[], int size, int searchValue)
{
int low = 0;
int high = size - 1;
int mid;
while (low <= high)
{
mid = (low + high) / 2;
if (searchValue == array[mid])
{
return mid;
}
else if (searchValue > array[mid])
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
return -1;
}
int main()
{
int num;
unsigned seed = time(0);
srand(seed); for (int i = 0; i < 10; i ++)
{
num = rand() % 15 + 1;
cout << num << endl;
}
return 0;
}
int selectionSort();
void selectionSort( int [], int );
int main()
{
const int SIZE = 100;
const int MAXRANGE = 100;
int sortThisArray[ SIZE ] = {0};
time_t t;
srand((unsigned ) time(&t));
for (int i=0; i<SIZE; i++)
{
sortThisArray[ i ] = (rand()%MAXRANGE)+1;
}
selectionSort(sortThisArray, SIZE );
{
for (int j=0; j< SIZE; j++)
cout << setw(4) << sortThisArray[j] << endl;
cout << endl;
}
return 0;
}
void selectionSort( int array[], int size )
{
int temp;
int i;
int j;
for (i=0; i< size; i++)
{
j = i;
while (j > 0 && (array [j-1] > array [j]))
{
temp = array [j];
array[j] = array [j-1];
array [j-1] = temp;
j--;
}
}
}
int bubbleSort();
int main()
{
int myArray[BUBBLE];
int i, j;
int temp = 0;
int num;
srand(time(NULL));
for (i = 0; i < BUBBLE; i ++)
{
num = rand() % BUBBLE + 1;
myArray[i] = num;
}
for (i = 0; i < BUBBLE; i++)
{
for (j = 0; j < BUBBLE-1; j++)
{
if (myArray[j] > myArray[j+1])
{
temp = myArray[j];
myArray[j] = myArray[j+1];
myArray[j+1] = temp;
}
}
}
for (i = 0; i < BUBBLE; i++)
{
printf("%d\n" ,myArray[i]);
}
system("PAUSE" );
return 0;
}
Mar 23, 2018 at 9:21pm UTC
I get an error when i run this code it tells me in line 76 and 210 my brackets are wrong function-definition is not allowed here before '{' token
error: expected '}' at end of input
Mar 23, 2018 at 9:43pm UTC
yeah there's two int main's also.
copy and paste error perhaps?
Mar 23, 2018 at 9:53pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
int linearSearch(){
int linearSearch(int array[], int size, int searchValue)
{
for (int i = 0; i < size; i++)
{
if (searchValue == array[i])
{
return i;
}
}
return -1;
}
int main()
{
int num;
unsigned seed = time(0);
srand(seed);
for (int i = 0; i < 100; i ++)
{
num = rand() % 99 + 1; cout << num << endl;
}
return 0;
}
}
Topic archived. No new replies allowed.