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

80.2K

Verified Solution

Question

Programming

c++ Please explain why its wrong.

The findDisorder function is supposed to find the first item inan array that
is less than the element preceding it, and set the p parameter topoint 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 tothe
function only; you must not change the the main routine below inany way,
yet as a result of your fixing the function, the main routine belowmust
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 <cout << \"The item's value is \" << *ptr <}
return 0;
}

Answer & Explanation Solved by verified expert
4.1 Ratings (814 Votes)
AnswerCodeinclude using namespace stdvoid    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students