Array checksum question

Back to General discussions forum

leeUNAGI     2021-08-04 03:46:54

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.

Quandray     2021-08-04 06:22:36
User avatar

What is the largest number that will fit in an int?

qwerty     2021-08-04 15:57:37

I suppose the largest number that will fit in int is around 10 ^ 500000.

After this my python said memory overflow.

Quandray     2021-08-04 16:23:10
User avatar

My question was directed at leeUNAGI, who is using C++.

qwerty     2021-08-04 16:59:37

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?

qwerty     2021-08-04 20:49:03

By the way, leeUNAGI, I looked into your solution and want to share some insights on coding style:

  1. 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?
  2. Never declare integer variables with bare 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:
    -declare 16-bit ints as signed short int or unsigned short int
    -declare 32-bit ints as signed long int or unsigned long int
    -declare 64-bit ints as signed long long or unsigned long long
  3. If you see that the value of some variable will never change, then this is not a variable, but a constant. Declare constants with a const keyword. For example, limit and seed variables in your program are actually constants.
  4. After you finished program, look carefully if there are any unused declarations left. For example, do you see where you use the 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];
    ...
Quandray     2021-08-05 06:09:39
User avatar

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!

qwerty     2021-08-05 07:00:13

But perhaps if I declare variable as signed long int, it will be -at least- 32-bit size? That's what I meant...

leeUNAGI     2021-08-06 04:56:50

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

qwerty     2021-08-06 07:17:01

I'm sorry to hear that.

I wish I could say some words to support you, but I'm not good enough at english.

leeUNAGI     2021-08-09 05:02:57

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.

leeUNAGI     2021-08-10 21:17:50

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

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