Fix Dynamic Array Bug: Find the First Item Less than Preceding

In summary, the findDisorder function is designed to find the first disordered item in an array and set the p parameter to point to that item's location. However, when the program is run, it fails due to multiple bugs in the function. Suggestions for fixing these bugs include initializing the ptr variable in main and understanding how C++ handles pointers in function parameters.
  • #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;
	}
    }
 
Physics news on Phys.org
  • #2
magnifik said:
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;
	}
    }

What do you mean when you say it fails? Have you learned how to use a debugger? That can show you what is happening at each step in your function, as well as what happens after your function returns to main.
 
  • #3
the program itself is able to open up, but immediately upon opening i get a runtime error that ptr is not initialized.. but the problems are in the void function, not in the main so I'm not sure what's wrong in the void part
 
  • #4
The first problem is easy to fix - just initialize ptr to something (nums would be appropriate).

Your second problem is that C++ is strictly a call-by-value language. You can simulate call-by-reference by passing a pointer. This means that if a function has a pointer parameter, the address that is passed in as an argument to the function cannot change. What can change is what is at that address.

What you're trying to do is pass in one address (the address of your nums array) and have p get changed inside the function. That does happen, but it has no effect on the actual address that was passed in.

There are two solutions: pass in the pointer exactly as before but return the address where the disordered value was found (or NULL if the array was ordered); make the pointer parameter a pointer to a pointer (int ** p).

If you want a pointer argument to The p parameter in
 
  • #5
Mark44 said:
What you're trying to do is pass in one address (the address of your nums array) and have p get changed inside the function. That does happen, but it has no effect on the actual address that was passed in.

There are two solutions: pass in the pointer exactly as before but return the address where the disordered value was found (or NULL if the array was ordered); make the pointer parameter a pointer to a pointer (int ** p).

If you want a pointer argument to The p parameter in

i'm not sure what you mean by returning the address where the disordered value was found. does that mean p = &k and not arr + k?
 
  • #6
What I mean is that you can do either of the following:
1. Keep the parameters to your function exactly the same but have the function return a pointer --
Code:
int * findDisorder(int arr[], int n, int* p)
2. Keep the function a void function, but change the third parameter to int ** p.
 
  • #7
i tried the second method you suggested and made int ** p and changed nothing else so my code for the void function was

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;
    }

but i am still getting a runtime error that ptr is not initialized...i'm not supposed to change anything in the main function so is there a way to fix it in the void??
 
  • #8
In main, I have
int nums[6] = { 10, 20, 20, 40, 30, 50 };
int * ptr = nums;

As far as not changing anything in main, this is new information. I don't see any way around not making a change in main since you're getting a run-time error the way it currently is, and no change to findDisorder will cause that error to go away.

The change I made by initializing ptr is pretty innocuous, so a reasonable prof shouldn't have any problem with a change like this. You should consult with him/her on that point.

In the first version of findDisorder, the parameter p is a pointer to an int, and the two instances of p in the body of this function are as p.

In the new version of this function, the parameter p is a pointer to a pointer to an int, so the two instances of p should be as ... ? Those are the only changes you need to make to findDisorder. (Be advised that I didn't thoroughly test your code, so you might have some edge conditions that could cause problems.)
 
  • #9
this is my code in its entirety (without having changed the main routine)

Code:
#include <iostream>
#include <cctype>
#include <cstring>

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;

        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;
	}
    }

now it doesn't compile but instead gives me the error that it cannot convert int * to int **
 
  • #10
Pass in &ptr - that's the address of a pointer to an int; i.e. a pointer to a pointer to int. Once you fix this, though, you might still get the run-time error about a pointer not being initialized.
 

FAQ: Fix Dynamic Array Bug: Find the First Item Less than Preceding

What is a dynamic array?

A dynamic array is a data structure that can grow or shrink in size during program execution. It is similar to a regular array, but it does not have a fixed size and can be resized as needed.

What is the bug in the "Fix Dynamic Array Bug: Find the First Item Less than Preceding" algorithm?

The bug in this algorithm is that it does not correctly find the first item in the array that is less than the preceding item. It may return the wrong index or not find any items at all.

How can I fix the bug in the algorithm?

The bug can be fixed by carefully reviewing the logic of the algorithm and making necessary adjustments. Some possible solutions could include using a different comparison operator or changing the order of operations.

What is the importance of fixing this bug?

Fixing this bug is important because it affects the accuracy and functionality of the algorithm. If the algorithm is not working correctly, it may produce incorrect results and impact the overall performance of the program.

Are there any potential issues or limitations to consider when fixing this bug?

Yes, there may be other factors to consider when fixing this bug, such as the size and complexity of the array, potential edge cases, and the efficiency of the algorithm. It is important to thoroughly test the fixed algorithm to ensure it is functioning properly in all cases.

Similar threads

Replies
3
Views
2K
Replies
21
Views
2K
Replies
4
Views
3K
Replies
4
Views
8K
Replies
3
Views
6K
Replies
23
Views
2K
Replies
1
Views
2K
Back
Top