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.
Consider the declaration
```
double a[10] = {1.2, 2.1, 3.3, 3.5, 4.5, 7.9, 5.4,
8.7, 9.9, 1.0};
```
Write a function named out_of_order that will test this array for the condition
```
a[0] <= a[1] <= a[2] <= ...
```
```
int out_of_order(double array[], int size)
{
for(int i=0; i < size-1; i++)
if (array[i] > array[i+1])
return i+1;
return -1;
}
```
The upper limit for index i is size-1 because we fetch a[i+1] for each i in the range of the loop. Otherwise we would fetch and compare a[size], causing erroneous results. (We don’t know what is in that location.)
You might also like to view...
This class is used to help implement mutual exclusion.
a. MutEx. b. Condition. c. Lock. d. Signal.
When a host's interface initializes, it may send which of the following messages to determine if any IPv6 routers are on the network segment?
A. Router Advertisement B. Router Solicitation C. Neighbor Advertisement D. Neighbor Solicitation
You can click ________ in Word Options to make changes to add commands to a tab
A) Quick Access Toolbar B) Add-Ins C) Customize Ribbon D) Display
Which attack involves sending an ICMP packet to the broadcast address so that it is then sent to the spoofed source address, causing the network to perform a DoS attack on one of more of its member servers?
a. Stack tweaking b. RST cookies c. Smurf IP attack d. None of the above