Back to General discussions forum
On the problem page it says this:
All input values are between 0 and 1,000,000,000 - be sure to take care of possible overflow in progress of calculations!
are there any webpages anyone could recommend me read on how to prevent overflow. i have a feeling that my calculation is having this issue. because it solves the practice question easily but fails the main calculation. i'm just not sure what to look for.
I suppose the largest number that will fit in int is around 10 ^ 500000.
After this my python said memory overflow.
I just wanted to give you a tip that your question is not quite correct.
There are a lot of different ints, so you must be more specific.
Do you at least know that C++ size of int is different for 32-bit and 64-bit systems?
By the way, leeUNAGI, I looked into your solution and want to share some insights on coding style:
int
. This is a bad habit because the program will compile differently on different platforms. On 16-bit platforms it will compile with 16-bit ints, on 32-bit platforms with 32-bit ints, and on 64-bit platforms with 64-bit ints. And I believe that 128-bit platforms are on the way. So be explicit:signed short int
or unsigned short int
signed long int
or unsigned long int
signed long long
or unsigned long long
const
keyword. For example, limit
and seed
variables in your program are actually constants.a
or index
variables? Yeah, they are never used anymore. So remove unnecessary declarations.For example there are first ten lines of your program nicely coded:
#include <iostream>
#include <fstream>
using namespace std;
ifstream fileIN1("array_checksum.data");
ofstream fileOUT1("array_checksum.sum.out");
int main() {
const signed long int seed = 113;
const signed long int limit = 10000007;
signed long int ct, result = 0;
signed long int arr[1000];
...
qwerty
The size of a short, int, long and long long are determined by the C++ compiler.
Running this C++ code, on a codeabbey problem page
#include<iostream>
using namespace std;
int main(){
signed long int a;
signed long long b;
cout << sizeof(a) << " " << sizeof(b) << endl;
}
gives 8 8, showing that a signed long int and a signed long long are the same size!
But perhaps if I declare variable as signed long int
, it will be -at least- 32-bit size? That's what I meant...
wow thanks so much for all the replies.. i'll catch up tomorrow and go over all the tips and get back with everyone. My GF's family dog passed earlier this morning and its been a rough day so i'll pick it back up tomorrow and reply with where i'm at with the problem.
thanks again. be back on tommorrow
I'm sorry to hear that.
I wish I could say some words to support you, but I'm not good enough at english.
oh no worries. Thats nice that you even said that.. thank you.
ok so I am in Visual Studio 2019
So i did a cout << sizeof(result) << endl; and on my laptop i'm on right now it said 4.
so then i did this:
signed short int x = 0; signed long int y = 0; signed long long z = 0;
then i did:
cout << sizeof(x) << endl; cout << sizeof(y) << endl; cout << sizeof(z) << endl;
told me:
2 4 8
So i took your advice not using just int and used signed int short, long int or long long for my result and other data and tada no overflow it seems :)
also the reason for this: "Be consistent on your coding style. For example, you surround operators with spaces at some places, but you do not surround them in other places, can you explain why?"
i didnt realize until recently that i need to keep the style the same all the time. I will be more consistent. Thank you for bringing that to my attention.
also ive been reading linux kernel coding style and taking notes on what style I should mimic ;)
i'm assuming Mr. Linus would be a good example to follow... and K&R of course