0

I have a problem with output of the following C program. It does not get me the correct output.I transform this program from the sum of two big numbers. So I get the output with some "errors" like "232423-3-4-34--3424" instead of "23242334343424". How do i fix this?

 #include<stdio.h>
int main() {
  int num1[255], num2[255], dif[255];
  char s1[255], s2[255];
  int l1, l2;

  printf("Enter Number1:\n");
  scanf("%s", &s1);
  printf("Enter Number2:\n");
  scanf("%s", &s2);

  for (l1 = 0; s1[l1] != '\0'; l1++)
    num1[l1] = s1[l1] - '0';

  for (l2 = 0; s2[l2] != '\0'; l2++)
    num2[l2] = s2[l2] - '0';

  int carry = 0;
  int k = 0;
  int i = l1 - 1;
  int j = l2 - 1;
  for (; i >= 0 && j >= 0; i--, j--, k++) {
    dif[k] = (num1[i] - num2[j] - carry) % 10;
    carry = (num1[i] - num2[j] - carry) / 10;
  }
  if (l1 > l2) {

    while (i >= 0) {
      dif[k++] = (num1[i] - carry) % 10;
      carry = (num1[i--] - carry) / 10;
    }

  } else if (l1 < l2) {
    while (j >= 0) {
      dif[k++] = (num2[j] - carry) % 10;
      carry = (num2[j--] - carry) / 10;
   }
  } else {
    if (carry > 0)
      dif[k++] = carry;
  }


  printf("Result:");
  for (k--; k >= 0; k--)
    printf("%d", dif[k]);

  return 0;
}
bruno
  • 32,421
  • 7
  • 25
  • 37
  • 2
    Please show the expected and actual inputs and outputs of the code to help us help you. – Thomas Jager Jan 06 '19 at 21:13
  • `scanf("%s", &s1);` must be `scanf("%s", s1);` and `scanf("%s", &s2);` must be `scanf("%s", s2);` and add `putchar('\n');` at the end – bruno Jan 06 '19 at 21:49
  • OT: for ease of readability and understanding (by humans) 1) consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. 2) separate code blocks: `for` `if` `else` `while` `do...while` `switch` `case` `default` via a single blank line. 3) Variable ( and parameter ) names should indicate `content` or `usage` (or better, both). Names like: 'L1', 'L2', etc are meaningless even in the current context. – user3629249 Jan 07 '19 at 23:37
  • OT: regarding statements like: `scanf("%s", &s2);` 1) when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the input format specifiers: '%s' and/or '%[...]' always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer because those specifiers always append a NUL byte to the input. This also avoids any possibility of a buffer overflow and the resulting undefined behavior – user3629249 Jan 07 '19 at 23:40
  • OT: the posted code contains some 'magic' numbers. I.E. 10, 255. 'magic' numbers are numbers with no basis. 'magic' numbers make the code much more difficult to understand, debug, etc. Suggest using `#define` statements or a `enum` statement to give those magic numbers meaningful names, then use those meaningful names throughout the code – user3629249 Jan 07 '19 at 23:42
  • OT: for ease of understanding and readability: follow the axiom: *only one statement per line and (at most) one variable declaration per statement.* – user3629249 Jan 07 '19 at 23:44
  • You might want to look at the header file: `bignum.h` [found at](https://tls.mbed.org/api/bignum_8h_source.html) and other places plus the related file: `bitnum.c`. Those files implement the basic math operations on huge numbers and already handle all the corner cases. – user3629249 Jan 07 '19 at 23:51

1 Answers1

3

Your result includes negative numbers because the code doesn't correctly implement modulo-10 arithmetic. In lines like this:

dif[k] = (num1[i] - num2[j] - carry) % 10;

If subtraction num1[i] - num2[j] - carry is less than 0, you want to store the result of 10-subtraction. There are languages where the % operator works like that in, but in C it returns a negative number, so -1 % 10 yields -1 and not 9.

To fix the issue, the code needs to be more explicit, e.g.:

int sub = num1[i] - num2[j] - carry;
if (sub >= 0) {
    dif[k] = sub;
    carry = 0;
} else {
    dif[k] = 10 - sub;
    carry = 1;
}
user4815162342
  • 141,790
  • 18
  • 296
  • 355