Question

In: Computer Science

Search Benchmarks No Pointers or Vectors can be used for this program. Do not use exit,...

Search Benchmarks

No Pointers or Vectors can be used for this program. Do not use exit, break, swap, or sort functions from C++.

Write a program to generate an array of 100 random three-digit integers (100 – 999). The program should display the array values (formatted as columns, ten items per line).

Once the numbers are generated, the user should be prompted for the search item. The program should use a linear search to find the item. The program should keep track of the number of comparisons made and whether or not the item was found.

The program should then sort the array (using the sort algorithm of your choice), display the sorted array, and then repeat the search using a binary search. Again, the program should report the number of comparisons made (not the spot it is found).

This program will make use of at least five functions:

void displayArray(int [], int) passing the array and the size of the array

void randomizeArray(int [], int) passing the array and the size of the array

void sortArray(int [], int) passing the array and the size of the array

void sequentialSearch(int [], int, int) passing the array, the size of the array, and the search item

void binarySearch(int [], int, int) passing the array, the size of the array, and the search item

The two search functions have return type void, as they must provide two different pieces of information (the number of comparisons made, and whether or not the item was found). Therefore these functions will use cout statements to provide this information.

Must use functions, no C-string function, no <bits/stdc++.h> . Only 1 return per function or main program.

Solutions

Expert Solution

#include <iostream>

using namespace std;

void displayArray(int arr[] , int len) {
 for(int i=0;i<100;i++){
    if(i%10==0)
      cout<<endl;
    cout<<arr[i]<<" ";
  }
}

void randomizeArray(int arr[], int len){
   int num;
  for(int i=0;i<100;i++){
     num = (rand() % (100 - 999 + 1)) + 100;
    arr[i] = num;
  }
}

//sorting using insertion sort
void sortArray(int arr[], int n){ 
    int i, key, j;  
    for (i = 1; i < n; i++) 
    {  
        key = arr[i];  
        j = i - 1;  
        while (j >= 0 && arr[j] > key) 
        {  
            arr[j + 1] = arr[j];  
            j = j - 1;  
        }  
        arr[j + 1] = key;  
    }  
}  


void sequentialSearch(int arr[], int size, int num){
  int num_comp = 0;
  for(int i=0;i<size;i++){
    num_comp++;
    if(arr[i]==num){
      cout<<"Element FOUND!!!. Number of comparisons = "<<num_comp<<endl;
      return;
    }
  }
  cout<<"Element NOT FOUND!!! Number of comparisons = "<<size<<endl;
}
int BS_comp_count = 0;
int binarySearch(int arr[], int l, int r, int x) 
{ 
    if (r >= l) { 
      BS_comp_count++;
        int mid = l + (r - l) / 2; 
        if (arr[mid] == x){
            return mid; 
        }
        if (arr[mid] > x) 
            return binarySearch(arr, l, mid - 1, x); 
        return binarySearch(arr, mid + 1, r, x); 
    } 
    return -1; 
} 
void binarySearch(int arr[],int size, int num){
  int index = binarySearch(arr, 0, 99, num);
  if(index!=-1){
    cout<<"Element FOUND!!!. Number of comparisons = "<<BS_comp_count<<endl;
  }
  else
   cout<<"Element NOT FOUND!!! Number of comparisons = "<<BS_comp_count<<endl;
}
int main() {
  int arr[100];
  cout<<"generating random array...";
  randomizeArray(arr,100);
  cout<<"\nRandomly generated array is:\n";
  displayArray(arr,100);
  cout<<"\nEnter number to search: ";
  int num;
  cin>>num;
  cout<<"\n---Search using Sequential search---"<<endl;
  sequentialSearch(arr, 100, num);
  cout<<"Sorted array is:\n";
  sortArray(arr, 100);
  displayArray(arr, 100);
  cout<<"\n---Search using Binary search---"<<endl;
  binarySearch(arr, 100, num);
}

Sample output for the above code is as following:

Sample Output 1:

Sample Output 2:

Note - any doubt/query do ask in comment section and please LIKE the solution


Related Solutions

Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following is a partial implementation of Set class. You are required to enhance and implement the following missing functions from the implementation: A) UNION B) reset C) intersection D) difference A SAMPLE driver program : int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2); Set s4 = s1.intersection(s2); Set s5 = s1.difference(s2); s3.print("s3"); s4.print("s4");...
What are some benchmarks that can be used in determining program effectiveness in the case of...
What are some benchmarks that can be used in determining program effectiveness in the case of a high school? Please be specific. Please help me out with this question. I need assistance on this. See what is written below in order to answer the question. Gathering Evidential Data Performance auditors can gather evidential data in a variety of ways, enabling them to assess whether the control system is working effectively (causing the entity to achieve its goals and objectives), to...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
Why do managers examine benchmarks? How can benchmarks be applied to some of the analytic techniques?
Why do managers examine benchmarks? How can benchmarks be applied to some of the analytic techniques?
If unit-costing is used to assess productivity, can we still use productivity benchmarks or standards? 300...
If unit-costing is used to assess productivity, can we still use productivity benchmarks or standards? 300 words
What characteristics or benchmarks can be used to assess the business value of a company such...
What characteristics or benchmarks can be used to assess the business value of a company such as Twitter?
Please use C++ for Mac (XCode) In this program, a user can search for a name...
Please use C++ for Mac (XCode) In this program, a user can search for a name within a list of names. Here are the steps the program will implement: 1. Reads the names from the file names.txt into a vector that stores strings. 2. Sorts the names in alphabetical order using selection sort. 3. The program then prompts the user to enter a name. 4. The program then uses binary search to see if the name is in the list....
Please do not use vectors or any previously posted code Write a C++ program which reads...
Please do not use vectors or any previously posted code Write a C++ program which reads in a list of process names and integer times from stdin/cin and simulates round-robin CPU scheduling on the list. The input is a list of lines each consisting of a process name and an integer time, e.g. ProcessA 4 ProcessB 10 Read line by line until an end-of-transmission (^d) is encountered. You should read the list and represent it in a linked list data...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Write a version of the binary search algorithm that can be used to search a string...
Write a version of the binary search algorithm that can be used to search a string vector object. Also, write a program to test your algorithm. (Use the selection sort algorithm you developed in Programming Exercise 12 to sort the vector.) Your program should prompt the user to input a series of strings, ending the input stream with zzz. The program should then prompt the user to search for a specific string in the list. If the string is found...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT