Answer to Question #225304 in C++ for Ulando

Question #225304

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.


1
Expert's answer
2021-08-12T16:23:39-0400
#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;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS