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...
C language only please and please make a simple code Write a function that will find...
C language only please and please make a simple code Write a function that will find whether there exist two integers that sum to the target integer. The function is to “return” three values.First, return “1” if the integers were found,return “-1” if your search was not successful.If you find two integers which add up to the target value, you should return their respective index position inside the array. Suggested prototype:int TwoSumFunction(int arr[], int size, int target, int*index1, int* index2);Inside...
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 write program in c++ with using pointer. create a function that can find the number...
Please write program in c++ with using pointer. create a function that can find the number of even numbers that are between the maximum and minimum elements of an given array.The program have to use pointer. example 8 -1 2 2 1 9 2 4 0 output: 2
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT