Question

DONE IN C++ ONLY. C++ C++ This week you should get some experience with I reporting...

DONE IN C++ ONLY. C++ C++

This week you should get some experience with I reporting errors from functions using exceptions I writing templated functions Part 1

1. Implement the linear and binary search functions. Each function should take as a formal parameter an array of integers (of size less than 100) and returns (a) in the case of successful search – the position (index) of the target in the array (b) in the case of unsuccessful search – an exception with the message “Unsuccessful search” thrown from the function to the user.

2. The binary search works correctly only when the input is sorted. This requirement should be satisfied before the algorithm starts the search operation. Therefore if the input is unsorted the exception with the message “Unsorted input” should be thrown by the function. 3. Test the two search functions on the different inputs in the main function. Use const size to define the number of input

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

Solution:

linear search code:


#include <iostream>
using namespace std;

int linearSearch(int array[], int len, int searchElement)
{
   int i;
   for (i = 0; i < len; i++)
       if (array[i] == searchElement)
           return i;
   return -1;
}

int main(void)
{
   int array[] = { 13, 5, 12, 43, 54, 2, 9, 7, 6, 95 };
   int searchElement = 9;
   int len = sizeof(array) / sizeof(array[0]);
   int result = linearSearch(array, len, searchElement);
(result == -1)? cout<<"The search element is not found in the array"
               : cout<<"The element is present at index " <<result;
return 0;
}


Output:

binary search code:

#include<iostream> //i/o library
using namespace std;
int binarySearch(int array[], int left, int right, int search, int count)
{
  
   count++;
while (left <= right)
{
int mid = left + (right-left)/2;

if (array[mid] == search)
  
return mid;

if (array[mid] < search)
return binarySearch(array, left, mid-1, search, count);


else
return binarySearch(array, mid+1, right, search, count);
}

  
return count;
}


int main(){
   int count= 0;
   int array[]= {5, 12, 25, 32, 38, 46, 58, 62, 85, 90, 97, 105, 110};
   count= binarySearch(array, 0, 12, 32, count);
   cout<<count;
  

return 0; //This is to make sure that the program terminates
}

Output:

Hit the thumbs up if you liked the answer. :)

Add a comment
Know the answer?
Add Answer to:
DONE IN C++ ONLY. C++ C++ This week you should get some experience with I reporting...
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
  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • JH: Student Name: 4) Given the following array, do the following (show all the work). A...

    JH: Student Name: 4) Given the following array, do the following (show all the work). A (56, 89, 23, 58, 22, 11, 45, 48, 90) (a - 5 pts) Construct a hash table for the given array using the hash function H(K)- K mod 5 (b- 4 pts) Determine the average number of comparisons for a successful search using the hash table of (c -3 pts) What is the worst case number of comparisons for an unsuccessful search in the...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Can I get some help with this question for c++ if you can add some comments...

    Can I get some help with this question for c++ if you can add some comments too to help understand that will be much appreciated. Code: #include <cstdlib> #include <getopt.h> #include <iostream> #include <string> using namespace std; static long comparisons = 0; static long swaps = 0; void swap(int *a, int *b) {     // add code here } void selectionSort(int *first, int *last) {     // add code here } void insertionSort(int *first, int *last) {     // add code here }...

  • Hello I have an automata question could you help me? [1Points] Give a formal description of a Tu...

    Hello I have an automata question could you help me? [1Points] Give a formal description of a Turing Machine M  that takes two parameters: an integer and an array of integers and decides whether the given integer is an element of the array or not. You can assume that all the integers are between 0 and 9. The input string will be written on the tape of the Turing machine. The first square of the tape contains the integer, the...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • C++ Lab 1. Read in the contents of a text file up to a maximum of...

    C++ Lab 1. Read in the contents of a text file up to a maximum of 1024 words – you create your own input. When reading the file contents, you can discard words that are single characters to avoid symbols, special characters, etc. 2. Sort the words read in ascending order in an array (you are not allowed to use Vectors) using the Selection Sort algorithm implemented in its own function. 3. Search any item input by user in your...

  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • please check my answers if it wrong answer me a) (25 points) Suppose that you are...

    please check my answers if it wrong answer me a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...

  • 3. Write the function find sorted elt whose input is a vector sorted in increasing order...

    3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....

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
ADVERTISEMENT