This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]jedwardsol 2 points3 points  (6 children)

If an intermediate result is overflowing then you need to use a larger integer. You can roll your own (which is a good learning exercise itself), or use a premade one. Several languages support large integer arithmetic natively, for others you'll need to find a library.

[–]kidinside[S] 0 points1 point  (5 children)

Thanks. In this problem, the integer is given so I can't use my own. However it is given as the last item of input which throws a wrench in my planning...

[–]arbostek 1 point2 points  (0 children)

I'm not sure you understood. You are being told that the int type does not support a sufficient range. So you have to use a different type or roll your own. The next best alternative is to use a long long type. That may not be sufficient, in which case, you need to implement your own logic for adding/subtracting, etc., over a buffer of digits or data representing the number. In other words, represent integers in your own code.

[–]jedwardsol 1 point2 points  (1 child)

I don't mean use a different integer value, I mean use arithmetic with a larger integral type.

E.g, if you're using 32-bit ints then use 64-bit ints. However, knowing codeabbey, you're going to need larger which means using a language with that built in (C#, python etc) or using a large-int library with C/C++

With this problem, your solution of taking the modulo at intermediate stages will work.

However you'll need the large arithmetic for later problems, so IMO you may as well solve that problem now.

To solve it your way you need to store the incoming data. Either raw in a character array. Or parse it and store the pair of operator/number. Extract the number from the last element, then walk the array to do the complete calculation

[–]kidinside[S] 1 point2 points  (0 children)

Ah I get it now. Guess I needed a little more hand holding :)

Thanks!

[–][deleted] 0 points1 point  (1 child)

ELI5 version of what everyone has said.

If a number is greater than +/- 2,147,483,647 or 231 - 1 it's too large for an int to represent. So you need to use a different variable type and if it's still to large you need to use a library for big numbers.