0

I have to do a code for school which counts sum of digits. As it has to be working with big numbers (80 000 digits+) I had to do first count as an array as it was impossible to put this big number even in long long int. My question is why is this code not working? (works with smaller numbers for example: 10^100) but when I try really big numbers (10^10000) it does not work properly. Could anyone help me out by saying how or help me fix this? Thanks

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char pole [100000];
    int c = 0;
    int sum = 0;
    int x = 0;
    int t;

    printf("Nacitaj cislo!\n");
    scanf("%s", pole);
    printf("Zadal si: %s\n", pole);

    while (pole[c] != '\0') {
      t   = pole[c] - '0';
      sum = sum + t;
      c++;
   }

   while(1){
        while(sum != 0){
            x = x + sum % 10;
            sum = sum/10;
        }
        if(x > 10){
            sum = x;
        }
        else{
            break;
        }
   }

    printf("%d\n", x);
    return 0;
}
  • 2
    Define *it does not work properly* please? Are you getting negative values in example? – Cid Mar 21 '19 at 09:34
  • 2
    What exactly is not working? What catches the eye is the `char pole [100000];`, does it work if you do `char *pole = malloc(100000);` instead and then `free(pole);` at the end? – Blaze Mar 21 '19 at 09:35
  • +1 to Blaze. Also might be worth taking a look at https://stackoverflow.com/questions/3144135/why-do-i-get-a-segfault-in-c-from-declaring-a-large-array-on-the-stack – Simon F Mar 21 '19 at 09:38
  • @Cid yes I am getting random negative values – Róbert Porubän Mar 21 '19 at 09:39
  • @Blaze I tried doing that, however it is still not working and I am getting random negative numbers (-2147483615 for exmaple) – Róbert Porubän Mar 21 '19 at 09:40
  • 1
    @RóbertPorubän You are getting negative values because at some point, you are overflowing the max size of an int. Try storing the value in a char array instead of in a number – Cid Mar 21 '19 at 09:41
  • 1
    In your last loop, where you finally calculate the digit root, shouldn't you reset `x` for each step? – M Oehm Mar 21 '19 at 09:44
  • @Cid but even if I am working with 80 000 digits number, the sum of that number cannot be bigger than 80000*9 = 720 000 which should not be the problem as it should not be overflowing the max size of int, shouldnt it? – Róbert Porubän Mar 21 '19 at 09:45
  • @MOehm that`s it and that`s why I was indeed overflowing int as @Cid said, thank you very much guys! – Róbert Porubän Mar 21 '19 at 09:49
  • Cool By the way, you don't need that last loop at all: You can sum the digits modulo 9, see [here](https://en.wikipedia.org/wiki/Digital_root). – M Oehm Mar 21 '19 at 09:53
  • @MOehm thats interesting, havent thought of that, will try it – Róbert Porubän Mar 21 '19 at 10:00
  • You also don't need a large array (which may overflow your stack). Reading one character at a time should be perfectly fine. Read one, add it to the sum, forget about it. – n. m. could be an AI Mar 21 '19 at 11:22

2 Answers2

0

The x value should be reset to 0 in while block

while(1) {
    x = 0; // add here
    while (sum != 0) {
    ...
}
ccxxshow
  • 844
  • 6
  • 5
0
 while(1){
    while(sum != 0){
        x = x + sum % 10;
        sum = sum/10;
    }
    if(x > 10){
        sum = x;
    }
    else{
        break;
    }
}

The problem here is that value x is not reset back to 0 after each iteration of the outer while loop. Since you are not resetting back, you would run into the problem of overflow of int. I guess you need to be careful when you play around with such loop iterations.

You could change the code to a way as described by @ccxxshow.

m4n1c
  • 127
  • 8