In: Computer Science
C++
Write a program to declare an array of double of size 25, ask
the user to input all array elements from the keyboard, then write
a function named out_of_order that will test this array for the
condition a[0] >= a[1] >= a[2] >= ... > a[24]
The function returns a -1 if the elements are not out of order,
otherwise it returns the index of the first element that is out of
order. Explain what you do to avoid out of bounds array access.
#source code:
#include<iostream>
using namespace std;
int out_of_order(double a[]){
int i=0,j=0;
int ind,c=0;
for(i=0;i<24;i++){
if(a[i]>=a[i+1]){
j=j+1;
}
else{
ind=i+1;
break;
}
}
if(j==24){
return -1;
}
else{
return ind;
}
}
int main(){
double a[25];
int i=0;
for(i=0;i<25;i++){
cout<<"Enter number:";
cin>>a[i];
}
cout<<out_of_order(a);
}
#output:
#if you have any doubts comment below.....