Need help on factoring program.

This is a discussion on Need help on factoring program. within the C++ Programming forums, part of the General Programming Boards category; I'm diving into C++ and after going through some the tutorials on CProgramming.com, I saw a challenge to make a ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    Need help on factoring program.

    I'm diving into C++ and after going through some the tutorials on CProgramming.com, I saw a challenge to make a program factor a number using recursion. So I had to do it. The program through each instance finds the right factor and the right number of loops, but and the end when I print the linked list out it's all the last value I found. I feel like I'm close on this and need some help.

    Big thanks in advance!

    Code:
    // class1.cpp: implementation of the class1 class.
    //
    //////////////////////////////////////////////////////////////////////
    
    #include "class1.h"
    #include "iostream.h"
    
    //////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    
    class1::class1()
    {
    
    }
    
    class1::~class1()
    {
    
    }
    
    struct fact {
    	int val;
    	fact *next;
    };
    
    void GO(int *srcNum, fact *srcFact) {
    	bool chgsrc;
    	fact *tmpFact;
    	tmpFact=new fact;
    
    	srcFact->next=new fact;
    	srcFact=srcFact->next;
    	srcFact->next=0;
    	srcFact->val=0;
    
    	chgsrc = 1;
    
    	if (*srcNum != 1) {
    		if (*srcNum % 2 == 0) {
    			srcFact->val=2;
    			if (chgsrc) {
    				*srcNum=*srcNum / 2;
    				cout<<"- "<<*srcNum<<endl;
    				cout<<srcFact->val<<endl;
    				cout<<srcFact<<endl;
    				//tmpFact=tmpRoot;
    				//if(tmpFact!=0)
    				//{
    					//while(tmpFact->next!=0)
    					//{
    						//cout<<tmpFact->val<<" - "<<tmpFact<<endl;
    						//tmpFact=tmpFact->next;   
    					//}
    					//cout<<tmpFact->val<<endl;
    				//}
    
    				cout<<endl;
    				chgsrc = 0;
    			}
    		}
    
    		if (*srcNum % 3 == 0) {
    			srcFact->val=3;
    			if (chgsrc) {
    				*srcNum=*srcNum / 3;
    				cout<<"- "<<*srcNum<<endl;
    				cout<<srcFact->val<<endl;
    				cout<<srcFact<<endl;
    				cout<<endl;
    				chgsrc = 0;
    			}
    		}
    
    		if (*srcNum % 5 == 0) {
    			srcFact->val=5;
    			if (chgsrc) {
    				*srcNum=*srcNum / 5;
    				cout<<"- "<<*srcNum<<endl;
    				cout<<srcFact->val<<endl;
    				cout<<srcFact<<endl;
    				cout<<endl;
    				chgsrc = 0;
    			}
    		}
    
    		if (*srcNum % 7 == 0) {
    			srcFact->val=7;
    			if (chgsrc) {
    				*srcNum=*srcNum / 7;
    				cout<<"- "<<*srcNum<<endl;
    				cout<<srcFact->val<<endl;
    				cout<<srcFact<<endl;
    				cout<<endl;
    				chgsrc = 0;
    			}
    		}
    	}
    
    	if (srcFact->val != 0) {
    		GO(srcNum, srcFact->next);
    	} else {
    		//srcFact->val = *srcNum;
    	}
    
    };
    
    
    int main() {
    	fact *root;
    	fact *nxtFact;
    	int numtochk;
    
    	numtochk = 30;
    
    	nxtFact = new fact;
    	root=new fact;
    	root->next=0;
    	root->val=1; //Make this 1 because a factorial is atleast that number times 1.
    
    	nxtFact = root;
    
    	GO(&numtochk, nxtFact);
    
    	
    	//NOW I WANT TO SEE ALL MY VALUES
    	nxtFact=root; //Lets go back to the beginning
    	if(nxtFact!=0)
    	{
    		while(nxtFact->next!=0)
    		{
    			cout<<nxtFact->val<<" - "<<nxtFact<<endl;
    			nxtFact=nxtFact->next;   
    		}
    		cout<<nxtFact->val<<endl;
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    3
    The final if statement in the GO function, I had mistyped. It should be:

    Code:
    	if (srcFact->val != 0) {
    		GO(srcNum, srcFact);
    	} else {
    		//srcFact->val = *srcNum;
    	}

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    3

    Found problem

    The main problem was with the if code block for each base prime. Amongst other small corrections I made in the code the correct if statement should be:

    Code:
    if (*srcNum % 2 == 0) {
    		if (chgsrc) {
    			srcFact->val=2;
    			*srcNum=*srcNum / 2;
    			chgsrc = 0;
    		}
    	}
    instead of

    Code:
    if (*srcNum % 2 == 0) {
    		srcFact->val=2;
    		if (chgsrc) {
    			*srcNum=*srcNum / 2;
    			chgsrc = 0;
    		}
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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