C++ min and max | Sololearn: Learn to code for FREE!
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???

13th Aug 2017, 10:43 PM
Christos Provopoulos
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
13th Aug 2017, 11:13 PM
Manual
Manual - avatar
+ 5
Without code, might be an overflow if it always displays the same random number with the same large insert. Got a CodePlayground link?
13th Aug 2017, 10:47 PM
Kirk Schafer
Kirk Schafer - avatar
+ 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.
13th Aug 2017, 11:26 PM
Kirk Schafer
Kirk Schafer - avatar
+ 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.
13th Aug 2017, 11:35 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
Christos 10000000000 is too large to be an interger use a template or decimal instead of int.
13th Aug 2017, 11:06 PM
Manual
Manual - avatar
+ 3
@Kirk You're welcome!
13th Aug 2017, 11:36 PM
Manual
Manual - avatar
13th Aug 2017, 11:04 PM
Manual
Manual - avatar
+ 2
13th Aug 2017, 11:11 PM
Manual
Manual - avatar
+ 2
@Christos Please do not forget to give the checkmark, to the one who helped you.
13th Aug 2017, 11:14 PM
Manual
Manual - avatar
+ 2
Try the update long long - bigger range
13th Aug 2017, 11:32 PM
Manual
Manual - avatar
+ 1
share your code [ +insert ] -add code | | \/
13th Aug 2017, 11:02 PM
Manual
Manual - avatar
+ 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; }
13th Aug 2017, 11:02 PM
Christos Provopoulos
+ 1
If you run it on Playground it shows the same result...
13th Aug 2017, 11:07 PM
Christos Provopoulos
+ 1
Manual Thanks for the tip, but in math this is an interger too. In coding is different, right??
13th Aug 2017, 11:10 PM
Christos Provopoulos
+ 1
Manual Thanks man I didn't know there was a limit in each data type... :-)
13th Aug 2017, 11:15 PM
Christos Provopoulos
+ 1
You're welcome!😸
13th Aug 2017, 11:16 PM
Manual
Manual - avatar
+ 1
did you try the new version? edit I tried it seems like the limit was reached...
13th Aug 2017, 11:28 PM
Manual
Manual - avatar