Write a program that will enter integers into the array then search for an element inside the array. The program should tell the location where the element was found. The user will be the one to determine the number of integers to be entered into the array.
#include <iostream>
using namespace std;
int main(){
int Array1[100];
int number;
cout<<"Enter a number of integers: ";
cin>>number;
for(int i=0;i<number;i++){
cout<<"Enter integer value "<<(i+1)<<" for Array1: ";
cin>>Array1[i];
}
int index=-1;
int target;
cout<<"Enter a target: ";
cin>>target;
for(int i=0;i<number;i++){
if(target==Array1[i]){
index=i;
}
}
if(index!=-1){
cout<<"A value "<<target<<" exist at index "<<index<<"\n";
}else{
cout<<"A value "<<target<<" does not exist\n";
}
cin>>index;
return 0;
}
Comments
Leave a comment