Question

In: Computer Science

c++ Please explain why its wrong. The findDisorder function is supposed to find the first item...

c++ Please explain why its wrong.

The findDisorder function is supposed to find the first item in an array that
is less than the element preceding it, and set the p parameter to point to
that item, so the caller can know the location of that item. Explain why this
function won't do that, and show how to fix it. Your fix must be to the
function only; you must not change the the main routine below in any way,
yet as a result of your fixing the function, the main routine below must
work correctly.
void findDisorder(int arr[], int n, int* p)
{
for (int k = 1; k < n; k++)
{
if (arr[k] < arr[k-1])
{
p = arr + k;
return;
}
}
p = nullptr;
}
int main()
{
int nums[6] = { 10, 20, 20, 40, 30, 50 };
int* ptr;
findDisorder(nums, 6, ptr);
if (ptr == nullptr)
cout << "The array is ordered" << endl;
else {
cout << "The disorder is at address " << ptr
<< endl;
cout << "It's at index " << ptr - nums << endl;
cout << "The item's value is " << *ptr << endl;
}
return 0;
}

Solutions

Expert Solution


Answer:-

Code:-

#include <iostream>
using namespace std;
void findDisorder(int arr[], int n, int **p)   {
   for (int k = 1; k < n; k++)
   {
       if (arr[k] < arr[k-1])
       {
           *p = arr+k;
           return;
       }
   }
   p = NULL;
}
int main()
{
   int nums[6] = { 10, 20, 20, 40, 30, 50 };
   int* ptr,**p=&ptr;
   findDisorder(nums, 6, p);
   if (ptr == NULL)
       cout << "The array is ordered" << endl;
   else {
       cout << "The disorder is at address " << ptr << endl;
       cout << "It's at index " << (ptr - nums) << endl;
       cout << "The item's value is " << *ptr << endl;
   }
   return 0;
}

Explanation:- Using pointers, we can modify contents in that address,we can not change the actual pointer address.So, store the address of ptr in double pointer ,then modify the address in it.

      Thank you...

Output:-


Related Solutions

Please explain in detail why it is right or wrong? 1. Subject 5 is a 21...
Please explain in detail why it is right or wrong? 1. Subject 5 is a 21 year old Boise State student who has the following values at baseline: PaO2 54 mm Hg, PaCO2 = 60 mm Hg while breathing FIO2 = 0.21 @ PB = 690 mm Hg. A Respiratory Therapist administers additional oxygen with an FIO2 = 0.4. Identify whether each of the following are correct or incorrect. a. At baseline, the PaCO2 is within normal limits b. At...
13) For the given cost function C(x) = 48,400 + 600x + x2, First, find the...
13) For the given cost function C(x) = 48,400 + 600x + x2, First, find the average cost function. Use it to find: a) The production level that will minimize the average cost x =      b) The minimal average cost $ = 14) Suppose a product's revenue function is given by R(q)=−4q2+400q, where R(q) is in dollars and q is units sold. Find a numeric value for the marginal revenue at 31 units. MR(31) = ? $ per unit
20) For the given cost function C(x)=22500+800x+x2, First, find the average cost function. Use it to...
20) For the given cost function C(x)=22500+800x+x2, First, find the average cost function. Use it to find: a) The production level that will minimize the average cost? 21) Given the function f(t)=(t−3)(t+7)(t−6). its f-intercept is   its t-intercepts are b) The minimal average cost?
Q. Identify and explain how the system of a single cell is supposed to function in...
Q. Identify and explain how the system of a single cell is supposed to function in a normal environment and is being affected by the item listed below. This means explaining how all aspects of the cell (inside and outside) may be impacted by this problem. -Make sure to fully explain the item listed in the cellular dysfunction as well as all other related items in the system of a cell. 1. Inherited Autosomal Recessive Mutation of hydrolytic enzymes (inherited...
Please answer in c++ 6.Define a function to find a given target value in an array,...
Please answer in c++ 6.Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible. 7.Write a swap function, that swaps the values of two variables in main, but use pointers instead of reference parameters. 8.Write a function that takes an array of ints and its size as arguments. It should create a new array that is the same size as the argument. It should set the values in...
please explain the solution and what it is wrong with my conception. FOLLOW the COMMENT PLEASE...
please explain the solution and what it is wrong with my conception. FOLLOW the COMMENT PLEASE Question: Let A and B are nonempty set bounded subset of R, and let A+B be the set of all sums a+b where a belngs to A and b belongs to B Prove Sup(A+B)=Sup(A)+Sup(B) Solution: Let ε>0, a is in A and b is in B, supA<=a+(ε/2), supB<=b+(ε/2) sup(A + B) ≥ a + b ≥ sup A − ε /2 + sup B...
Why do corporations exist? Are they supposed to serve Shareholders first and foremost with their decisions...
Why do corporations exist? Are they supposed to serve Shareholders first and foremost with their decisions or are they supposed to satisfy the Stakeholders? minimum 250 words
a. Find the two first partials ?? and ?? of the following function. ?(?, ?) =...
a. Find the two first partials ?? and ?? of the following function. ?(?, ?) = ?3?5 + ?4x sin(?) b. Find and classify all critical points of ?(?, ?) = ?3 − 3? + ?2 − 4? + 7. c. Maximize and minimize ?(?, ?) = 3? + ? + 33 subject to ?2 + ?2 = 40.
Underline all the correct answers. Explain why an answer is correct or why it is wrong....
Underline all the correct answers. Explain why an answer is correct or why it is wrong. You are supposed to select all of the correct answers (1-6) for each roman numeral (I-III) --Please justify/explain answers thoroughly-- Problem 7. If photons with energy of 1.7 eV was applied to A Si Wafer is at 300k (Eg for Si =1.12eV), the valence and conduction band are (I) 1-      Both are completely empty 2-      Both are completely filled. 3-      Both are partially empty....
Item 11 Item 11 Nu Company reported the following pretax data for its first year of...
Item 11 Item 11 Nu Company reported the following pretax data for its first year of operations. Net sales 2,960 Cost of goods available for sale 2,360 Operating expenses 800 Effective tax rate 40 % Ending inventories: If LIFO is elected 960 If FIFO is elected 1,140 What is Nu's gross profit ratio if it elects LIFO?----- --------------------------------------------------------------------------------------------------------------------------------------------------- Item 12 Item 12 Udon Inc. adopted dollar-value LIFO (DVL) as of January 1, 2018, when it had an inventory of $710,000....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT