Answer to Question #112548 in C++ for m zawar

Question #112548
Write a program that initializes an array of ten integers. It input an integer (key) from the user and search the value (key) in the array using sequential search.
1
Expert's answer
2020-04-30T13:36:44-0400
#include <iostream>
using namespace std;

int main()
{
    const int size = 10;
    int arr[size];
    // init array with sequence
    for(int i = 0; i < size; i++)
        arr[i] = i;
    
    // print array
    cout << "Array: ";
    for(int i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << "\n";
    
    int key, found = -1;
    cout << "Enter the searched value\n";
    cin >> key;
    for(int i = 0; i < size && found == -1; i++) {
        if( arr[i] == key ) {
            found = i;
        }
    }
    if( found == -1) {
        cout << "Can't find the given number!\n";
    } else {
        cout << "Key "<< key << " is at " << found + 1 << "'th place in the array\n";
    }
    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
APPROVED BY CLIENTS