Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite+ 1
C++ min and max
I have wrote a code in C++ inserting 10 values and printing the min and max. But when I insert a number with many digits (for example 1000000000000), it won't continue and displays a random number as both min and max. Why is that happening???
17 Réponses
+ 2
Intergers have limits you need a different data type if you have enough digits.
https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
+ 5
Without code, might be an overflow if it always displays the same random number with the same large insert.
Got a CodePlayground link?
+ 4
@Manual...I've been sitting with this in my clipboard because I'm having some trouble with both of yours's code:
==============
Integers are 4 bytes on this platform. 4 bytes = 32 bits = 2^32 = 4294967296.
You're using signed integers, so divide that in half:
2,147,483,648..... trails off to try code....
==================
When I enter 10000000000, as in the question statement (and regardless of any other values, even incomplete input), the min/max ceilings to integer (not long) max and all the other cins appear to be nonfunctional.
Just FYI.
+ 4
Yep, that's what I was fiddling with:
int i; long l; unsigned long long ull;
cout << sizeof those
4, 4, 8
Anyway, learned something .... longs and ints not different here.
Thanks @Manual.
+ 3
Christos
10000000000 is too large to be an interger
use a template or decimal instead of int.
+ 3
@Kirk
You're welcome!
+ 2
+ 2
I adjusted the range
try this
https://code.sololearn.com/cp2qYDcwUFmO/?ref=app
+ 2
@Christos
Please do not forget to give the checkmark, to the one who helped you.
+ 2
Try the update long long
- bigger range
+ 1
share your code
[ +insert ]
-add code
|
|
\/
+ 1
#include <iostream>
using namespace std;
int main()
{
int a,min,max;
cin >> a;
max=a; min=a;
for (int x=2; x<=10; x++) {
cin >> a;
if (a<min) {
min=a;
}
if (a>max) {
max=a;
}
}
cout << "The minimum is " << min << endl << "The maximum is " << max;
}
+ 1
If you run it on Playground it shows the same result...
+ 1
Manual
Thanks for the tip, but in math this is an interger too. In coding is different, right??
+ 1
Manual
Thanks man I didn't know there was a limit in each data type... :-)
+ 1
You're welcome!😸
+ 1
did you try the new version?
edit
I tried it
seems like the limit was reached...