In: Computer Science
Create a program that has an array of length 100 which will automatically be filled with randomly generated numbers between 1 and 100 each time the program is run. Have the program ask the user to enter a number between 1 and 100. Check to see if that number is one of the values in the array. If it is display “We found your number XX at position YY in the array” If the number is not in the array tell the user that no match was found.
Program:
#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
//declare an array of size 100
int myArray[100];
int i,num,index;
bool found=false;
srand (time(NULL));
//fill the array with random numbers between 1 and
100
for(i=0;i<100;i++)
{
myArray[i]=rand() % 100 + 1;
}
//read a number from the user
cout<<"\nEnter a number between 1 and 100:
";
cin>>num;
//loop through the array
for(i=0;i<100;i++)
{
//check if array number matches
with the user number, if match found store the index and set found
to true
if(myArray[i]==num)
{
index=i;
found=true;
}
}
//if match found print the position where the number
is found in the array
if(found)
cout<<"\nWe found your number
"<<num<<" at position "<<(index+1)<<" in
the array.";
//else print match not found
else
cout<<"\nNo match
found.";
return 0;
}
Output: