using c Write a loop that will search an array of int values

using c++ Write a loop that will search an array of int values, myArray, to determine if
it contains the integer value stored in a variable myInt and display the message
“Found” if it finds it.
Clearly comment your code. You may assume the myArray and myInt are both
declared and initialised.

Solution

#include <iostream>

using namespace std;

int main()
{
int size = 5,myInt;
int myArray[size];
cout<<\"Enter 5 numbers: \"<<endl;
for(int i=0; i<size; i++){
cin>>myArray[i];
}
cout << \"Enter the search element: \";
cin >> myInt;
for(int i=0; i<size; i++){
if(myArray[i] == myInt ){
cout<<\"Found\"<<endl;
break;
}
}

return 0;
}

output:

sh-4.3$ g++ -std=c++11 -o main *.cpp sh-4.3$ main

Enter 5 numbers:                                                                                                                                                                                                            

4 3 1 2 6                                                                                                                                                                                                                   

Enter the search element: 1                                                                                                                                                                                                 

Found

using c++ Write a loop that will search an array of int values, myArray, to determine if it contains the integer value stored in a variable myInt and display th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site