Answer to Question #149459 in C++ for Jochelle Buenaventura

Question #149459
Create a program using a one-dimensional array that accepts five input values from the keyboard. Then it should also accept a number to search. This number is to be searched if it is among the five input values. If it is found, display the message “Searched number is found!”, otherwise display “Search number is lost!”.
Sample input/output dialogue:
Enter five numbers:
10 15 20 7 8
Enter a number to search: 7
Search number is found!
1
Expert's answer
2020-12-08T05:42:56-0500
#include <iostream>

using namespace std;

int main()
{
    // we declare an one-dimensional array that accepts five element
    int arr[5];
    
    // we should enter the elements of array from keyboard
    cout << "Enter 5 numbers: " << endl;
    for (int i = 0; i < 5; i++) {
        cin >> arr[i];
    }
    cout << "The entered array is : " << endl;
    for (int i = 0; i < 5; i++) {
        cout << arr[i] << " ";
    }
    
    // we declare a variable for search the element in array
    int number_searched;
    cout << "\nEnter a number to search: " << endl;
    cin >> number_searched;
    int t = 0;
    for (int i = 0; i < 5; i++) {
        if (number_searched == arr[i]) {
            cout << "Searched number is found!";
            break;
        }
        t++;
    }
    if (t == 5) {
        cout << "Search number is lost!";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog