D
D
Doaxan2015-10-06 18:40:09
Mathematics
Doaxan, 2015-10-06 18:40:09

How to quickly understand similar code?

There is a code that sorts 3 numbers in ascending order.

int main()
{
    int A,B,C;
    while(true)
    {
        cout<<"Enter A : ";cin>>A;
        cout<<"Enter B : ";cin>>B;
        cout<<"Enter C : ";cin>>C;
        if(C < A)
        {
            C = A + C;
            A = C - A;
            C = C - A;
        }
        if(B < A)
        {
            B = A + B;
            A = B - A;
            B = B - A;
        }
        if(C < B)
        {
            C = B + C;
            B = C - B;
            C = C - B;
        }
        cout<<"Sorted numbers :\n"
            <<A<<" "<<B<<" "<<C
            <<endl;
    }
    return 0;

It doesn't immediately become obvious to me how such code works. After a few minutes, I figured it out, but the time spent on parsing simple code is frustrating. Therefore, the question is: what should be read / studied in order to "click" similar code in a couple of seconds? There were no problems with mathematics (linear and discrete), I always liked to solve mathematical problems, just maybe there is not enough knowledge? Or has the brain become lazy due to rare calculations?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
Zelimkhan Beltoev, 2015-10-06
@Doaxan

... just maybe there is not enough knowledge?
To be more precise - not enough practice. For this example, a lot of knowledge, as it were, is not needed, a basic knowledge of syntax is enough.
Program more often and such code will be easy to perceive.

T
Tlito, 2015-10-06
@tlito

it's not just you. for normal programmers, there is a rule
that a program without comments and documentation is a trifle that no one needs.
in this example, the programmer took care not to organize an extra variable and swaps the variables by shuffling the data inside them. this is of course his choice, but that's why the program becomes incomprehensible.
there are no comments in this code, and the algorithm, in my opinion, is not the best and not the most understandable.
if you want this code could be implemented like this:

#include <iostream>
using namespace std;

int main()
{
    int A,B,C;
    cout << "Программа сортировка по возрастанию\n";
    cout << "Введите три целых числа через пробел:\n";
    cin >> A >> B >> C;
    //opredelyaem poryadok sortirovki
    if (A > B) {
        if (B > C) {
          // A > B > C
          //poryadok ostaetsa pryamym
          //nichego ne delat
        } else {
            if (A > C) {
                // A > C > B
                //menyaem B i C
                B += C;
                C = B - C;
                B -= C;
            } else {
                // C > A > B
                //menyaem A i C, potom B i C
                A += C;
                C = A - C;
                A -= C;

                B += C;
                C = B - C;
                B -= C;
            }
        }
    } else {
        //esli A < B
        if (A > C) {
            // B > A > C
            //menyaem tolko A i B
            B += A;
            A = B - A;
            B -= A;
        } else {
            if (B > C) {
                // B > C > A
                //menyaem C i A, i potom A i B
                C += A;
                A = C - A;
                C -= A;

                B += A;
                A = B - A;
                B -= A;
            } else {
                // C > B > A
                // menyaem A i C
                C += A;
                A = C - A;
                C -= A;
            }
        }
    }
    cout << "По возрастанию: \n";
    cout << C << " " << B << " " << A;
    return 0;
}

I admit, the code in the question is better than in this answer. but here are some comments

A
abcd0x00, 2015-10-07
@abcd0x00

Draw flowcharts, first on paper (mandatory), then in your head.
So you can easily select just one piece

//
        if(C < A)
        {
            C = A + C;
            A = C - A;
            C = C - A;
        }

and you will understand that it is repeated everywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question