Drive me crazy...About plus calculation by array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • upyzl

    Drive me crazy...About plus calculation by array

    #include <stdio.h>
    #define digit 21

    int main()
    {
    int a, b, max;
    int i = 0;
    int j = 0;
    int x[digit - 1] = { 0 };
    int y[digit - 1] = { 0 };
    int z[digit] = { 0 };//result

    printf("Input the first number:\n");
    scanf("%d", &a);
    printf("Input the second number:\n");
    scanf("%d", &b);

    while ( a != 0 ) {
    x[i] = a % 10;
    i++;
    a /= 10;
    }
    while ( b != 0 ) {
    y[j] = b % 10;
    j++;
    b /= 10;
    }

    max = i;
    if ( j i ) {
    max = j;
    }

    for ( ; max >= 0; max--) {
    z[max] = x[max] + y[max];
    if ( z[max] >= 10 ) {
    z[max+1]++;
    z[max] -= 10;
    }
    printf("%d", z[max]);
    }
    printf("\n");
    return 0;
    }
  • upyzl

    #2
    Re: Drive me crazy...About plus calculation by array

    Where on earth does it error??

    Comment

    • Eric Sosman

      #3
      Re: Drive me crazy...About plus calculation by array

      upyzl wrote:
      #include <stdio.h>
      [... code snipped; see up-thread ...]
      It would be a good idea to explain what your code is
      supposed to do (we can only see what it actually does), and
      how what it actually does fails to meet your purposes. Do
      you take a vow of silence before you visit your doctor?

      Still, I can see at least one thing that looks wrong.
      You can probably discover it for yourself by working through
      a small example with pencil and paper: Write down the values
      of the program's variables and start following through the
      code step by step, as if you were the computer (this can be
      a surprisingly effective way to discover mistakes). An
      example that highlights the flaw I spotted is to try adding
      181 and 19.

      (A hint for future revisions: When adding the numbers,
      you may find it easier to start at the ones' place and work
      upward than to start at the topmost digit and work down.)

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • upyzl

        #4
        Re: Drive me crazy...About plus calculation by array

        On 11ÔÂ16ÈÕ, ÏÂÎç10ʱ37·Ö, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
        upyzl wrote:
        #include <stdio.h>
        [... code snipped; see up-thread ...]
        >
        It would be a good idea to explain what your code is
        supposed to do (we can only see what it actually does), and
        how what it actually does fails to meet your purposes. Do
        you take a vow of silence before you visit your doctor?
        >
        Still, I can see at least one thing that looks wrong.
        You can probably discover it for yourself by working through
        a small example with pencil and paper: Write down the values
        of the program's variables and start following through the
        code step by step, as if you were the computer (this can be
        a surprisingly effective way to discover mistakes). An
        example that highlights the flaw I spotted is to try adding
        181 and 19.
        >
        (A hint for future revisions: When adding the numbers,
        you may find it easier to start at the ones' place and work
        upward than to start at the topmost digit and work down.)
        >
        --
        Eric Sosman
        esos...@ieee-dot-org.invalid
        I mean, I just want to do a plus calculation, but numbers' digits are
        over 10(such as 20)

        Comment

        • Barry Schwarz

          #5
          Re: Drive me crazy...About plus calculation by array

          On Mon, 17 Nov 2008 04:52:41 -0800 (PST), upyzl <zj262144@163.c om>
          wrote:
          >On 11??16??, ????10??37??, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
          >upyzl wrote:
          #include <stdio.h>
          [... code snipped; see up-thread ...]
          >>
          > It would be a good idea to explain what your code is
          >supposed to do (we can only see what it actually does), and
          >how what it actually does fails to meet your purposes. Do
          >you take a vow of silence before you visit your doctor?
          >>
          > Still, I can see at least one thing that looks wrong.
          >You can probably discover it for yourself by working through
          >a small example with pencil and paper: Write down the values
          >of the program's variables and start following through the
          >code step by step, as if you were the computer (this can be
          >a surprisingly effective way to discover mistakes). An
          >example that highlights the flaw I spotted is to try adding
          >181 and 19.
          >>
          > (A hint for future revisions: When adding the numbers,
          >you may find it easier to start at the ones' place and work
          >upward than to start at the topmost digit and work down.)
          >>
          >--
          >Eric Sosman
          >esos...@ieee-dot-org.invalid
          >
          >I mean, I just want to do a plus calculation, but numbers' digits are
          >over 10(such as 20)
          We understand that. Eric gave you a very good method to find the
          error in your logic. If you do what he suggested, it should be
          obvious where you need to adjust your code.

          --
          Remove del for email

          Comment

          • CBFalconer

            #6
            Re: Drive me crazy...About plus calculation by array

            Barry Schwarz wrote:
            upyzl <zj262144@163.c omwrote:
            >Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
            >>
            .... snip ...
            >>
            >> (A hint for future revisions: When adding the numbers,
            >>you may find it easier to start at the ones' place and work
            >>upward than to start at the topmost digit and work down.)
            >>>
            >>-- <
            >>Eric Sosman <
            >>esos...@iee e-dot-org.invalid <
            ^^^^^ above is a signature ^^^^'
            >>
            >I mean, I just want to do a plus calculation, but numbers' digits
            >are over 10(such as 20)
            >
            We understand that. Eric gave you a very good method to find the
            error in your logic. If you do what he suggested, it should be
            obvious where you need to adjust your code.
            And the simple technique of examining your reply before hitting
            'send' will expose the fact that you have failed to purge
            signatures. Those are everything following the "__ " alone marker.

            --
            [mail]: Chuck F (cbfalconer at maineline dot net)
            [page]: <http://cbfalconer.home .att.net>
            Try the download section.

            Comment

            Working...