Question

Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...

Having issues using binary search on a pointer array.

1. Write 1000 random ints to file

2. Binary Search to find if number exists in element from file.

int main()

{

ArrayActions action;  

int count;

int userInput;

int num = 1000;

int array[num];

ofstream myFile ("/Users/chan/Desktop/LANEY_CIS27/Assignemtn2_CIS27/Assignemtn2_CIS27/File.txt");  

//Set srand with time to generate unique random numbers

srand((unsigned)time(0));  

//Format random num up to 999

for(count = 0; count < num; count++)

{

array[count] = rand() % 1000;

}

  

  

  

//Condition to check if file is open

if (myFile.is_open())

{

for (int i = 0 ; i < num; i++)

{

//Write all random values to myFile

action.bubblesort(array, num);

myFile << array[i] << "\n";

}

  

cout << "------Ouput is written to myFile--------\n";

  

  

//Close File

myFile.close();

  

}

else

cout<<"File doesn't exist.\n";

cout << "Enter input from menu: ";

cin >> userInput;

  

//int n = sizeof(array) / sizeof(array[0]);

int result = action.binarySearch(array, 0, num-1, userInput);

if (result == -1)

cout << "Element is not present in array\n";

else

cout << "Element is present at index " << result << endl;

int binarySearch(int *arr, int l, int r, int x)

{

while (l <= r) {

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

  

// Check if x is present at mid

if (arr[m] == x)

return 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;

}

  

// if we reach here, then element was

// not present

return -1;

}

  

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
class ArrayActions
{
   public:

int binarySearch(int *arr, int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) / 2;
// Check if x is present at mid
if (arr[m] == x)
return 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;
}
// if we reach here, then element was
// not present
return -1;
}
void bubblesort(int *a,int num)
{
   int i, j,temp;
   for(i = 0; i<num; i++) {
        for(j = i+1; j<num; j++){
       if(a[j] < a[i]) {
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
           }
            }
       }
   }
};
int main ()
{
ArrayActions action;
int count;

int userInput;

int num = 1000;

int array[num];

ofstream myFile ("D:/File.txt");

//Set srand with time to generate unique random numbers

srand((unsigned)time(0));

//Format random num up to 999

for(count = 0; count < num; count++)
{
array[count] = rand() % 1000
}

//Condition to check if file is open

if (myFile.is_open())

{

for (int i = 0 ; i < num; i++)

{

//Write all random values to myFile

action.bubblesort(array, num);

myFile << array[i] << "\n";

}

cout << "------Ouput is written to myFile--------\n";

//Close File

myFile.close();

}

else

cout<<"File doesn't exist.\n";

cout << "Enter input from menu: ";

cin >> userInput;

//int n = sizeof(array) / sizeof(array[0]);

int result = action.binarySearch(array, 0, num-1, userInput);

if (result == -1)

cout << "Element is not present in array\n";

else

cout << "Element is present at index " << result << endl;
}

Output:


Add a comment
Know the answer?
Add Answer to:
Having issues using binary search on a pointer array. 1. Write 1000 random ints to file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • An easier way to do the Binary Search part or a simplistic way for beginners. Not...

    An easier way to do the Binary Search part or a simplistic way for beginners. Not using such high code for the assignment in general also commenting on what each line does. COMMENT ON the code below .what does each part do for the program? and a better and more simple and basic way to do binary search and get same results. #include <iostream> #include <fstream> using namespace std; void swap(int *xp, int *yp) { int temp = *xp; *xp...

  • Hello, I need help with my code. The code needs to display random number from 1...

    Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() {   const int n = 50; int *arr,...

  • can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define...

    can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block {    std::string word;    int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) {    string filename="input.txt";    //declare array of struct word_block    word_block arr[SIZE];    int count = 0;    if (argc < 2)    {        cout << "Usage: " << argv[0] << "...

  • Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random...

    Q2. Consider the following C++ program that declares, allocates and fills in a1D array with random numbers between 0 and 100. The array is sent to a function that finds the indices of all items > 50. A new array is created and the indices are stored inside it. The size of the new arrays MUST BE the same as the number of items > 50. The function returns the new array which is then printed out by main. Here...

  • Java Im doing a binary search on an array and need to allow as many queries...

    Java Im doing a binary search on an array and need to allow as many queries as possible and cannot figure out how to. The output should read something like this. Enter a number. 3 3 is a prime number. Enter another number. 4 4 is not a prime number. Enter another number. 8 Current file not large enough for 8. Enter another number. -1 Bye. My code so far looks like this. public static void main(String[] args)    {...

  • Add reverse() method which reverses the content of array without using additional array. rotate(k) method which...

    Add reverse() method which reverses the content of array without using additional array. rotate(k) method which rotates left the content of array without using additional array by k elements. import java.util.*; * Implementation of the ADT List using a fixed-length array. * * if insert is successful returns 1, otherwise 0; * for successful insertion: * list should not be full and p should be valid. * * if delete is successful returns 1, otherwise 0; * for successful deletion:...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT