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
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. :)
DONE IN C++ ONLY. C++ C++ This week you should get some experience with I reporting...
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...
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 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 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...
I can not get this recursive binary search to wok for an edge case and it works but not for all, If you could look at it and try to find what is wrong to make this work for all edge cases. //Write a function that will return true if an element k is found in an array of integers A and false otherwise. The input to your //function is the array, its length 0 < n <= 10000, and...
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 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...
I
need help writing this code in C++
Proj11.cpp is provided as well as the randomdata.txt
thank you in advance!
Objectives: The main objectives of this project is to introduce you to recursion, and test your ability to work with some STL functionalities. A review of your knowledge on working with templates, dynamic data structures, as well as manipulating dynamic memory, classes, pointers and iostream to all extents, is also included. Description: For the entirety of this project, you will...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...
I only need the "functions" NOT the header file nor the main
implementation file JUST the implementations for the
functions
Please help, if its difficult to do the complete program I would
appreciate if you could do as much functions as you can especially
for the derived class.
I am a beginer so I am only using classes and pointers while
implementing everything using simple c++ commands
thank you in advanced
Design and implement two C++ classes to provide matrix...