Thread: how to add 2 digits?

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    proper indentation never hurts... Improper indentation like that you are demonstrating - could hurt you severely

    scanf("%s",&list1); - no,no,no - no need to & when list1 is already pointer to string (array is pointer to its first element)

    you do not need to cast malloc in C - read FAQ
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To add to what vart said: you should also free() any memory that you malloc(), or you'll probably end up with some memory leaks.

    Indentation suggestion: here's what "indent -kr" spits out. It could be better, but it's better than what you have.
    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;
    }
    Doesn't it seem a little easier to read? . . . .
    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.

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