Problem Statement:- Implement a program that takes input as an array and search for an element in the array and return the index of the element using pointers in C++.
Note:- Scroll horizontally to see the full line of code.
#include <iostream>
using namespace std;
int main()
{
int arr[5];
int *ptr=arr;
int i=0;
while(i<5){
cout<<"Enter the element: ";
cin>>*(ptr+i);
i++;
}
cout<<"Array is: ";
for(int i=0;i<5;i++){
cout<<arr[i]<<"\t";
}
cout<<endl;
int n;
cout<<"Enter the element to be searched: ";
cin>>n;
i=0;
int flag=0;
while(i<5){
if((*(ptr+i))==n){
cout<<"Element found at index: "<<i;
flag=1;
break;
}
i++;
}
if(flag==0){
cout<<"Element is not present in array.";
}
}
Comments
Post a Comment