Pointer Problems...

This is a discussion on Pointer Problems... within the C++ Programming forums, part of the General Programming Boards category; Anyone? ha I don't know how exactly to implement this, should i make the function return a pointer to the ...

  1. #16
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Anyone? ha

    I don't know how exactly to implement this, should i make the function return a pointer to the next 2 nodes?

    Can you have a function return 2 values? Can you have it return a pointer to a structure?

    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #17
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Code:
    int countNodes(node *startPoint)
    {
    	if(startPoint == NULL)
    	{
    		//No Nodes = 0
    		return 0;
    	}else{
    		int count = 1;
    		count += countNodes(startPoint->cLeft);
    		count += countNodes(startPoint->cRight);
    		return count;
    	}
    }
    And here's where it's implemented:
    Code:
    createTree(root, numbers, size);
    	//TEMPORARY
    	cout<<"The Tree Has "<<countNodes(root)<<" nodes.\n";
    	//END TEMPORARY
    I'm getting segmentation faults like crazy! ha

    Here's the rest of the program should you guys need it:
    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(node *start, int numbers[], int size);		//Creates the Tree
    struct node *addPly(node *parent, int numbers[], int cArrayPoint);	//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[], int size);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[], int size);
    int countNodes(node *startPoint);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	
    	int numbers[10];	//Array of numbers to be represented by the tree
    	int *pNum[10];		//Array of Pointers to the array of numbers
    	int size;			//Size of the two previous arrays
    	size = (sizeof(numbers)/sizeof(numbers[0]));
    	if(assignPointersArray(numbers, pNum, size) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter 10 Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum, size);
    	//TEMPORARY - DISPLAY THE LIST
    	cout<<"\n"<<"SORTED LIST:\n";
    	for(int disp=0; disp<size; disp++)
    	{
    		cout<<"Element "<<disp+1<<": "<<numbers[disp]<<endl;
    	}
    	cin.ignore();
    	//END TEMPORARY
    
    	createTree(root, numbers, size);
    	//TEMPORARY
    	cout<<"The Tree Has "<<countNodes(root)<<" nodes.\n";
    	//END TEMPORARY
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(node *start, int numbers[], int size)
    {
    	//Create Tree Function
    	//Assumes the list has been sorted properly, from lowest to highest
    	//Every node to the left of the parent is less than, and vice verse
    
    	//Determine the starting point
    	int startPoint;
    
    	if(size &#37; 2 == 0)
    	{
    		startPoint = size/2;
    	}else{
    		startPoint = (int)((size/2) - 0.5);
    	}
    
    	start->data = numbers[startPoint];
    
    	//Create the Plies of the tree
    	node *tempL;
    	node *tempR;
    	node *tempP;
    	node *tempP2;
    
    	tempP = addPly(start, numbers, startPoint);
    
    	tempL = tempP->cLeft;
    	tempR = tempP->cRight;
    
    	for(int i=0; i<size; i++)
    	{
    		tempP = addPly(tempL, numbers, startPoint-(i+1));
    		tempP2 = addPly(tempR, numbers, startPoint+(i+1));
    		tempL = tempP->cLeft;
    		tempR = tempP2->cRight;
    	}
    }
    
    struct node *addPly(node *parent, int numbers[], int cArrayPoint)
    {
    	node *nLeft;
    	node *nRight;
    
    	nLeft = new node;
    	nRight = new node;
    
    	if(numbers[cArrayPoint-1])
    	{
    		nLeft->data = numbers[cArrayPoint-1];
    		parent->cLeft = nLeft;
    	}else{
    		parent->cLeft = NULL;
    		delete nLeft;
    	}
    	if(numbers[cArrayPoint+1])
    	{
    		nRight->data = numbers[cArrayPoint+1];
    		parent->cRight = nRight;
    	}else{
    		parent->cRight = NULL;
    		delete nRight;
    	}
    	return parent;
    }
    
    void sortNumbers(int numbers[], int *pNum[], int size)
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<size; i++)
    	{
    		for(int s=0; s+1<size; s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    
    	//Replace the old numbers[] array with the new, sorted one
    	for(int r=0; r<size; r++)
    	{
    		*pNum[r] = numbers[r];
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum[], int size)
    {
    	//Assign Pointers to Array Elements Function
    	if(sizeof(numbers) != sizeof(pNum))
    		return false;
    	else{
    		for(int i=0; i<size; i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    
    int countNodes(node *startPoint)
    {
    	if(startPoint == NULL)
    	{
    		//No Nodes = 0
    		return 0;
    	}else{
    		int count = 1;
    		count += countNodes(startPoint->cLeft);
    		count += countNodes(startPoint->cRight);
    		return count;
    	}
    }
    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    (I'm working upwards here.) countNodes() looks okay. assignPointersArray() still has this line, which as I said earlier is useless:
    Code:
    	if(sizeof(numbers) != sizeof(pNum))
    		return false;
    Just leave it out. (Or pass both lengths and compare them.) You can't determine how many elements are in an array if you are passed the array. You have to get the calling function to pass the number of elements in it. (Assuming you're trying to do what I think you are.)

    displayTree() is obviously a stub.

    sortNumbers() still isn't doing things optimally. Take a look at this: http://www.cprogramming.com/tutorial.../sorting1.html

    addPly() seems to assume that cArrayPoint is >0 and <max-1. I don't know if that is a valid assumption or not. At least mention it if it is.

    In createTree() . . .
    Code:
    	if(size &#37; 2 == 0)
    	{
    		startPoint = size/2;
    	}else{
    		startPoint = (int)((size/2) - 0.5);
    	}
    The highlighted line won't do what you want. Since size is odd, dividing it by two will truncuate the number. (5/2 is 2.) Then you subtract .5 from that, and that becomes truncuated. In effect, the resulting number will be two less than if you'd used the code in the even branch of the if statement. I don't think that's what you want, but maybe it is. If it is there are easier ways to go about it.

    A debugger is a great tool for debugging segmentation faults. If you're using Dev-C++, it comes with (an annoying implementation of) GDB, the GNU debugger. There is a tutorial about its use here: http://www.cprogramming.com/gdbtutorial.html
    Last edited by dwks; 04-04-2007 at 12:55 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Okay i'll omit those lines and fix those things. I'm using MSVC Express.

    And i'm still lost, ha

    Thanks for the help! Keep it comin'!

    EDIT-----

    All the segmentation faults are stemming from the countNodes() function
    Last edited by Junior89; 04-04-2007 at 01:52 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    (Code like that also occurs in createTree().) You only need one temporary variable to swap two variables (and none in Perl ):
    Code:
    temp = one;
    one = two;
    two = temp;
    Also:
    Code:
    	int numbers[10];	//Array of numbers to be represented by the tree
    	int *pNum[10];		//Array of Pointers to the array of numbers
    	// [...]
    	if(assignPointersArray(numbers, pNum, size) == false)// [...]
    	{
    		return 1;
    		//Fatal Error
    	}
    
    // [...]
    
    bool assignPointersArray(int numbers[], int *pNum[], int size)
    {
    	//Assign Pointers to Array Elements Function
    	if(sizeof(numbers) != sizeof(pNum))
    		return false;
    	else{
    		for(int i=0; i<size; i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    As far as I can see, the only reason you created pNum was so that you could modify the values of the array in other functions, like sortNumbers(). Well, there's an easier way to do that.

    Code:
    const int array_size = 10;
    
    void called(int (*array)[array_size]);
    
    void driver(void) {
        int array[array_size] = {0};  /* initalize all elements to zero */
        called(&array);
    }
    
    void called(int (*array)[array_size]) {
        /* now the contents of array[] can be modified like so: */
        (*array)[0] = 0;
    }
    You wouldn't believe how long it took me to figure that out when I first started programming. I was writing a backup program, dbackup . . .
    Last edited by dwks; 04-04-2007 at 01:57 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks! I'll try to implement that, i've learned so much about pointers with all this, ha, more than i have with binary trees

    Lol. Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  7. #22
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(node *start, int numbers[], int size);		//Creates the Tree
    struct node *addPly(node *parent, int numbers[], int cArrayPoint);	//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[], int size);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[], int size, int pSize);
    int countNodes(node *startPoint);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	const int array_size = 10;
    
    	int numbers[array_size];	//Array of numbers to be represented by the tree
    	int *pNum[array_size];		//Array of Pointers to the array of numbers
    	int size;
    	int pSize;			//Size of the two previous arrays
    	size = (sizeof(numbers)/sizeof(numbers[0]));
    	pSize = (sizeof(pNum)/sizeof(pNum[0]));
    	if(assignPointersArray(numbers, pNum, size, pSize) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter "<<array_size<<" Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum, size);
    	//TEMPORARY - DISPLAY THE LIST
    	cout<<"\n"<<"SORTED LIST:\n";
    	for(int disp=0; disp<size; disp++)
    	{
    		cout<<"Element "<<disp+1<<": "<<numbers[disp]<<endl;
    	}
    	cin.ignore();
    	//END TEMPORARY
    
    	createTree(root, numbers, size);
    	//TEMPORARY
    	cout<<"The Tree Has "<<countNodes(root)<<" nodes.\n";
    	//END TEMPORARY
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(node *start, int numbers[], int size)
    {
    	//Create Tree Function
    	//Assumes the list has been sorted properly, from lowest to highest
    	//Every node to the left of the parent is less than, and vice verse
    
    	//Determine the starting point
    	int startPoint;
    
    	if(size % 2 == 0)
    	{
    		startPoint = size/2;
    	}else{
    		startPoint = (int)(size/2);
    	}
    
    	start->data = numbers[startPoint];
    
    	//Create the Plies of the tree
    	node *tempL;
    	node *tempR;
    	node *tempP;
    	node *tempP2;
    
    	tempP = addPly(start, numbers, startPoint);
    
    	tempL = tempP->cLeft;
    	tempR = tempP->cRight;
    
    	for(int i=0; i<size; i++)
    	{
    		tempP = addPly(tempL, numbers, startPoint-(i+1));
    		tempP2 = addPly(tempR, numbers, startPoint+(i+1));
    		tempL = tempP->cLeft;
    		tempR = tempP2->cRight;
    	}
    }
    
    struct node *addPly(node *parent, int numbers[], int cArrayPoint)
    {
    	node *nLeft;
    	node *nRight;
    
    	nLeft = new node;
    	nRight = new node;
    
    	if(numbers[cArrayPoint-1] != NULL)
    	{
    		nLeft->data = numbers[cArrayPoint-1];
    		parent->cLeft = nLeft;
    	}else{
    		parent->cLeft = NULL;
    		delete nLeft;
    	}
    	if(numbers[cArrayPoint+1] != NULL)
    	{
    		nRight->data = numbers[cArrayPoint+1];
    		parent->cRight = nRight;
    	}else{
    		parent->cRight = NULL;
    		delete nRight;
    	}
    	return parent;
    }
    
    void sortNumbers(int numbers[], int *pNum[], int size)
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<size; i++)
    	{
    		for(int s=0; s+1<size; s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    
    	//Replace the old numbers[] array with the new, sorted one
    	for(int r=0; r<size; r++)
    	{
    		*pNum[r] = numbers[r];
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum[], int size, int pSize)
    {
    	//Assign Pointers to Array Elements Function
    	if(size != pSize)
    		return false;
    	else{
    		for(int i=0; i<size; i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    
    int countNodes(node *startPoint)
    {
    	if(startPoint == NULL)
    	{
    		//No Nodes = 0
    		return 0;
    	}else{
    		int count = 1;
    		count += countNodes(startPoint->cLeft);
    		count += countNodes(startPoint->cRight);
    		return count;
    	}
    }
    Still getting all these segmentation faults with the countNodes() function

    The problem probably lies in the createTree and addPly functions

    Thoughts?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  8. #23
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your program crashes in this line
    Code:
        if(numbers[cArrayPoint-1] != NULL)
    when you call addPly with cArrayPoint 0.
    numbers are not pointers so I guess you want to check if parent has a left child.
    Mayby like that
    Code:
        if(cArrayPoint > 0 )
    This one creates an illegal access too
    Code:
        if(numbers[cArrayPoint+1] != NULL)
    You would have to pass the size of numbers to addPly to see if there is a right child
    Kurt

  9. #24
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I'm trying to see if that element of the array actually exists.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  10. #25
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Junior89 View Post
    I'm trying to see if that element of the array actually exists.
    I understand but you cannot do it this way.
    To access the element -1 of an array gives you an access error for shure.
    Kurt

  11. #26
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Then how should i go about doing it?
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  12. #27
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    the answer is in post #23
    Kurt

  13. #28
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I think i fixed it, but i'm still getting segmentation faults, with the addPly() funciton i think.


    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(node *start, int numbers[], int size);		//Creates the Tree
    struct node *addPly(node *parent, int numbers[], int cArrayPoint);	//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[], int size);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[], int size, int pSize);
    int countNodes(node *startPoint);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	const int array_size = 10;
    
    	int numbers[array_size];	//Array of numbers to be represented by the tree
    	int *pNum[array_size];		//Array of Pointers to the array of numbers
    	int size;
    	int pSize;			//Size of the two previous arrays
    	size = (sizeof(numbers)/sizeof(numbers[0]));
    	pSize = (sizeof(pNum)/sizeof(pNum[0]));
    	if(assignPointersArray(numbers, pNum, size, pSize) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter "<<array_size<<" Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum, size);
    	//TEMPORARY - DISPLAY THE LIST
    	cout<<"\n"<<"SORTED LIST:\n";
    	for(int disp=0; disp<size; disp++)
    	{
    		cout<<"Element "<<disp+1<<": "<<numbers[disp]<<endl;
    	}
    	cin.ignore();
    	//END TEMPORARY
    
    	createTree(root, numbers, size);
    	//TEMPORARY
    	cout<<"The Tree Has "<<countNodes(root)<<" nodes.\n";
    	//END TEMPORARY
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(node *start, int numbers[], int size)
    {
    	//Create Tree Function
    	//Assumes the list has been sorted properly, from lowest to highest
    	//Every node to the left of the parent is less than, and vice verse
    
    	//Determine the starting point
    	int startPoint;
    
    	if(size % 2 == 0)
    	{
    		startPoint = size/2;
    	}else{
    		startPoint = (int)(size/2);
    	}
    
    	start->data = numbers[startPoint];
    
    	//Create the Plies of the tree
    	node *tempL;
    	node *tempR;
    	node *tempP;
    	node *tempP2;
    
    	tempP = addPly(start, numbers, startPoint);
    
    	tempL = tempP->cLeft;
    	tempR = tempP->cRight;
    
    	for(int i=0; i<size; i++)
    	{
    		tempP = addPly(tempL, numbers, startPoint-(i+1));
    		tempP2 = addPly(tempR, numbers, startPoint+(i+1));
    		tempL = tempP->cLeft;
    		tempR = tempP2->cRight;
    	}
    }
    
    struct node *addPly(node *parent, int numbers[], int cArrayPoint)
    {
    	node *nLeft;
    	node *nRight;
    
    	nLeft = new node;
    	nRight = new node;
    
    	if(cArrayPoint-1 >= 0)
    	{
    		nLeft->data = numbers[cArrayPoint-1];
    		parent->cLeft = nLeft;
    	}else{
    		parent->cLeft = NULL;
    		delete nLeft;
    	}
    	if(cArrayPoint+1 <= 10)
    	{
    		nRight->data = numbers[cArrayPoint+1];
    		parent->cRight = nRight;
    	}else{
    		parent->cRight = NULL;
    		delete nRight;
    	}
    	return parent;
    }
    
    void sortNumbers(int numbers[], int *pNum[], int size)
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<size; i++)
    	{
    		for(int s=0; s+1<size; s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    
    	//Replace the old numbers[] array with the new, sorted one
    	for(int r=0; r<size; r++)
    	{
    		*pNum[r] = numbers[r];
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum[], int size, int pSize)
    {
    	//Assign Pointers to Array Elements Function
    	if(size != pSize)
    		return false;
    	else{
    		for(int i=0; i<size; i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    
    int countNodes(node *startPoint)
    {
    	if(startPoint == NULL)
    	{
    		//No Nodes = 0
    		return 0;
    	}else{
    		int count = 1;
    		count += countNodes(startPoint->cLeft);
    		count += countNodes(startPoint->cRight);
    		return count;
    	}
    }
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  14. #29
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I think your problem is here somewhere, where you call addPly():
    Code:
    void createTree(node *start, int numbers[], int size)
    {
    	//Create Tree Function
    	//Assumes the list has been sorted properly, from lowest to highest
    	//Every node to the left of the parent is less than, and vice verse
    
    	//Determine the starting point
    	int startPoint;
    
    	if(size &#37; 2 == 0)
    	{
    		startPoint = size/2;
    	}else{
    		startPoint = (int)(size/2);
    	}
    
    	start->data = numbers[startPoint];
    
    	//Create the Plies of the tree
    	node *tempL;
    	node *tempR;
    	node *tempP;
    	node *tempP2;
    
    	tempP = addPly(start, numbers, startPoint);
    
    	tempL = tempP->cLeft;
    	tempR = tempP->cRight;
    
    	for(int i=0; i<size; i++)
    	{
    		tempP = addPly(tempL, numbers, startPoint-(i+1));
    		tempP2 = addPly(tempR, numbers, startPoint+(i+1));
    		tempL = tempP->cLeft;
    		tempR = tempP2->cRight;
    	}
    }
    First of all, both branches of the if(size%2...) statement do the same thing, so why not get rid of the conditional code?

    Secondly, shouldn't
    Code:
    for(int i=0; i<size; i++)
    be <size/2 or <startPos or something? Thirdly, what if tempP->cLeft or Right is NULL? BOOM, segmentation fault. Check to see if they're NULL, and if so, exit the loop.
    Last edited by dwks; 04-05-2007 at 05:40 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #30
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Code:
    #include <iostream>
    
    using namespace std;
    
    //Node Structure
    struct node
    {
    	int data;		//Data of the Node (int)
    	node *parent;	//Pointer to the Parent Node
    	node *cLeft;	//Pointer to the child node on the left
    	node *cRight;	//Pointer to the child node on the right
    };
    
    //Global Variables
    node *root;
    node *current;
    
    //Function Prototypes
    void createTree(node *start, int numbers[], int size);		//Creates the Tree
    struct node *addPly(node *parent, int numbers[], int cArrayPoint);	//Used by createTree, adds another ply/level to the tree
    void displayTree();					//Displays the Tree
    void searchTree(int number);		//Searches the tree for a certain number
    void sortNumbers(int numbers[], int *pNum[], int size);	//Sorts an Array of numbers from Lowest to Highest
    bool assignPointersArray(int numbers[], int *pNum[], int size, int pSize);
    int countNodes(node *startPoint);
    
    int main()
    {
    	root = new node;	//Clear out memory for the Root node
    	const int array_size = 10;
    
    	int numbers[array_size];	//Array of numbers to be represented by the tree
    	int *pNum[array_size];		//Array of Pointers to the array of numbers
    	int size;
    	int pSize;			//Size of the two previous arrays
    	size = (sizeof(numbers)/sizeof(numbers[0]));
    	pSize = (sizeof(pNum)/sizeof(pNum[0]));
    	if(assignPointersArray(numbers, pNum, size, pSize) == false)//Function which assigns pointers to the numbers
    	{
    		return 1;
    		//Fatal Error
    	}
    
    	//Get Numbers
    	cout<<"Please Enter "<<array_size<<" Numbers..."<<endl;
    	for(int g=0; g<10; g++)
    	{
    		cout<<"Element "<<(g+1)<<": ";
    		cin>>numbers[g];
    	}
    
    	sortNumbers(numbers, pNum, size);
    	//TEMPORARY - DISPLAY THE LIST
    	cout<<"\n"<<"SORTED LIST:\n";
    	for(int disp=0; disp<size; disp++)
    	{
    		cout<<"Element "<<disp+1<<": "<<numbers[disp]<<endl;
    	}
    	cin.ignore();
    	//END TEMPORARY
    
    	createTree(root, numbers, size);
    	//TEMPORARY
    	cout<<"The Tree Has "<<countNodes(root)<<" nodes.\n";
    	//END TEMPORARY
    	displayTree();
    	cin.ignore();
    	return 0;
    }
    
    void createTree(node *start, int numbers[], int size)
    {
    	//Create Tree Function
    	//Assumes the list has been sorted properly, from lowest to highest
    	//Every node to the left of the parent is less than, and vice verse
    
    	//Determine the starting point
    	int startPoint;
    
    	if(size &#37; 2 == 0)
    	{
    		startPoint = size/2;
    	}else{
    		startPoint = (int)(size/2);
    	}
    
    	start->data = numbers[startPoint];
    
    	//Create the Plies of the tree
    	node *tempL;
    	node *tempR;
    	node *tempP;
    	node *tempP2;
    
    	tempP = addPly(start, numbers, startPoint);
    
    	tempL = tempP->cLeft;
    	tempR = tempP->cRight;
    
    	for(int i=0; i<size/2; i++)
    	{
    		
    		
    		tempP = addPly(tempL, numbers, startPoint-(i+1));
    		tempP2 = addPly(tempR, numbers, startPoint+(i+1));
    		
    		if(tempP->cLeft == NULL || tempP->cRight == NULL || tempP2->cLeft == NULL || tempP2->cRight == NULL)
    		{
    			break;
    		}
    		
    		tempL = tempP->cLeft;
    		tempR = tempP2->cRight;
    	}
    }
    
    struct node *addPly(node *parent, int numbers[], int cArrayPoint)
    {
    	node *nLeft;
    	node *nRight;
    
    	nLeft = new node;
    	nRight = new node;
    
    	if(cArrayPoint-1 >= 0)
    	{
    		nLeft->data = numbers[cArrayPoint-1];
    		parent->cLeft = nLeft;
    	}else{
    		parent->cLeft = NULL;
    		delete nLeft;
    	}
    	if(cArrayPoint+1 <= 10)
    	{
    		nRight->data = numbers[cArrayPoint+1];
    		parent->cRight = nRight;
    	}else{
    		parent->cRight = NULL;
    		delete nRight;
    	}
    	return parent;
    }
    
    void sortNumbers(int numbers[], int *pNum[], int size)
    {
    	//Sort Number Array Function - LO-HI
    	int temp1, temp2;
    	for(int i=0; i<size; i++)
    	{
    		for(int s=0; s+1<size; s++)
    		{
    			if(numbers[s] > numbers[s+1])
    			{
    				temp1 = numbers[s];
    				temp2 = numbers[s+1];
    				numbers[s] = temp2;
    				numbers[s+1] = temp1;
    			}else if(numbers[s] < numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else if(numbers[s] == numbers[s+1])
    			{
    				//Nothing - Stay the Same
    			}else{
    				//Nothing - Stay the Same
    			}
    		}
    	}
    
    	//Replace the old numbers[] array with the new, sorted one
    	for(int r=0; r<size; r++)
    	{
    		*pNum[r] = numbers[r];
    	}
    }
    
    void displayTree()
    {
    	//Display Tree Function
    }
    
    bool assignPointersArray(int numbers[], int *pNum[], int size, int pSize)
    {
    	//Assign Pointers to Array Elements Function
    	if(size != pSize)
    		return false;
    	else{
    		for(int i=0; i<size; i++)
    		{
    			pNum[i] = &numbers[i];
    		}
    		return true;
    	}
    }
    
    int countNodes(node *startPoint)
    {
    	if(startPoint == NULL)
    	{
    		//No Nodes = 0
    		return 0;
    	}else{
    		int count = 1;
    		count += countNodes(startPoint->cLeft);
    		count += countNodes(startPoint->cRight);
    		return count;
    	}
    }
    More segmentation faults!!!!! I hate seg faults, haha.

    THese are (again) with the countNodes() function.

    I must not be allocating the memory properly or soemthing...


    EDIT----------

    Yeah the startPoint->cLeft and cRight in the countNodes() function are NULL.

    =(
    Last edited by Junior89; 04-05-2007 at 07:39 PM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21