write a c++ program that creates a one dimentional array with of length 5. Your program should read 5 integers from the keyboard into the array. You are to perform sequential search for an integer in the array and push the location of the element sought for on to a stack.
#include <iostream>
using namespace std;
int main()
{
int arr[5];
cout<<"Enter five numbers\n";
for(int i=0; i<5; i++){
cin>>arr[i];
}
cout<<"\nThe elements of the array are: \n";
for(int i=0; i<5; i++){
cout<<arr[i]<<"\t";
}
int n;
cout<<"\nEnter the number to find\n";
cin>>n;
int index;
for(int i=0; i<5; i++){
if(arr[i]== n){
index = i;
cout<<"The number is found at location "<<(i+1)<<endl;
}
else{
cout<<" "<<endl;
}
}
int n2;
cout<<"Enter the new number \n";
cin>>n2;
arr[index] = n2;
cout<<"The final array after the insertion of the new integer is: \n";
for(int i=0; i<5; i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Comments
Leave a comment