add two large numbers

This is a discussion on add two large numbers within the C Programming forums, part of the General Programming Boards category; I was trying to add 2 large numbers stored in two different circular linked lists. The result will be stored ...

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    add two large numbers

    I was trying to add 2 large numbers stored in two different circular linked lists. The result will be stored in another linked list. I tried to wrote the whole code but got stuck with my create() function. I'm only showing the part of the code which is not working.
    Code:
    struct node
    {
    	int info;
    	struct node *next;
    };
    struct node *getnode(void)
    {
    	struct node *p;
    	p=malloc(sizeof(struct node));
    	return p;
    }
    void create(struct node *r)
    {
    	int num;
    	if(r==NULL)
    	{
    		r=getnode();
    		r->info=-1;
    		r->next=r;
    	}	else
    	{
    		printf("Enter 5 digits or less");
    		scanf("%d",&num);
    		insafter(r,num);
    	}
    }
    int main(void)
    {
    	struct node *p=NULL,*q=NULL;
    	int ch;
    	char ch2;
    	while(1)
    	{
    	printf("What do you wanna do?\n1.make a list\n2.sum of the integers\n3.exit");
    	scanf("%d",&ch);
    	switch(ch)
    	{
    	case 1:
    		{
    			printf("Which list p or q");
    			scanf(" %c",&ch2);
    			switch(ch2)
    			{
    			case 'p':case 'P':
    				create(p);
    				break;
    			case 'q':case 'Q':
    				create(q);
    				break;
    			}
    		}
    		break;
    	case 2:
    		{
    			s=addint(p,q);
    			printf("The sum is");
    			for(s=s->next;s->info!=-1;s=s->next)
    			printf("%d",s->info);
    		}
    		break;
    	case 3:exit(0);
    	}
    	}
    }
    Firstly I choose the option 1. to make a list, then option p for creating the list named p, here it goes in the create() function. When I try to make a circular list using create() function it gets stuck to the if condition(which is always getting true), i.e r is always coming out to be NULL in each call to create(). Why is this happening?
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Two things :
    1) Are you sure that case 'p' or 'q' always work fine ?
    You should have also a default case.
    Better remove the space before scanf(" %c",&ch2);, you may read \n.

    2) (More question than notation) As far as i can understand from your code you want to add.. etc two large numbers, but your struct stores more than one digit right ?
    Now imagine those lists (as |X| a node with a number in it)

    p = |1|2|3|45|5023|
    q = |2|6|11|4|

    How would you add these two lists ?
    See 5023 should be added to the whole q list and the rest is just p.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Surely you're not expecting the function to be able to change the value of r as seen by the program? (It can change the value pointed to by r, but not make it point somewhere else, and if r doesn't point to anything valid then you're stuck.)

    And I'm not sure what ch4 means by the bit in red; the point of the code in red is to remove the \n from what is read in? That looks fine to me.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tabstop View Post
    Surely you're not expecting the function to be able to change the value of r as seen by the program? (It can change the value pointed to by r, but not make it point somewhere else, and if r doesn't point to anything valid then you're stuck.)

    And I'm not sure what ch4 means by the bit in red; the point of the code in red is to remove the \n from what is read in? That looks fine to me.
    This is what I'm doing. For the first node(which I'm making it a header), the info field is -1, and the next field is that node itself as it's the first node in a circular list. For that I'm allocating space for the pointer using getnode(), the next time a node is inserted I'm inserting it after the header node. But I dont understand what you're saying, how is it NULL everytime?

    And yes I'm using space in scanf to ignore the \n in the buffer.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BEN10 View Post
    This is what I'm doing. For the first node(which I'm making it a header), the info field is -1, and the next field is that node itself as it's the first node in a circular list.
    And then you throw it away. You've seen this, I'm sure:
    Code:
    #include <stdio.h>
    
    void change(int i) {
        i = 10;
    }
    
    int main() {
        int i = 5;
        change(i);
        printf("%d\n", i);
        return 0;
    }
    It prints five, right? Changes made in functions disappear when the function ends.

    (If you like, a closer example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void change (int *i) {
        i = NULL;  //make it go away
    }
    
    int main(void) {
        int x = 5;
        int *i = &x;
        printf("%p\n", i);
        change(i);
        printf("%p\n", i);
        return 0;
    }

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks. Now I get it. I've corrected it and that problem has gone. But now it's not printing the sum, but I'll try it a bit and then come with the code(if it troubles me).
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Ok. Now after spending some time with the code, I found that all mistake was done in the creation of circular list. So I decided to write another program for circular list. Thsi is what I wrote.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    struct node
    {
    	int info;
    	struct node *next;
    };
    void display(struct node *p)
    {
    	struct node *q;
    	for(q=p;((q->next)->info)!=-1;q=q->next)
    		printf("\n%d",q->info);
    }
    struct node *getnode(void)
    {
    	struct node *p;
    	p=malloc(sizeof(struct node));
    	return p;
    }
    void insafter(struct node *p,int x)
    {
    	struct node *q;
    	if(p==NULL)
    	{
    		printf("void insertion");
    		getch();
    		exit(1);
    	}
    	q=getnode();
    	q->info=x;
    	q->next=p->next;
    	p->next=q;
    }
    void create(struct node **r)
    {
    	int num;
    	if(*r==NULL)
    	{
    		*r=getnode();
    		(*r)->info=-1;
    		(*r)->next=*r;
    	}
    	else
    	{
    		printf("Enter number");
    		scanf("%d",&num);
    		insafter(*r,num);
    	}
    }
    int main(void)
    {
    	struct node *p=NULL;
    	int ch;
    	while(1)
    	{
    		printf("What do you wanna\n1.make a list\n2.display\n3.exit");
    	scanf("%d",&ch);
    	switch(ch)
    	{
    	case 1:
    		create(&p);
    		break;
    	case 2:
    		display(p);
    		break;
    	case 3:
    		exit(0);
    		break;
    	}
    	}
    	getch();
    }
    Now the problem with the code is that it doesn't displays the contents of the node properly. Suppose that I enter the nodes with value of (except for the very first node which has info=-1) 2,3. Then I choose the option of displaying it and it prints -1 and 3 not 2. Why is this happening? I'm not sure whether my insafter() function is correct or not.
    And one more thing, the way I understand the code, please tell me if I'm correct or not. Firstly p points to the header node with info field -1. Then as I insert more nodes then q points to them in succession, I mean if I enter 2 for the second node's info, q(in insafter()) points to this node, as I ask for another node with info 3, q points to the new node(with info 3). My doubt is, Is p pointing always to the header node and it's q which points to the newly allocated node or it's p which is moving through the nodes?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want to print the entire list, you should probably let your for loop run to the end of the list instead of stopping early.

  9. #9
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    What's wrong with my for loop? It's starting from the header till the last node.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BEN10 View Post
    What's wrong with my for loop? It's starting from the header till the last node.
    That's the point. It stops when it reaches the last node. That means it doesn't actually execute the body of the for loop for the last node....

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure why you're using circular lists. Or are you?
    I was trying to add 2 large numbers stored in two different circular linked lists.
    It seems like you're making too much out of this. What's the point of them being circular? It just makes running through them harder than it has to be.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tabstop View Post
    That's the point. It stops when it reaches the last node. That means it doesn't actually execute the body of the for loop for the last node....
    Ok. Now my question will sound slightly stupid but here it is. If I input the nodes in this order
    -1->2->3 then should I print it like the same order or is it ok with this order also -1->3->2? By the way -1 is the info field of the header node.

    Quote Originally Posted by quzah View Post
    I'm not sure why you're using circular lists. Or are you?It seems like you're making too much out of this. What's the point of them being circular? It just makes running through them harder than it has to be.


    Quzah.
    Actually this all started when I was studying the book. A simple example was given in the book of adding two large numbers. So I decided to write a program to add two large numbers using circular lists. But then I got stuck in the very first thing i.e in constructiing a circular list. It's just for practice.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BEN10 View Post
    Ok. Now my question will sound slightly stupid but here it is. If I input the nodes in this order
    -1->2->3 then should I print it like the same order or is it ok with this order also -1->3->2? By the way -1 is the info field of the header node.
    Whatever you want. You're building your list in a strange way to try to print it out in the order it was given, though.

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tabstop View Post
    Whatever you want. You're building your list in a strange way to try to print it out in the order it was given, though.
    Now I'm done with the circular list creation and it prints the in the correct order.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Yipee. I've made the code for adding two large integers and it's working perfectly fine. Thanks tabstop for your help.
    By the way will it be efficient to find the factorial of 3 digit numbers using this method which uses circular list. (by placing the digits of the answer in the nodes)
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add two numbers
    By Rajesh Chandra in forum C Programming
    Replies: 3
    Last Post: 02-15-2009, 10:59 PM
  2. Storing large numbers as an array
    By Rush152 in forum C Programming
    Replies: 9
    Last Post: 05-19-2006, 11:51 PM
  3. Large Numbers
    By Hypercase in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2004, 06:56 PM
  4. rather large numbers
    By crepincdotcom in forum C Programming
    Replies: 15
    Last Post: 08-27-2004, 09:27 AM
  5. How to add an array of numbers in a struct node ?
    By emilyashu in forum C Programming
    Replies: 1
    Last Post: 04-26-2003, 04:52 AM

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