sum char array

This is a discussion on sum char array within the C Programming forums, part of the General Programming Boards category; I am traying to make program that takes from input strings s1 and s2 and then write them out in ...

  1. #1
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178

    sum char array

    I am traying to make program that takes from input strings s1 and s2 and then write them out in output string s3 in what are sum of numbers thar are write in s1 and s2.
    for example: if is
    s1='11111111'
    s2='11112222' then program must sum s3 and like rezult have to get
    s3='22223333'

    for example: if is
    s1 = '19'
    s2 = '1001', then
    s3 = '1020'.

    But I have little trouble. Almost everything is doing just fine!
    First here you are my code and after code I will put my results. And there you will see what is wrong!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 1024
    
    void zbroji( char*, char*, char*); 
                   
    int main()
    {
        char s1[SIZE]; char s2[SIZE]; char s3[SIZE]; int i = 0;
        scanf("%s", s1);
        scanf("%s", s2);
        
        zbroji(s1, s2, s3);
        
        while( s3[i] != '\0')     /*Here need to be printf("%s", s3); but then is something wrong*/
        {
            printf("%d", s3[i]);
            i++;
        } 
        printf("\n");    
         system("PAUSE");
         return 0;
    }
    
    void zbroji(char a[SIZE], char b[SIZE], char c[SIZE])
    {
        int i = 0, j = 0, k = 0, l = 0; int lena = 0, lenb = 0;
        int BIG = 0; int salji = 0; int SMALL = 0; char TEMP[SIZE]; int temp;
        
        while( a[lena] != '\0')
            lena++;
        while( b[lenb] != '\0')
            lenb++;
        lena -= 1; lenb -= 1;
        if( lena >= lenb)
        {
            BIG = lena;
            SMALL = lenb; 
        }
        else
        {
            BIG = lenb;
            SMALL = lena;
        }
        temp = BIG;
        c[BIG + 1] = '\0';
        for( i = BIG; i >= 0; i--, lena--, lenb--, BIG--)
        {
            if( lena < 0 )
            {
    			j = 0;
            }
            else
            {
              if ( lenb < 0 )
    		  {
    		      k = 0;
    		  }
              else
    		  {
                    j = a[lena] - '0'; k = b[lenb] -'0';
    		  }
            }
    
            l = 0;       
            l = j + k + salji;
            if( l > 9)
            {
                if ( BIG == 0 )
                    c[BIG] = l;
                else
                {
                    c[BIG] = l - 10;
                    salji = 1;
                }
            }
            else
            {
                c[BIG] = l;
            }
        }    
    }
    Code:
    Output is:
    s1 = 1111
    s2 = 2222
    s3 = 3333    that's OK!
    
    s1 = 999
    s2 = 999 
    s3 = 1998 that's OK!
    
    s1 = 11
    s2 = 1111
    s3 = 1122 that's OK! 
    
    But when I make like this
    s1 = 555
    s2 = 5555
    s3 = 611    WRONG!
    or
    s1 = 9999
    s2 = 99
    s3 = 10      WRONG!
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Your code is really hard to read, mostly because of your variable naming scheme but partly because of your odd indentation style. I think I'd do it more like this:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAXLEN 4096
    
    void get_number(char *name, char *buf)
    {
      char *p;
    
      printf("%s number: ", name);
      fflush(stdout);
      fgets(buf, MAXLEN, stdin);
      if((p = strchr(buf, '\n')))
        *p = '\0';
    }
    
    void big_add(char *s1, char *s2, char *buf)
    {
      char temp[MAXLEN];
      char *p, *q, *t;
      int num, carry = 0;
    
      p = s1 + strlen(s1) - 1;
      q = s2 + strlen(s2) - 1;
      t = temp;
    
      while(p >= s1 || q >= s2)
      {
        num = carry;
        num += p >= s1 ? *p - '0' : 0;
        num += q >= s2 ? *q - '0' : 0;
    
        if((carry = num / 10))
          num -= 10;
    
        *t++ = num + '0';
        p--;
        q--;
      }
    
      if(carry && t - temp < MAXLEN)
        *t = '1';
      else
        t--;
    
      while(t >= temp)
        *buf++ = *t--;
      *buf = '\0';
    }
    
    int main(void)
    {
      char s1[MAXLEN], s2[MAXLEN], s3[MAXLEN];
    
      get_number("First", s1);
      get_number("Second", s2);
    
      big_add(s1, s2, s3);
    
      printf("Result: %s\n", s3);
      return 0;
    }
    Code:
    itsme@dreams:~/C$ ./bigadd
    First number: 555
    Second number: 5555
    Result: 6110
    itsme@dreams:~/C$ ./bigadd
    First number: 283749208374983274
    Second number: 746391823749187234
    Result: 1030141032124170508
    If you don't understand something in it just let me know.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Thanks!
    I never seen this before
    Code:
    get_number("First", s1);
    I will look at FAQ for this. I know is there somwhere
    Code:
    fflush(stdout);
      fgets(buf, MAXLEN, stdin);
    I undersand else mostly!
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    get_number("First", s1); just passes 2 strings to the get_number() function. I'm not sure what you don't understand about it.

    fflush(stdout); needs to be called when you print a string that doesn't end with a newline. It's not necessary on all OS's, but some won't display the string until a newline is printed or the buffer is flushed.

    fgets(buf, MAXLEN, stdin); is definitely in the FAQ. Look for the FAQ on getting input from the user.
    If you understand what you're doing, you're not learning anything.

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by xxxrugby
    I am traying to make program that takes from input strings s1 and s2 and then write them out in output string s3 in what are sum of numbers thar are write in s1 and s2.

    for example: if is
    s1 = '19'
    s2 = '1001', then
    s3 = '1020'.
    They invented this cool data type a while ago....maybe you've heard of it....it's called INT. If you want to add numbers together...use a freaking int. See dear, that makes things a lot easier.

    Instead of going through a bunch of tedious crap, you can use this great operator called '+'. It does wonders for adding numbers.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Quote Originally Posted by Krak
    They invented this cool data type a while ago....maybe you've heard of it....it's called INT. If you want to add numbers together...use a freaking int. See dear, that makes things a lot easier.

    Instead of going through a bunch of tedious crap, you can use this great operator called '+'. It does wonders for adding numbers.
    Try adding these 2 numbers using ints...

    itsme@dreams:~/C$ ./bigadd
    First number: 283749208374983274
    Second number: 746391823749187234
    Result: 1030141032124170508
    If you understand what you're doing, you're not learning anything.

  7. #7
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    An unsigned long int will give you as big of a number as you're ever going to need.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    An unsigned long int will give you as big of a number as you're ever going to need.
    In some fields (e.g. branches of mathematics, applications of cryptography) numbers of arbitrary precision need to be represented for certain calculations.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    isnt a unsigned long int = 4 bytes with values ranging from 0 - 4.294.967.295 ? how are they keeping track on the US national debt that is 7 trillion? silly question but im curious.
    Last edited by InvariantLoop; 03-12-2005 at 12:22 PM. Reason: typo
    When no one helps you out. Call google();

  10. #10
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    They could use an __int64.
    how are they keeping track on the US national debt that is 7 trillion
    EDIT
    According to http://www.brillig.com/debt_clock/ the debt is actually closer to 8 trillion.
    Scary
    /EDIT
    In some fields (e.g. branches of mathematics, applications of cryptography) numbers of arbitrary precision need to be represented for certain calculations.
    Indeed, an algorithm like RSA uses 768+ bits often, although i've never seen it used with more then 2048 bits.
    Last edited by MadCow257; 03-12-2005 at 01:03 PM.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Krak
    An unsigned long int will give you as big of a number as you're ever going to need.
    "640K ought to be enough for anybody."
    Quote Originally Posted by InvariantLoop
    isnt a unsigned long int = 4 bytes with values ranging from 0 - 4.294.967.295 ?
    That range is a minimum, the size is whatever it needs to be to contain that minimum -- one byte might suffice on some systems.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. code help required
    By Yobbo in forum C Programming
    Replies: 9
    Last Post: 09-02-2005, 11:15 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

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