Question

Given an array containing 1500 integers, write a C++ program that searches through the array for...

Given an array containing 1500 integers, write a C++ program that searches through the array for a user given value. (Hint: Use Sequential and Binary Search algorithms).

Homework Answers

Answer #1

Below is the complete C++ code. If you face any difficulty while understanding the code, Please let me know in the comments.

Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

// Function for sequential/linear search
void sequentialSearch(int arr[], int n, int target) {
  
for (int i=0;i<n;i++)
if(arr[i] == target) {
std::cout << "Sequential Search: Value found!" << std::endl;
return;
}
  
// Print message if target value not found in the array
std::cout << "Sequential Search: Value not found!" << std::endl;
}


// Function to perform binary Search
void binarySearch (int arr[], int n, int target) {
// Store the left and right indices
int left = 0, right = n - 1;
  
// Loop until left <= right
while (left <= right) {
int mid = left + (right - left) / 2;
  
// Check if target is present at mid
if (arr[mid] == target) {
std::cout << "Binary Search: Value found!" << std::endl;
return;
}
  
// If target greater, ignore left half
if (arr[mid] < target)
left = mid + 1;
  
// If target is smaller, ignore right half
else
right = mid - 1;
}
  
std::cout << "Binary Search: Value not found!" << std::endl;
}


int main() {
  
// TODO: Add all your 1500 numbers into array
int arr[] = {2,3,14,5,6};
int n = sizeof(arr)/sizeof(arr[0]);
int target = 5;
  
// Call sequentialSearch function
sequentialSearch(arr, n, target);
  
// Sort the array
sort(arr, arr+n);
  
// Call binarySearch function
binarySearch(arr, n, target);
  
return 0;
}

Screenshots:

Output:

Know the answer?
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
Similar Questions
Write a recursive method that performs binary search on an array Arr and searches for a...
Write a recursive method that performs binary search on an array Arr and searches for a key k. Apply the recursive method to the following array: 13 | 20 | 22 | 26 | 30 | 41 | 50 | 60 While the key you are search for is 50. Show the stack trace.
C Programming: Write a function that takes in an array of integers and an integer containing...
C Programming: Write a function that takes in an array of integers and an integer containing the count of elements, then have it returns the sum of all the even values inside the array.
//C++ Given a string S containing a number of substrings separated by spaces. Write a program...
//C++ Given a string S containing a number of substrings separated by spaces. Write a program to do the following: Count the number of substrings in S and print S with extra spaces deleted. Define a dynamic array of pointers (a pointer for each substring in S). Save a pointer to each substring S into the array. Hint: use strtok function.
Its a c++ task. Write a program that reads 10 integers from the user into an...
Its a c++ task. Write a program that reads 10 integers from the user into an array and uses a function arrayMinimum that accepts an integer array a along with its size arraySize as parameters and returns the smallest array element. The program then outputs the result (the smallest array element).
Write a program in c++ to Convert an array of inches to an array of centimeters....
Write a program in c++ to Convert an array of inches to an array of centimeters. The program should contain a function called inchesTOcm with three parameters (inches array that contains the values in inches, cm array to save the result in, and an integer variable that defines the length of the array). In the main function: 1. Define an array (inches) of length 3. 2. Initialize the array by asking the user to input the values of its elements....
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose...
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose input is taken by user, the array is passed to a functions named i.e. smallest(int A[20) and largest(int B[20]) to determine minimum and maximum values respectively. Also create a function named modify(int *p) which modifies the value at the index given by user.
JAVA Write a program that has two functions in which the user chooses which one to...
JAVA Write a program that has two functions in which the user chooses which one to perform; 1. reads in a CSV file of integers into an array and insert it into a binary search tree class 2. deserializes an object and inserts it into a binary search tree class the tree class must be in a separate class from the CSV file read and deserialize object load
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
Write a program that uses an array of integers initialized to whatever values you wish. Write...
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10) In Java
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...