Chapter 7: Problem 7
What is wrong with the following piece of code? int sample_array[10]; for (int index = 1; index <= 10; index++) sample_array[index] = 3*index;
Chapter 7: Problem 7
What is wrong with the following piece of code? int sample_array[10]; for (int index = 1; index <= 10; index++) sample_array[index] = 3*index;
All the tools & learning materials you need for study success - in one app.
Get started for freeWrite 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.
Write a program that will read up to ten letters into an array and write the letters back to the screen in the reverse order. For example, if the input is abcd. then the output should be dcba Use a period as a sentinel value to mark the end of the input. Call the array letter_box. For this exercise you need not use any functions. This is just a toy program and can be very minimal.
Insert const before any of the following array parameters that can be changed to constant array parameters: void output(double a[], int size); //Precondition: a[0] through a[size - 1] have values. //Postcondition: a[0] through a[size - 1] have been //written out. void drop_odd(int a[], int size); //Precondition: a[0] through a[size - 1] have values. //Postcondition: All odd numbers in a[0] through //a[size - 1] have been changed to 0.
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.
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.
What do you think about this solution?
We value your feedback to improve our textbook solutions.