Thread: plz help!sth wrong!

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    2

    plz help!sth wrong!

    Hi,

    i'm totally new to c programming and i have just began writing code.What i want to make is a program that adds (and another one that subtracts) 2 numbers, =no matter how big they are= and i just can't do it.Can someone help?Below is the code that i wrote with some help:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void main()
    {
    	int *a,*b,*c;
    	char buf[10000];
    	char buf2[10000];
    	int n, i;
    
    	printf("\nGive a number: ");
    	scanf("&#37;s",buf);
    	n= strlen(buf);
    
    	printf("\n Number: %s \n",buf);
    	printf("Length of the number: %d \n ",n);
    	a=(int*)malloc(n*sizeof(int));
    	*a=atoi(buf);
    
    /*for (i=0;i<n;i++){
    					a[i]=buf[n-i-1]-'0';
    					}*/
    
    	printf("\nGive a number: ");
    	scanf("%s",buf2);
    	n= strlen(buf2);
    	printf("\n Number: %s \n",buf2);
    	printf("Length of the number: %d \n ",n);
    	b=(int *) malloc(n*sizeof(int));
    	*b=atoi(buf2);
    /*for (i=0;i<n;i++){
    				b[i]=buf2[n-i-1]-'0';
    				}*/
    
    
    	c=(int *) malloc(n*sizeof(int));
    	*c= *a-*b;
    
    	printf("\n First Number : %d \n",*a);
    	printf("\n Second Number : %d \n",*b);
    
    	printf("\n Result : %d \n",*c);
    
    }
    Thanx in advance!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Arbitrary precision arithmetic in general is hard. For adding two integers, it's relatively simple, if you don't need the fastest possible implementation. You can do it the same way as by hand - you add one pair of digits at a time, from right to left, keeping track of the carry. For efficiency you should rely on existing libraries, such as GMP.

    http://en.wikipedia.org/wiki/Bignum

    http://en.wikipedia.org/wiki/GNU_Mul...cision_Library

    Edit: Also, it's "int main(void)" and not "void main()", and you don't need to cast malloc().

    http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Last edited by robatino; 05-25-2007 at 09:30 AM.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You cant read not more than 32,767 or in other words u cant store not more than 32,767 on an integer datatye. There are algorithms to add a very long number.

    Robatina has a good and a simple one. You could try that. And i also noticed that why do u want it to be int * to store th return value of atoi. You could just store them on a int.

    And do not use scannf to read a string. Cos it dosn't recoganise the words after the white space. Which you will find a problem in the future.

    ssharish2005

  4. #4
    Registered User
    Join Date
    May 2007
    Posts
    2

    program for addin/subtracting very big numbers

    Quote Originally Posted by ssharish2005 View Post
    And do not use scannf to read a string. Cos it dosn't recoganise the words after the white space. Which you will find a problem in the future.

    ssharish2005
    What should i use instead of scanf?thanks for the correction

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    What should i use instead of scanf?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. [Request] Need Help Plz
    By TylerD in forum Tech Board
    Replies: 4
    Last Post: 01-03-2009, 09:54 AM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM