Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with c++ program!!! I keep getting errors!! Program: Write, compile,

ID: 3697221 • Letter: P

Question

Please help with c++ program!!! I keep getting errors!!

Program:

Write, compile, and run a C++ program that generates 200 random integers in between 1 to 200. Some of the randomly generated integers may be repeated. Next sort the randomly generated 200 integers using the Bubble sort algorithm. Store the sorted numbers in an array as well as an output data file, outSort.txt. Then prompt the user to enter an integer in between 1 to 200 (inclusive) out of the keyboard as a search key. Next find out the location of the search key in the sorted array (from previous step) using the binary search, and then repeat the search using the sequential search algorithms. The result returned by the search may vary for repeated elements inside the sorted array. This is acceptable.

Display the results returned by the binary search and the sequential search algorithms to the standard console, and also write out the results to the output data file. If the search target does not exist among the generated integers, display a message “Key does not exist in the array.” Implement the Bubble sort algorithm, Binary Search and Sequential Search algorithms as three separate functions that will be called from the function main().

Explanation / Answer

#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

void generateRandomNos(int arr[], int length) {

   srand(time(NULL));
   for (int i = 0; i < length; i++) {
       arr[i] = rand() % length + 1;
   }

}

void bubblesort(int arr[], int length) {
   int temp;
   for (int p = 0; p <= length - 1; p++) // Loop for Pass
           {
       for (int j = 0; j <= length - 1; j++) {
           if (arr[j] > arr[j + 1]) {
               temp = arr[j]; // Interchange Values
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
           }
       }

   }
}

int binarySearch(int key, int arr[], int length) {
   int beg = 1;
   int end = length;

   int mid = (beg + end) / 2; // Find Mid Location of Array

   while (beg <= end && arr[mid] != key) // Compare Item and Value of Mid
   {
       if (arr[mid] < key)
           beg = mid + 1;
       else
           end = mid - 1;

       mid = (beg + end) / 2;
   }

   if (arr[mid] == key) {
       return mid;
   } else {
       return -1;
   }
}
void printArr(int arr[], int length) {
   for (int i = 0; i < length; i++) {
       cout << arr[i] << " ";
   }
   cout << endl;
}

int main() {

   int arr[200];
   generateRandomNos(arr, 200);
   cout << " Unsorted array: " << endl;
   printArr(arr, 200);
   bubblesort(arr, 200);
   cout << " Bubble sorted array: " << endl;
   printArr(arr, 200);

   int searchKey[] = { 145, 32, 78 };
   int index = -1;
   for (int i = 0; i < 3; i++) {
       index = binarySearch(searchKey[i], arr, 200);

       if (index != -1)
           cout << "Key : "<<searchKey[i]<<" found at index : " << index << endl;
       else
           cout << "Key:"<<searchKey[i]<<" does not exist in the array." << endl;
   }
   return 1;
}


--output-----------------

Unsorted array:
189 195 196 174 41 137 145 89 22 125 83 12 102 177 55 19 77 124 84 200 187 40 68 191 133 121 146 51 70 120 139 21 130 30 74 170 35 101 143 148 56 75 96 132 2 136 107 134 73 155 114 109 7 183 192 81 131 132 195 46 88 91 186 32 87 122 185 139 195 105 75 103 3 108 191 44 93 114 62 178 129 181 52 77 140 171 165 7 126 84 106 174 174 69 60 183 172 21 200 27 47 94 100 170 144 174 41 101 183 182 89 104 85 179 91 151 162 99 124 112 39 42 88 42 62 198 58 173 81 63 64 122 17 116 35 91 8 146 86 62 176 86 116 191 93 148 62 60 81 136 197 36 175 86 53 66 194 190 67 127 158 74 97 22 63 186 120 172 105 48 77 136 40 72 190 111 58 106 6 75 137 115 135 84 53 152 88 79 105 60 168 167 100 196 26 41 43 26 50 13
Bubble sorted array:
2 3 6 7 7 8 12 13 17 19 21 21 22 22 26 26 27 30 32 35 35 36 39 40 40 41 41 41 42 42 43 44 46 47 48 50 51 52 53 53 55 56 58 58 60 60 60 62 62 62 62 63 63 64 66 67 68 69 70 72 73 74 74 75 75 75 77 77 77 79 81 81 81 83 84 84 84 85 86 86 86 87 88 88 88 89 89 91 91 91 93 93 94 96 97 99 100 100 101 101 102 103 104 105 105 105 106 106 107 108 109 111 112 114 114 115 116 116 120 120 121 122 122 124 124 125 126 127 129 130 131 132 132 133 134 135 136 136 136 137 137 139 139 140 143 144 145 146 146 148 148 151 152 155 158 162 165 167 168 170 170 171 172 172 173 174 174 174 174 175 176 177 178 179 181 182 183 183 183 185 186 186 187 189 190 190 191 191 191 192 194 195 195 195 196 196 197 198 200 200
Key : 145 found at index : 146
Key : 32 found at index : 18
Key:78 does not exist in the array.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote