Back to Problem Solutions forum
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;
}
Hint: Read the last line first to get the mod value. Then after each operation apply % mod.
Hope this helps ;)
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.
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.
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...
I use D which makes implementation of dynamic arrays quite easy. I'd also tried C/C++ but found things somewhat over-complicated.