Answer the question
In order to leave comments, you need to log in
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;
Answer the question
In order to leave comments, you need to log in
... 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.
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;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question