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++ (10th Edition)
Additional Engineering Textbook Solutions
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Concepts Of Programming Languages
Java: An Introduction to Problem Solving and Programming (8th Edition)
Database Concepts (8th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Web Development and Design Foundations with HTML5 (8th Edition)
- Suppose we are talking about a mysterious number called Opaque Number. Any positive integer is Opaque Number if it has the following properties. All digits of the number alternate between even and odd. Thefirst/last digit cannot be the highest digit in that number. Your task is to take a positive integer number from user and find if it is Opaque Number or not. Write down this C Program by using only if else and loop without array.arrow_forwardWrite a program that takes 20 elements of array from user at run time. And also Find maximum element of array and swap it with last element of array. Display before and after swapping array.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_forward
- Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint:Read a number and store it to an array if it is new. If the number is already in the array, ignore it. While solving the question please use an array.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter ten numbers: 1 2 3 21634 5 2 The number of distinct number is 6. The distinct numbers are: 123645arrow_forwardA chess has ABCD andEstudents. They take a test for 100 points. Write a program which takes input of test scores fiom the gouder and gives the letter grade for the students and the averye score of the class. Use Array \& loop.arrow_forwardA scientist has developed a mathematical model for a physical process, and he wants to check how good is model is. To evaluate the correctness of his model, he wants to test the results of his model under certain parameters and compare them with experimental results. Write a program that first reads the number of tests (testCount) as an int followed by the results of each test according to the model as a double array (test Model) and finally the results of each test according to experiments as a double array (testExperiment). Then, the program should calculate the error of the model by evaluating the average of the squares of the differences between the model result and experimental result (see formula below) using a function. Error Input 1 testCount NOTE: Individual square of the difference between the model value and the experiment value calculations MUST be done in the function. Every other functionality MUST be done in the main function. NOTE: To calculate square of a number you…arrow_forward
- Modify your program in part (a) such that the user enters the numbers that will be populated to the array. A sample run would be as follows:Enter number 1 to populate: 2Enter number 2 to populate: 65Enter number 3 to populate: 9Enter number 4 to populate: 78Enter number 5 to populate: 27Enter number 6 to populate: 36Enter number 7 to populate: 25Enter number 8 to populate: 4Enter number 9 to populate: 61Enter number 10 to populate: -98Enter a number to search: 27Number 27 is in the array! A sample run would be as follows:Enter number 1 to populate: 2Enter number 2 to populate: 85Enter number 3 to populate: 9Enter number 4 to populate: 15Enter number 5 to populate: 22Enter number 6 to populate: 36Enter number 7 to populate: -87Enter number 8 to populate: 4Enter number 9 to populate: 1Enter number 10 to populate: 0Enter a number to search: 48Number 48 is not in the array!arrow_forwardA prime integer is any integer that is evenly divisible only byitself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows:a) Create an array with all elements initialized to 1 (true). Array elements with prime subscriptswill remain 1. All other array elements will eventually be set to zero. You’ll ignoreelements 0 and 1 in this exercise.b) Starting with array subscript 2, every time an array element is found whose value is 1,loop through the remainder of the array and set to zero every element whose subscriptis a multiple of the subscript for the element with value 1. For array subscript 2, all elementsbeyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6,8, 10, etc.); for array subscript 3, all elements beyond 3 in the array that are multiplesof 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on.When this process is complete, the array elements that are still set to one indicate that the subscriptis a…arrow_forwardIn this assignment, you will decide how to keep the inventory in the text file. Then your program must read the inventory from the file into the array. Each product must have a record in the file with the name, regular price, and type. There are several options for storing records in the file. For example, • each value takes one line in the file (i.e., three lines for one product). Then you must take care of correct handling of the ends of the lines; • all values are in one line that can be read as a string. Then you must handle the parsing of the string; • all values are in one line separated by a delimiter. Then you must handle a line with delimiters. Assume that the inventory does not have more than 100 products. But the actual number is known only after the reading of the file. Once you can read data from the file into the array, you must add a new property to the product class – a static variable that holds the number of products in the inventory. Its value must grow as reading…arrow_forward
- Write a program that declares an array of 15 integers and reads the values from the user. Make sure that the user doesn’t enter a value greater than 10 (if the user enters a value not in the allowed range, discard that value and read another to put into the array). Now tell the number that occur maximum number of times in the array. For example for the following 7 has maximum occurrence. 4,7,3,3,2,7,1,5,7,9,9,9,7,8,10 Use both linear and binary search.arrow_forwardProblem: Write a method that will determine whether a given value can be made given an array of coin values. For example, each value in the array represents a coin with that value. An array with [1, 2, 3] represents 3 coins with the values, 1, 2, and 3. Determine whether or not these values can be used to make a desired value. The coins in the array are infinite and can only be used as many times as needed. Return true if the value can be made and false otherwise. Dynamic Programming would be handy for this problem. Data: An array containing 0 or more coin values and an amount. Output: Return true or false. Sample Data ( 1, 2, 3, 12, 5 ), 3 ( 4, 15, 16, 17, 1 ), 21 ( 1 ), 5 ( 3 ), 7 Sample Output true true true falsearrow_forwardThis is the unoptimized version of class BubbleSort. This function also prints the number of passes (number of times the outer loop runs) made through the input array. One way to optimize bubblesort is to stop when the inner loop does not swap any elements. And another function to this class called bubblesortoptimized which stops when the inner loop does not swap any elements. This function should also print the number of passes (number of times the outer loop runs) made through the input array.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