Westonci.ca is the Q&A platform that connects you with experts who provide accurate and detailed answers. Our platform provides a seamless experience for finding precise answers from a network of experienced professionals. Discover detailed answers to your questions from a wide network of experts on our comprehensive Q&A platform.

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"

Sagot :

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;

}

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"; }