Write a c++ program that creates a one dimensional array with of length5 integers from the keyboard into the array .your to perform sequential search for an integer in the array and insert a new intem in that location of the array.
#include <iostream>
#include <string>
using namespace std;
int sequentialSearch(int integers [], int n, int target){
for (int i = 0; i < n; i++)
if (integers [i] == target){
return i;
}
return -1;
}
int main()
{
int integers[5];
int target;
for(int i=0;i<5;i++){
cout<<"Enter integer value "<<(i+1)<<": ";
cin>>integers[i];
}
cout<<"Enter integer value to search: ";
cin>>target;
int index=sequentialSearch(integers,5,target);
if(index!=-1){
cout<<"Enter integer value to insert: ";
cin>>target;
integers[index]=target;
cout<<"\nArray:\n";
for(int i=0;i<5;i++){
cout<<integers[i]<<" ";
}
}else{
cout<<"The integer value does not exist in array.\n";
}
cin>>integers[0];
return 0;
}
Comments
Leave a comment