Thread: how to add 2 digits?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    56

    Question how to add 2 digits?

    I am trying to write a program using linked lists which takes in numbers the program must input two integers of arbitrary length from the keyboard. The built-in data types in C such as int and long have a maximum fixed length so variables of these types cannot be used to input the integers from the keyboard. Each large integer must be read from the keyboard and stored in a singly linked list, one digit per node. The least significant digit of the large integer must be stored at the top of the list and the most significant digit must be stored at the end of the list. Assume that each large integer has at most 50 digits.
    After creating the linked lists for the two large integers, you must find the sum of the integers. This sum must be stored in another linked list.

    I have wirtten the part which inputs and reads the numers , allocates the memory,makes the list,and prints what's in the list.I tried writing a reverse but aomehow it's not working.THE troublesome part that i m not sure how to approach is the part where i sum the digits in the link list.I was thinkin along the lines of reversing the original list (list1 and list2) and storing it in new lists (r1 and r2).then i will traverse the new lists adding the first digits(which will be the last).is this a logical approach?im not sure how to do this part please help me.thanks

    i hope i explained the problem clear enough!
    if i input 11111111111111111111111111 in list1
    and i input 22222222222222222222222222 in list2
    i should print out 33333333333333333333333333




    here is my code:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct node{
            int num;
            struct node*next;
            }node,*nodeptr;
    
    void sl();
    nodeptr makenode(int);
    void print(nodeptr);
    nodeptr makelist();
    nodeptr reverse1(nodeptr);
    
    int main(){
        nodeptr List1=NULL,List2=NULL,r1=NULL,r2=NULL;
        
        printf("enter numbers for first large interger,type -999 to end data input: ");
        List1=makelist();
        sl();
        printf("enter numbers for second large interger,type -999 to end data input: ");
        List2=makelist();
        
        print(List1);
        sl();
        print(List2);
        sl();
        sl();
       r1 =reverse1(List1);
        print(r1);
         sl();
        
       r2 =reverse1(List2);
       print(r2);
       sl(); 
        
        
        system("pause");
        return 0;
    }
    
    nodeptr makenode(int n){
            nodeptr np=(nodeptr)malloc(sizeof(node));
            np->num=n;
            np->next=NULL;
            return np;
            }
    
    
    
    void  print(nodeptr np){
            while (np!=NULL){
                  printf("%d",np->num);
                  np=np->next;
                  }
                  }
                  
                  
                  
          
    
    nodeptr makelist(){
            nodeptr makenode(int),np,top,last;
            int n;
            top=NULL;
            scanf("%d",&n);
            while(n!=-999){
                           np=makenode(n);
                           if(top==NULL)top=np;
                           else  last->next=np;
                           last=np;
                           scanf("%d",&n);
                           }
                           return top;
                           }
                           
    void sl(){
         printf("\n");
         }    //just skips a line.
    
    
    
    
        nodeptr reverse1(nodeptr top){
                
                nodeptr newtop=NULL,temp=NULL;
                while(top!=NULL){
                                 temp=top;
                                 top=top->next;
                                 temp->next=newtop;
                                 newtop=temp;
                                 }
                                 return newtop;
                                 }
    [\code]

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    A linked list seems a little overcomplicated. If you are trying to make something like a "big int" class, most (as far as I know), including one that I wrote awhile ago, use dynamic char arrays.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    I must use linked lists to solve

  4. #4
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Why? Homework?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    nope im studyin for final exams and this is a past paper question.can you point out to me why my reverse list is not functioning?im stuck there for the longest while

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nefsan View Post
    nope im studyin for final exams and this is a past paper question.can you point out to me why my reverse list is not functioning?im stuck there for the longest while
    What's wrong with it?

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    well it's not reversing.plus i think the whole solution is wrong as it cant take more than 20+ numbers into one node

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    how am i supposed to read such a large intger number by number into the list?do i scan it in with a &#37;s or something?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, that would work. But you'd need to use an array of characters (a string) to store the data. And you'd need to do the addition yourself -- but you need to do that anyway. Don't forget about carrying . . . .

    However, I think what you're really looking for is &#37;c. That lets you read one character at a time, which you can then stick into the linked list. Note that you'll probably want to ignore all characters that aren't digits. (Check out isdigit() from <ctype.h>.) %c reads into a char. You can convert this to a single-digit number (assuming the character is a digit) by subtracting '0'.
    Code:
    char c = '3';
    int n = c - '0';
    if(n == 3) {
        puts("It worked.");
    }
    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.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    im not sure i follow ,so i need to use a char aray to store the number then?i can only think of using the scanf("&#37;s") to store the number into the array.how would i use the %c?

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    okay i did the question using char arrays and link list.everything works except my sum.where is my sum function messed up and how do i fix it?..yay im almost done

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    typedef struct node{
            int num;
            struct node*next;
            }node,*nodeptr;
            
    void sl();
    nodeptr sum(nodeptr,nodeptr);
    nodeptr makenode(int);
    void print(nodeptr);
    nodeptr makelist(char[]); 
    nodeptr reverse1(nodeptr);       
     nodeptr sum(nodeptr,nodeptr);       
    int main(){
        char list1[50],z;
        char list2[50];
        int count=0;
        int i=0,t=0,temp;
        nodeptr List1=NULL,List2=NULL,sumlist=NULL;
        
        
    
    // initializin array to be a character    
        printf("enter integers for list1 : ");
        scanf("&#37;s",&list1);
        
        printf("Enter integers for List2 : ");
        scanf("%s",&list2);
        
        List1=makelist(list1);
        List2=makelist(list2);
        sl();
        sl();
        
     print(List1);
     sl();
     print(List2);
     sl();
     
     sumlist=sum(List1,List2);              
        
        
        
        system("pause");
        return 0;
    }
    nodeptr makenode(int n){
            nodeptr np=(nodeptr)malloc(sizeof(node));
            np->num=n;
            np->next=NULL;
            return np;
            }
    
    
    
    void  print(nodeptr np){
            while (np!=NULL){
                  printf("%d",np->num);
                  np=np->next;
                  }
                  }
                  
                  
    nodeptr makelist(char list1[]){
            nodeptr makenode(int),np,top,last;
            int i=0;
            char c = list1[i];
            int n;
            top=NULL;
            
            while(c!='\0'){
                           n = c - '0';
                           np=makenode(n);
                           if(top==NULL)top=np;
                           else  last->next=np;
                           last=np;
            
                           c = list1[++i];
                           }
                           return top;
                           }
                           
    void sl(){
         printf("\n");
         }    //just skips a line.
    
    
    
    nodeptr reverse1(nodeptr top){
                
                nodeptr newtop=NULL,temp=NULL;
                while(top!=NULL){
                                 temp=top;
                                 top=top->next;
                                 temp->next=newtop;
                                 newtop=temp;
                                 }
                                 return newtop;
                                 }           
    
    
    
    nodeptr sum(nodeptr l1,nodeptr l2){
            nodeptr l3=NULL,last,r1=NULL,r2=NULL;
            
            int n,m;
            
            
            r1=reverse1(l1);
            r2=reverse1(l2);
            
            
            while(r1!=NULL && r2!=NULL){
                           
                           n= r1->num + r2->num;
                           if(n<10){
                                    if(l3==NULL)l3=n;
                                    else last->next=n;
                                    last=n;
                                    }
                           else m=n-10;
                           if(l3==NULL)l3=m;
                           else last->next=m;
                           last=m;
                           r1=r1->next +1;
                           r2=r2->next;
                           
                           
                           if(l3==NULL)last->next=r2;
                           else last->next=r1;
                           return l3;

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    i modified it and it still deosnt work please help!
    Code:
    nodeptr sum(nodeptr l1,nodeptr l2){
            nodeptr l3=NULL,last,r1=NULL,r2=NULL,np,mp;
            
            int n,m;
            
            
            r1=reverse1(l1);
            r2=reverse1(l2);
            
            
            while(r1!=NULL && r2!=NULL){
                           
                           n=r1->num + r2->num;
                           np=makenode(n);
                           if(n<10){
                                    if(l3==NULL)l3=np;
                                    else last->next=np;
                                    last=np;
                                    }
                           else m=n-10;
                           mp=makenode(m);
                           if(l3==NULL)l3=mp;
                           else last->next=mp;
                           last=mp;
                           r1=r1->next +1;
                           r2=r2->next;
                           
                           
                           if(l3==NULL)last->next=r2;
                           else last->next=r1;
                           return l3;
                           }
                           }

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    okay i figured it out thanks for the help.............................................. ........or no .

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How did you do it? Do you want to perhaps post your latest code so that we can comment on it?

    Note that in C, a function declared as func() doesn't mean that function takes no parameters. It means that you don't know or don't care or whatever how many parameters that function takes. If you want to declare or prototype a function that takes no parameters, using func(void) is a better idea. With
    Code:
    void func();
    func(1, 2, 3);
    the compiler won't give you any warnings, but with
    Code:
    void func(void);
    func(1, 2, 3);
    it will. Clearly, you're passing three parameters to a function that takes none.
    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. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    56
    SURE HERE IS THE CODE it took some adjustments in the sum function simple stufff but it drained my brain.lol!
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    
    typedef struct node{
            int num;
            struct node*next;
            }node,*nodeptr;//STRUCTURE DECLARED
            
    //FUNCTIONS DECLARED//        
            
    void sl();
    nodeptr sum(nodeptr,nodeptr);
    nodeptr makenode(int);
    void print(nodeptr);
    nodeptr makelist(char[]); 
    nodeptr reverse1(nodeptr);       
     nodeptr sum(nodeptr,nodeptr);  
     
     //============================================================//     
    int main(){
        char list1[50],list2[50];//declaration of char array
         
        nodeptr List1=NULL,List2=NULL,sumlist=NULL;
        
          
        printf("enter integers for list1 : ");
        scanf("&#37;s",&list1);//inputs into the array
        
        printf("Enter integers for List2 : ");
        scanf("%s",&list2);
        
        List1=makelist(list1);//creates a link list with the values from the array
        List2=makelist(list2);
        sl();
        sl();
     printf("List 1 : ");   
     print(List1);
     sl();
     printf("List 2 : ");
     
     print(List2);
     sl();
     printf("__________________");
     sl();
     printf("SUM    : ");
     print(reverse1(sum(List1,List2)));              
      sl();
      sl();  
        
        system("pause");
        return 0;
    }
    
    //==================================================//
    nodeptr makenode(int n){
            nodeptr np=(nodeptr)malloc(sizeof(node));
            np->num=n;
            np->next=NULL;
            return np;
            }//creates a node for the list
    
    //================================================//
    
    void  print(nodeptr np){
            while (np!=NULL){
                  printf("%d",np->num);
                  np=np->next;
                  }
                  }
                  
     //==============================================//             
    nodeptr makelist(char list1[]){
            nodeptr makenode(int),np,top,last;
            int i=0;
            char c = list1[i];//char at the 1st position of the array
            int n;
            top=NULL;
            
            while(c!='\0'){//while char is not the end of the aray
                           n = c - '0';//makes the char into an int
                           np=makenode(n);//makes the node for the int
                           if(top==NULL)top=np;
                           else  last->next=np;
                           last=np;
            
                           c = list1[++i];//increments the list
                           }
                           return top;
                           }
     //================================================//                      
    void sl(){
         printf("\n");
         }    //just skips a line.
    
    
    //================================================//
    nodeptr reverse1(nodeptr top){//reverses the list
                
                nodeptr newtop=NULL,temp=NULL;
                while(top!=NULL){
                                 temp=top;
                                 top=top->next;
                                 temp->next=newtop;
                                 newtop=temp;
                                 }
                                 return newtop;
                                 }           
    
    
    //===============================================//
    nodeptr sum(nodeptr l1,nodeptr l2){
            nodeptr l3=NULL,last,r1=NULL,r2=NULL,np,mp;
            
            int n,num1,num2,carr=0;
            
            
            r1=reverse1(l1);//reverses the list to add the numbers
            
            r2=reverse1(l2);
      
          
         while(r1!=NULL || r2!=NULL){
                           if (r1 != NULL){ num1 = r1 -> num; r1 = r1 -> next;}
                           else num1 = 0;
                           if (r2!=NULL){num2 = r2 -> num; r2 = r2 -> next;}
                           else num2=0;
                           
                           n=num1 + num2 + carr;
                           carr = 0;
                           
                           if(n<10) np=makenode(n);
                           else {
                                np=makenode(n-10);
                                carr = 1;
                                }
                                if(l3==NULL)l3=np;
                                else last->next=np;
                                last=np;
                                
                           
                           
                           }
                          if(carr==1)last->next=makenode(carr);
                                      
                                      
                           return l3;
                           }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  2. Help needed Please
    By jereland in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 05:30 AM
  3. Add and delete functions
    By Ana Val sazi in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 09:59 PM
  4. Max Digits
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:28 AM