- #1
magnifik
- 360
- 0
this 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. when i run it.. it fails. there are probably multiple bugs.. any suggestions on how to fix? thanks
Code:
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;
findDisorder(nums, 6, ptr);
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;
}
}