Basic doubts in C++

I have a few silly general questions

  1. Many times for me, using int when constraints are up to 10^8 works and sometimes, int fails for 10^5 and 10^6. For one of the problems, even long int didn’t work for 10^6 and I had to use long long int to get AC. So could someone properly specify the boundaries of these data types? From a few sources, I understand int can be used for <10^9 and from a few other sources, I understand that int works only until 32768.

  2. Whenever we declare an array do its elements automatically get set to 0? Although it’s better to play safe by initializing all elements to 0, I think it can get time consuming for really large arrays.

I had more such doubts, but have forgotten them now. :stuck_out_tongue: I’ll add them when I remember.

Refer this and this.
Regarding initialisation, it depends how are you declaring the array. Ex: if you are using new , then it’s always initialised, but if you are using malloc, then it’s never. If you use calloc, then yes, it is!! If you are locally declaring(at both compile and run time), then it’s not initialised. You have to do that explicitly. If globally declared, then it is.

However, some newer versions may contradict on this. Like I remember, I mistakenly initiliased dynamically allocated array this manner int a[n+1]={0}; and this worked in C++14.

1 Like

I actually think that you face those problems because of the different test files in the different problems…moreover many time the problems is such like you use those int values from the array elements or so and your answer sums to the value greater than the value of int , which you might be facing for a value (in problem) 10^5 or less.

If you want to know more about the rnage of different data types you can follow this link.

And yes you are right with the default value of array…And it’s a good habit to be on the safe side.
HAPPY CODING

I typed all this before I saw the other answer so might as well post it I guess.

int can store values in the range from -2^31 to +2^31, including -2^31 but not including +2^31. 2^31 is more than 2*10^9 (exact value 2147483648). int and long int and long are all identical. long long int and long long can store from -2^63 to +2^63. 2^63 is around 9*10^18 (exact value 9223372036854775808).

Your problem is probably overflow during multiplication. Suppose you have int a = 10^5, b = 10^5. In this case is a*b 10^10, which is more than the limit for int, so you have to use long long. It doesn’t even work if you do long long t = a*b; This is because the value a*b is an int even if t is long long. a and b both need to be long long as well. Finally, t = 100000*10000; will fail as well because 100000 is an int, not a long long. If you want it to be a long long, you should type 100000LL. i.e. long long t = 100000LL*100000LL

Edit: Turbo C++ has the limit as 32768. You might be looking at an outdated source.

You can make sure an array is initialized by assigning {0}

e.g. int a[10000] = {0};

edit: codechef formatting is really annoying

1 Like

Oh yes! Precisely what I was facing! I never knew a and b had to be long long as well. Thank you! :slight_smile:

1 Like

If this is the problem you’re facing, then its not only multiplication. In many operations, like exponentiation also, overflow can occur. So, decide the data type according to the computation you have to do.

1 Like