c++ - maximum 2 digits from a number -


In order to find the maximum number of values ​​from user input number, my code is in 'Attempt': #include & lt; Iostream & gt; using namespace std; Zero Max 2 (int a, int and max 1, int and max 2) {int temp = 0; Max 2 = a% 10; Max 1 = ((A / 10)% 10); If (max2> max 1) {temp = max1; Max1 = max2; For Max2 = Temporary;} (; a & gt; 0; a / = 10) {if (a% 10> max1) max1 = a% 10; And if (a% 10 & lt; max1 & amp; one% 10> Max 2) max2 = a% 10; }} Zero main () {int max1, max2, num; CIN & gt; & Gt; Number; Maximum2 (number, max1, max2); Cout & lt; & Lt; "Max 1 =" & lt; & Lt; Max1 & lt; & Lt; "Max2 =" & lt; & Lt; Max2 & lt; & Lt; Andal & lt; & Lt; Andal; }

It works perfectly for most numbers, e.g. Like 34256, except for some cases, for example, when I input 54321, the maximum is 5, which is correct, but it allows maximum 2 to 1, which is not the desired value. Will you be very kind to help me find out my error?

inside for the loop, you either < Value of the code> max1 or value of max2 . But to make your code work in all cases, there should be a point within for the loop, where you change the value of both to max1 and value of Max 2 .

After saying this, here's a suggestion:

  Zero Max 2 (int a, int and max 1, int and max2) {max 1 = 0; Maximum 2 = 0; (; A & gt; 0; a / = 10) {int temp = a% 10; If (Float> Max 1) {max2 = max1; Max 1 = Temporary; } And if (temp> max 2) {max2 = temp; }}}    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -