what is wrong with my function?

This is a discussion on what is wrong with my function? within the C++ Programming forums, part of the General Programming Boards category; I am using this function with my ADT Natural number in large precision (My DT is Code: struct NatNo{ unsigned ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Unhappy what is wrong with my function?

    I am using this function with my ADT Natural number in large precision
    (My DT is
    Code:
    struct NatNo{ unsigned n; //number of digits
                           unsigned V[30];  // array of digits in order of introduction ( starting from 0)
                         }
    )

    Code:
    void add(NatNo a, NatNo b, NatNo &sum)
    {
            unsigned i;
    	 
            unsigned maxlength= a.n>b.n? a.n: b.n;
    	 
            init(sum, maxlenght); /* initializes sum with maxlength elements of 0 (s.n=maxlength and the array(s.V) with zero values ) */
    	
             for(i=a.n;i<maxlength;i++)
    		 a.V[i]=0;
    	 
             for(i=b.n;i<maxlength;i++)
    		 b.V[i]=0;
    		 
       	 for(i=maxlength-1;i>=0;i--);
             {
                  sum.V[i+1] += (a.V[i] + b.V[i]) % 10;  /* the sum will have an additional        
                                                                                digit(pozition 0) for overflow */
                  sum.V[i] += (a.V[i] + b.V[i]) / 10;  // transportation
    	  }
                  
         }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Seems that you want to add two numbers. But why are you resetting the operands a and b to zero before adding ?
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    I am only adding zeroes in the number with less digits...

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    instead of init(sum,maxlenght) i use init(sum,maxlength+1) (as i said, the sum will have one digit more than the biggest number) but still does not solve the problem

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Sorry, I overlooked that.
    But
    Code:
    sum.V[i+1] += (a.V[i] + b.V[i]) % 10;
    sum.V[i] += (a.V[i] + b.V[i]) / 10;
    guess you have to do something like this
    Code:
    sum.V[i+1] = (sum.V[i+1] + a.V[i] + b.V[i]) % 10; 
    sum.V[i] = (sum.V[i] + a.V[i] + b.V[i]) / 10;
    to correctly add transportation
    Kurt
    Last edited by ZuK; 03-21-2009 at 06:12 AM.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Unhappy

    thanks, Kurt...
    to make sure that addition works right, i introduced an auxilliary variable, which calculates the partial sums on every position(aux),and one for the transportation digit(t)
    Code:
    unsigned aux=0,t=0;
    for(i =maxlength-1;i>=0;i--);
             {
    	    aux=t+a.V[i]+b.V[i];
                sum.V[i+1]= aux % 10;
    	     t=aux/10;
    	}
    sum.V[0]=t;
    unfortunately, this STILL doesn't solve the problem...

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    for(i =maxlength-1;i>=0;i--);  // remove the semicolon
    BTW I'm too lazy to create a compilable program from your code by myself.
    You'd get much more help if you would create such a program. Also it's quite possible that the real error is somewhere else.
    Kurt

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    the semicolon was embarrassing but the problem was indeed somewhere else...i eventually managed to solve it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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