Write a program in cpp which uses Linear Search to search the elements 45,and 78 and delete it. NUMBERS=[11,22,33,36,45,52,57,60,64,78] without using vectors.
Question
Write a program in cpp which uses Linear Search to search the elements 45,and 78 and delete it. NUMBERS=[11,22,33,36,45,52,57,60,64,78] without using vectors.
Solution
#include <iostream>
using namespace std;
// Function to perform linear search and delete the element if found
void linearSearchAndDelete(int arr[], int &size, int element) {
int index = -1;
// Perform linear search
for (int i = 0; i < size; i++) {
if (arr[i] == element) {
index = i;
break;
}
}
// If element is found, delete it
if (index != -1) {
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--; // Reduce the size of the array
}
}
int main() {
int NUMBERS[] = {11, 22, 33, 36, 45, 52, 57, 60, 64, 78};
int size = sizeof(NUMBERS) / sizeof(NUMBERS[0]);
// Elements to be searched and deleted
int elementsToDelete[] = {45, 78};
int elementsSize = sizeof(elementsToDelete) / sizeof(elementsToDelete[0]);
// Perform search and delete for each element
for (int i = 0; i < elementsSize; i++) {
linearSearchAndDelete(NUMBERS, size, elementsToDelete[i]);
}
// Print the updated array
for (int i = 0; i < size; i++) {
cout << NUMBERS[i] << " ";
}
return 0;
}
Similar Questions
Given numbers = [[1, 2], 3, [4, 5]]numbers = [[1, 2], 3, [4, 5]], give an expression using indexing (with positive indices) on numbersnumbers that evaluates to 44.
NumbersWhen a number is divided by 357 the remainder is 39. If that number is divided by 17, the remainder will be :Options30511
In a busy airport, passengers are holding tickets with unique numbers. Write a program using the linear search algorithm to check if a given ticket number exists in the array of ticket numbers. If the ticket number is found, print a congratulatory message along with the position where it's found. If the ticket number is not found, encourage the passenger with a message for better luck on the next attempt.Input format :The first line of input consists of an integer N, representing the number of tickets.The second line consists of N space-separated integers, representing the ticket numbers.The third line consists of an integer, representing the ticket number to search.Output format :If the ticket number is found, print "Congratulations! Ticket number X found at position Y!", where X is the ticket number and Y is the position (position starts from 1).Otherwise, print "Better luck next time! Ticket number X not found!", where X is the ticket number.Refer to the sample output for formatting specifications.Code constraints :1 ≤ N ≤ 1010 ≤ ticket number ≤ 1000Sample test cases :Input 1 :5123 456 789 321 654321Output 1 :Congratulations! Ticket number 321 found at position 4!Input 2 :6741 456 982 364 789 258369Output 2 :Better luck next time! Ticket number 369 not found!Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.
Program to implement linear search.
Write a program to read and store n+ve numbers in array and search a number k provided by the user using a. Linear Searchb. Binary Search
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.