Question: *** in C++ data structure, *** I need to create class file, main file, function(void)file. 1. program should read 1000 random integers from a text

*** in C++ data structure,

*** I need to create class file, main file, function(void)file.

1. program should read 1000 random integers from a text file (which I have already created as "1000rands.txt")

2. program should bubble sort the text file

3. use BINARY SEARCH (not linear search) to make function to enter a search value. (program should output the message "Enter the value you want to search" and then get input, then output the location of the value)

Please just fix my code to make it work as the instruction above.

my code for "void.cpp"

#include #include #include #include using namespace std; #include "3task2.h"

void task32::printmenu() //display the menu { cout << endl << endl; cout << "**************************************************" << endl; cout << "* < MENU > *" << endl; cout << "* Enter 1. Output all integers *" << endl; cout << "* Enter 2. Sort and then output values *" << endl; cout << "* Enter 3. Search value *" << endl; cout << "* Enter 4. Exit *" << endl; cout << "**************************************************" << endl; }

//method to print random 1000 integers #1 void task32::randInts(int *array, int size) { fstream file; cout << "Output all integers from a file 3task1.txt are: " << endl; file.open("1000rands.txt"); //read from a text file //check whether file is opened or not if(file.is_open()) { int num, count = 0; //read numbers from file to a variable while(file >> num) { cout << num << " "; count++; if(count % 20 == 0) cout << endl; } file.close(); //close the file } }

void task32::bubbleSort(int *arr, int n) { fstream file; file.open("1000rands.txt"); //read from a text file

int i, j, temp; //use the formula of bubble sort

//check whether file is opened or not if(file.is_open()) { //int count = 0; for (i = 0; i < 1000; i++) //to sort the values in order { for (j = i + 1; j < 1000; j++) { if (arr[j] < arr[i]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } //file.close(); } } //int count = 0; for(i = 0; i < 1000; i++) cout << arr[i] << " "; //count++; //if(count % 20 == 0) // cout << endl; } file.close(); }

void task32::binarySearch(int *arr) { int l, r, x, i; cout << "Enter the value you want to search: "; cin >> x;

while(l <= r) { int m = l + (r - l) / 2;

//check if x is present at mid if(arr[m] == x) x = m;

//if x greater, ignore left half if(arr[m] < x) l = m + 1;

//if x is smaller, ignore right half else r = m - 1; } }

--------------------------------------------------------------

my code for "3task2.h"

//2task22.h -- 2task22 class interface

#ifndef TASK32_H_

#define TASK32_H_

#include

class task32 //class declaration

{

int *array, size, n; //private by defualt

public:

void printmenu();

void randInts(int *array, int size);

void bubbleSort(int *arr, int n);

void binarySearch(int *arr);

}; //note semicolon at the end

#endif

----------------------------------------------------------------

my code for "main.cpp"

#include #include #include #include using namespace std; #include "3task2.h"

int main() //main function which will call all the methods above //and to execute it. { task32 obj; int inp, i, num = 1000; //1000 integers int array[num],result; int l, r, x;

//generate random numbers srand((unsigned)time(0)); //use srand with time for(i = 0; i < num; i++) { array[i] = (rand() % 1000) + 1; //integers between 1-1000 } obj.printmenu(); //print menu cout << " Enter Input: "; //tell user to enter input cin >> inp; //get input while(inp !=4) //this function will be repeated unless the user input 12 to exit. { switch(inp) { case 1: obj.randInts(array, num); //executes a method named randInts break;

case 2: obj.bubbleSort(array, num); //executes a method named bubbleSort break;

case 3: obj.task32::binarySearch(array); //executes a method named search break;

case 4: //it doesn't include any methods break; //since it is to exit the program default: cout << " Invalid Input!" << endl; //if other than 1-12 is entered, it prints error message cout << "Enter the number from the MENU."; //to tell user to enter valid option }

cout<< endl; obj.printmenu(); cout << " Enter Input: "; cin >> inp; }

cout << " *** Exit the Program ***" << endl << endl; return 0; }

please just fix my codes. I need to make Binary Search work. also for the bubbleSort, I want the program to change the line after displaying every 20 integers, but my code not work for that.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!