Write a c++ program that create a one dimensional array with of length5.your program should read 5 integers from the keyboard into the array. Your to perform sequential search for an integer in the array and insert a new item in that location of the array.
#include <iostream>
using namespace std;
int main()
{
int arr[5];
cout<<"Enter the five integers of the array\n";
for(int i=0; i<5; i++){
cout<<"Enter integer number "<<(i+1)<<endl;
cin>>arr[i];
}
cout<<"\nThe elements of the array are: \n";
for(int i=0; i<5; i++){
cout<<arr[i]<<"\t";
}
int number;
cout<<"\nEnter the number to find\n";
cin>>number;
int index;
for(int i=0; i<5; i++){
if(arr[i]== number){
index = i;
cout<<"The number is found at location "<<(i+1)<<endl;
}
else{
cout<<" "<<endl;
}
}
int number2;
cout<<"Enter the new number \n";
cin>>number2;
arr[index] = number2;
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