Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Write a function named out_of_order that takes as parameters an array of doubles and an int parameter named size and returns a value of type int. This function will test this array for being out of order, meaning that the array violates the following condition: a[0] <= a[1] <= a[2] <= ... The function returns -1 if the elements are not out of order; otherwise, it will return the index of the first element of the array that is out of order. For example, consider the declaration double a[10] = {1.2, 2.1, 3.3, 2.5, 4.5, 7.9, 5.4, 8.7, 9.9, 1.0}; In this array, a[2] and a[3] are the first pair out of order, and a[3] is the first element out of order, so the function returns 3. If the array were sorted, the function would return –1.

Short Answer

Expert verified
Question: Write a function called 'out_of_order' that takes an array of doubles and an integer as its parameters. The function should return the index of the first element in the array that is out of order according to the non-descending condition or return -1 if the array is sorted. Answer: In order to find the first out-of-order element in a given array of doubles, we can loop through the array, compare each pair of consecutive elements, and check if they follow the non-descending condition (a[i] <= a[i+1]). If we find a pair that violates the condition, we return the index of the first element in the pair that is out of order. If no such pair is found, we return -1 to indicate that the array is sorted. The function 'out_of_order' can be implemented as follows: ```cpp int out_of_order(double arr[], int size) { int outOfOrderIndex = -1; for (int i = 0; i < size - 1; i++) { if (arr[i] > arr[i + 1]) { outOfOrderIndex = i + 1; break; } } return outOfOrderIndex; } ```

Step by step solution

01

(1. Function Signature and Initialization)

First, we'll write the function signature, taking an array of doubles, and an int as its parameters. We'll initialize a variable to hold the out of order index. ```cpp int out_of_order(double arr[], int size) { int outOfOrderIndex = -1; // Rest of the code } ```
02

(2. Check Consecutive Elements for Non-Decreasing Condition)

Now, we will loop through the array, checking each pair of consecutive elements to see if they break the non-decreasing condition `a[i] <= a[i+1]`. If we find a pair that breaks the condition, we will set `outOfOrderIndex` to the index of the first element in the pair that is out of order. ```cpp for (int i = 0; i < size - 1; i++) { if (arr[i] > arr[i + 1]) { outOfOrderIndex = i + 1; break; } } ```
03

(3. Return the Out of Order Index)

Finally, we'll return the value of `outOfOrderIndex`. If no pair of elements is out of order, the value will still be -1, indicating that the array is sorted. ```cpp return outOfOrderIndex; ```
04

(4. Complete Function Implementation)

Bringing all the steps together, we get the following solution: ```cpp int out_of_order(double arr[], int size) { int outOfOrderIndex = -1; for (int i = 0; i < size - 1; i++) { if (arr[i] > arr[i + 1]) { outOfOrderIndex = i + 1; break; } } return outOfOrderIndex; } ``` This function will correctly find the index of the first element that is out of order in a given array or return -1 if the array is sorted.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Following is the declaration for an alternative version of the function search defined in Display \(7.12 .\) In order to use this alternative version of the search function we would need to rewrite the program slightly, but for this exercise all you need to do is to write the function definition for this alternative version of search. bool search(const int a[], int number_used, int target, int& where); //Precondition: number_used is <= the declared size of the //array a; a[0] through a[number_used -1] have values. //Postcondition: If target is one of the elements a[0] //through a[number_used - 1], then this function returns //true and sets the value of where so that a[where] == //target; otherwise this function returns false and the //value of where is unchanged.

Write some \(\mathrm{C}++\) code that will fill an array a with 20 values of type int read in from the keyboard. You need not write a full program, just the code to do this, but do give the declarations for the array and for all variables.

What is the output of the following code? int i, temp[10]; for (i = 0; i < 10; i++) temp[i] = 2*i; for (i = 0; i < 10; i++) cout << temp[i] << " "; cout << endl; for (i = 0; i < 10; i = i + 2) cout << temp[i] << " ";

Write a program that will read up to 10 nonnegative integers into an array called number_array and then write the integers back to the screen. For this exercise you need not use any functions. This is just a toy program and can be very minimal.

Suppose we expect the elements of the array a to be ordered so that a[0] ? a[1] ? a[2]? ... However, to be safe we want our program to test the array and issue a warning in case it turns out that some elements are out of order. The following code is supposed to output such a warning, but it contains a bug. What is it? double a[10]; for (int index = 0; index < 10; index++) if (a[index] > a[index + 1]) cout << "Array elements " << index << " and " << (index + 1) << " are out of order.";

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free