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];
<Some code to fill the array a goes here.>
for (int index = 0; index < 10; index++)
if (a[index] > a[index + 1])
cout << “Array elements ” << index << “ and”
<< (index +1) << “are out of order.”;
Want to see the full answer?
Check out a sample textbook solutionChapter 7 Solutions
Problem Solving with C++, Student Value Edition plus MyProgrammingLab with Pearson eText -- Access Card Package (9th Edition)
Additional Engineering Textbook Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
Starting Out with Python (4th Edition)
Digital Fundamentals (11th Edition)
Starting Out with Python (3rd Edition)
Web Development and Design Foundations with HTML5 (8th Edition)
- reversing the elements of an array involves swapping the corespodning elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array. given an array a, an int variable n containing the number of elements in a, and two other int variables, k and temp, write a loop that reverses the elements of the array. do not use any other variables besides a, n, k, and temp. in c++arrow_forwardWrite a program that reads an integer array a[] from the console and writes out the subsequence of the array that sums to the largest value. If there are more than one such subsequence, then the program should write out the subsequence that starts at the smallest position in the array and, if there are more than one subsequence of largest sum starting at the same smallest position, it should write out the shortest such subsequence.arrow_forwardWrite a program that prompts the user for an integer, then asks user to enter that many integer value. Store these values in an integer array and print the array. Reverse the array elements so that first element become the last element, the second element become the second to last element, and so on, with the old last element now first. Do not just reverse the order in which they are printed; actually, change the way they are stored in the array. Try not to create a second array; just rearrange the elements within the array you have. (Hints; Swap elements that need to change place.) When the elements have been reversed, Print the array again.arrow_forward
- I have a java program that is supposed to compare the sums of the columns of a 2D array and output the index of the column with the largest sum. I was able to find the column sums but I do not know how to compare the different sums and output the index of the largest sum. Here is the piece of code that finds the column sums: int [] colSum = new int [arr[0].length]; for (int k = 0; k < colSum.length; k++) { System.out.print(colSum[k]); ----> colSum[k] = the column sums How do I compare the integers in colSum[k] to find the largest sum and how do I output the index of the largest sum?arrow_forward3. Write a program to find intersection of two sorted array.For example, if the two sorted arrays as input are {21, 34, 41, 22, 35} and {61, 34, 45, 21, 11}, it should returnan intersection array with numbers {34, 21}. For the sake of this Problem, you can assume that numbers ineach integer array are unique.arrow_forwardSuppose that you are playing a card game whose goal is to collect exactly k cards with a total value of t. Write a solution that can determine if it is possible for a given collection of cards whose values are given in the array values. For example, if the array contained the values <10,5,7,2>, k = 3, and t = 12, you would return false because no three values add to 12. However, if t = 14, you would return true, since 5 + 7 + 2 = 14. So I considered this to be a form of groupSum recursion, so I try to implement it in recursion form. However, since the passing parameters are not the incrementing level index but 'k' that is the limit number of the numbers we can choose from the array, I cannot use passing index through recursion to search from the start of the array toward the end of the values.length(the length of the array). would there be any method I can use to increment and search along the array using k, the number of numbers we can add into the total(t) instead of the…arrow_forward
- Given your array declared above, and assuming your program has already read in a certain number of Complex values into that array, we'd like to take the average of these Complex values. It may help if you also knew that the Complex class has this method for adding two Complex objects: Complex Complex::add(const Complex & b) const; and a method to divide a Complex object by a floating-point value: Complex Complex::divide(double n) const; Now, show your code in C++ to average all the elements in your array of Complex numbers. (Hint: What is the data type of the average of two or more Complex numbers?)arrow_forwardHow would I transform the following program to allow the user to input an odd number of positive integers in a single line (space-separated). In other words, how would I change the array to allow user input? The program should output the number that occurs the odd number of times. Code to be Modified: #include <iostream> using namespace std; int getNumOccurredOdd(int arr[], int n) { int res = 0; for (int i = 0; i < n; i++) res = res ^ arr[i]; return res; } int main() { int arr[] = {3, 4, 6, 5, 6, 3, 5, 4, 6, 3, 5, 5, 3}; int n = sizeof(arr)/sizeof(arr[0]); cout << getNumOccurredOdd(arr, n) << " is present odd number of times"; }arrow_forwardPlease code in python.Given an array of integers a of even length, your task is to split it into two arrays of equal length such that all the numbers are unique in each of them. There may be more than one possible answer, in which case you may return any of them. If there are no possible answers, return an empty array. Hint: Count the number of occurrences of each integer in a. If there are integers occurring more than twice, then there is no solution. Next, put the integers occurring twice into both answer arrays. Finally, put all other numbers in the answer arrays, following the condition that they should have equal sizes. Example • For a = [2, 1, 2, 3, 3, 4], the output can be solution(a) = [[2, 1, 3], [2, 3, 4]]. Answers like [[1, 2, 3], [2, 3, 4]] or [[4, 2, 3], [3, 2, 1]] would also be considered correct. • For a = [1, 2, 2, 1], the output can be solution(a) = [[1, 2], [2, 1]]. Again, there are other possible answers. • For a = [2, 2, 3, 3, 2, 2], the output should be solution(a)…arrow_forward
- Write a program that creates an array of size 6 and input values to it through user input. It turnsout that these values that the user inputs were biased, and had an offset relative to the index ofthe array (for example, the first value had no bias since index = 0, the second value had a bias of 1since index = 1, and so on...).Pass this array to a function, that should remove the bias from each value by subtracting the biasfrom the original value. You need to subtract values using a for loop.Print the updated values of the array in the main function after the call to this bias removerfunction.arrow_forwardWrite a program that execute first part if user press 1 and execute second part if user press 2. • Recall the 1D array and functions concepts that we learned then write a program using those concepts. Declare a 1D array of n elements. Then input values in array in such a way that user only allowed to input into the last element. But if you always input value into the last element then the remaining array always remains empty. Therefore, we introduced a shifting method that shift each element to its neighboring element before entering the new element. Write a function Shift_Array () which takes the array as an argument and perform the element shifting process and then in main function input item at the end of the array. The functionally is explained below:8Array after 1st value012345Index812Array after 2nd value012345Index8122Array after 3rd value012345Index812213Array after 4th value012345Index8122137Array after 5th value012345Index812213719Array after 6th value012345Indexarrow_forwardYou are given an array of integers that contain numbers in random order. Write a program to find and return the number which occurs the maximum times in the given input. If two or more elements contend for the maximum frequency, return the element which occurs in the array first. Input Format: The first line of input contains an integer, that denotes the value of the size of the array. Let us denote it with the symbol N. The following line contains N space separated integers, that denote the value of the elements of the array. Output Format : The first and only line of output contains most frequent element in the given array. Constraints: 0 <= N <= 10^8.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education