Define an array of integers and enter an integer x, if x is not in this array, returns the position of the maximum number less than x and the position of the smallest number greater than X J;
If x is in this array, the position of the number in the array is returned.
Source:
#include <iostream>using namespace Std;void main () {int array[]={1,2,3,4,5,6,7,89,45,32,56,78,12,43,90,19};// 16 digits int X;int max=array[0];int min=array[0];int maxweizhi=0;int minweizhi=0;int weizhi=0;char end;cout<< " Please enter the value of positive integer x: "; cin>>x;for (int i=0;i<16;i++) {if (X==array[i]) {weizhi=i+1;break;} Else{if (array[i]>x) {if (max<=x| | Max>array[i])//The position of the smallest integer greater than x {maxweizhi=i+1;max=array[i];}} if (array[i]<x) {if (min>=x| | Min<array[i])//For the position of the largest integer less than x {minweizhi=i+1;min=array[i];}}} cout<< "array element is:"; for (int i=0;i<16;i++) {cout<<array[i]<< " ";} Cout<<endl;if (weizhi!=0) {cout<<x<< "in the array" <<weizhi<< "position" <<ENDL;} else{cout<<x<< "not in array" <<endl;cout<< "less than" <<x<< "the maximum number of locations is" <<minweizhi< <endl;cout<< the position of the minimum number greater than <<x<< is "<<maxweizhi<<endl"; cout<< "Please press any character enter end:"; cin>>end;}
Define an array of integers and enter an integer x, if x is not in this array, returns the position of the maximum number less than x and the position of the minimum number greater than X j