Answer to Question #197820 in C++ for M Kamran

Question #197820

Write down the code to traverse the arrayuntill X located ?


1
Expert's answer
2021-05-24T07:42:46-0400
#include <iostream>

int main()
{
    const int ARRAY_SIZE = 10;
    int data[ARRAY_SIZE] = {1, 11, 43, 32, 63, 3, 19, 54, 17, 21};

    std::cout << "Please enter the number to locate: ";
    int number;
    std::cin >> number;

        if(!std::cin)
        {
            std::cout << "Bad input\n";
            return 1;
        }

    int position = -1;
    
    for(int i = 0; i < ARRAY_SIZE; ++i)
    {
        if(data[i] == number)
        {
            position = i;
            break;
        }
    }

    if(position < 0)
    {
        std::cout << "The number not found in the array\n";
    }
    else
    {
        std::cout << "The number is in the array at position " << position << "\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