Knowee
Questions
Features
Study Tools

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]

🧐 Not the exact question you are looking for?Go ask a question

Solution

1. Break Down the Problem

To solve this problem, we need to:

  1. Implement a Linear Search algorithm to find the elements 45 and 78 in the array NUMBERS.
  2. Delete these elements from the array, ensuring that the remaining elements are shifted correctly.

2. Relevant Concepts

  • Linear Search: This involves iterating through each element of the array until the desired element is found.
  • Deleting an Element: This will involve shifting the elements of the array to close the gap created by the deletion.

3. Analysis and Detail

Implementation Steps:

  • We will define a function for Linear Search that returns the index of the found element or -1 if not found.
  • We will maintain an array size variable to help manage the current size of the array as we delete elements.
  • When deleting elements, we will shift the subsequent elements to the left.

4. Verify and Summarize

Finally, we will ensure that the remaining elements in the array are displayed correctly after deletion.

C++ Program

#include <iostream>
using namespace std;

const int MAX_SIZE = 10;

int linearSearch(int arr[], int size, int key) {
    for (int i = 0; i < size; i++) {
        if (arr[i] == key) {
            return i; // Found
        }
    }
    return -1; // Not found
}

void deleteElement(int arr[], int &size, int key) {
    int index = linearSearch(arr, size, key);
    if (index != -1) {
        for (int i = index; i < size - 1; i++) {
            arr[i] = arr[i + 1]; // Shift elements to the left
        }
        size--; // Decrease the size of the array
    }
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    int NUMBERS[MAX_SIZE] = {11, 22, 33, 36, 45, 52, 57, 60, 64, 78};
    int size = 10;

    cout << "Original array: ";
    printArray(NUMBERS, size);

    // Delete elements 45 and 78
    deleteElement(NUMBERS, size, 45);
    deleteElement(NUMBERS, size, 78);

    cout << "Array after deletion: ";
    printArray(NUMBERS, size);

    return 0;
}

Final Answer

The C++ program uses Linear Search to find and delete the elements 45 and 78 from the array NUMBERS. After execution, the modified array excluding the deleted elements will be displayed.

This problem has been solved

Similar Questions

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

Find the missing number in the following set of numbers: Options: a.) 327 b.) 386 c.) 438 d.) 469 Please give explanation​

The average of 50 numbers is 38. If two numbers namely 45 and 55 are discarded, the aver- age of the remaining numbers is :Select one:a. 32.5b. 37.5c. 35d. 36

write a tcl program to print all the numbers from 1 to 100.which is divisible by 4 and skip when its in 70's and 90's.

Write a c++ program which can delete a value specified by the user. After deletion make sure the tree remains min heap. you cannot use vectors

1/1

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.