218k views
14 votes
Declare an array of 10 integers. Initialize the array with the following values: 000 101 202 303 404 505 606 707 808 909 Write a loop to search the array for a given number and tell the user if it was found. Do NOT write the entire program. Example: User input is 404 output is "found". User input is 246 output is "not found"

User Jdcantrell
by
5.2k points

1 Answer

8 votes

Answer:

In C++:

#include <iostream>

using namespace std;

int main(){

int myArray[10] = {000, 101, 202, 303, 404, 505, 606, 707, 808, 909};

int num;

bool found = false;

cout<<"Search for: ";

cin>>num;

for(int i = 0;i<10;i++){

if(myArray[i]==num){

found = true;

break;

}

}

if(found){ cout<<"found"; }

else { cout<<"not found"; }

return 0;

}

Step-by-step explanation:

This line initializes the array

int myArray[10] = {000, 101, 202, 303, 404, 505, 606, 707, 808, 909};

This line declares num as integer

int num;

This initializes boolean variable found to false

bool found = false;

This prompts user for input

cout<<"Search for: ";

This gets user input

cin>>num;

This iterates through the array

for(int i = 0;i<10;i++){

This checks if num is present in the array

if(myArray[i]==num){

If yes, found is updated to true

found = true;

And the loop is exited

break;

}

}

If found is true, print "found"

if(found){ cout<<"found"; }

If found is false, print "not found"

else { cout<<"not found"; }

User Jabb
by
5.2k points