[14] [C] How do we deal with large intermediate numbers?

Back to Problem Solutions forum

ugsdhfjbnsja     2015-07-18 11:53:21

I am writing #14 in C. However I am fidning it difficult to complete becuase of the insanely large intermediate numbers in the test data.

My programs works perfectly for any intermediate numbers below INT-MAX but this restraint is breaking my calculation for larger inputs.

How can I go about using large numbers?

I tried using doubles rather than ints as my main data type but could never get this to work properly. Is this one way of going about it?

My code so far:

#include <stdio.h>

#define FALSE 0
#define TRUE !FALSE

int main(){
    int workingResult;
    scanf(" %d", &workingResult);

    char operation;
    int newValue;
    int modFlag = FALSE;    // Defines whether modulus operation has been hit

    do{
        scanf(" %c %d", &operation, &newValue);

        switch(operation){

            case '+':
                workingResult += newValue;
                break;

            case '*':
                workingResult *= newValue;
                break;

            case '%':
                workingResult %= newValue;
                modFlag = TRUE;
                break;

            default :
                return 1; // Error 1: Invalid operator
        }

    } while(modFlag != TRUE);

    printf("%d", workingResult);

    return 0;
}
Ratzenfutz     2015-07-18 12:08:04
User avatar

Hint: Read the last line first to get the mod value. Then after each operation apply % mod.

Hope this helps ;)

ugsdhfjbnsja     2015-07-18 12:25:30

I'll give it a go. I'll have to pretty much rewrite the entire program to do that then I beleive. C is horrible for reading input. Thanks.

ugsdhfjbnsja     2015-07-18 13:21:19

I got it done with your suggestion, thank you Ratenfutz.

It's actually a very simple procedure to do but C makes the job much harder than other languages becuase of dynamic memory allocation being necassary in this case and the fact that input is notoriously difficult in C.

Rodion (admin)     2015-07-18 20:50:46
User avatar

I also thank Ratzenfutz for speedy help!

Well, it is only partially the fault of C. When I've tried to write this in Scala (the same for any "functional" approach) I've run into the same trouble - things would be quite easier if the divisor is given in the first line instead of last.

If I met such case in industry I surely will try to persuade colleagues to change the incoming data flow format - anyway if we'll need to process, say, billion of operations we could not easily save all stuff in the memory temporarily... :)

Concluding - I'm glad that problems with large values are still encountered. People writing in Python regretfully do not see any puzzle in this problem since they use endless numbers. This language removes any opportunity for thinking, ha-ha...

Ratzenfutz     2015-07-18 21:53:12
User avatar

I use D which makes implementation of dynamic arrays quite easy. I'd also tried C/C++ but found things somewhat over-complicated.

Please login and solve 5 problems to be able to post at forum