Question #319171

Write a program that takes an array ‘A’ of size 10 and an integer ‘x’ as input and searches the index of ‘x’ in the array. If ‘x’ is found, print the index. If ‘x’ is not found, then print -1. If ‘x’ is present two or more than two times in the array, then print the index of last occurrence of ‘x’.

Expert's answer



#include <iostream>


using namespace std;










int main(void){
	int A[10];
	int index=-1;
	int x;
	for(int i=0;i<10;i++){
		cout<<"Enter the number "<<(i+1)<<": ";
		cin>>A[i];
	}




	cout<<"Enter x: ";
	cin>>x;
	for(int i=0;i<10;i++){
		if(x==A[i]){
			index =i;
		}
	}




	cout<<"The index: "<<index<<"\n";




	system("pause");
	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!

LATEST TUTORIALS
APPROVED BY CLIENTS