Two Cases To Consider
I have been in the tech culture for quiet some time and I remembered when I was still starting out, I was confused on how I would consume all the information regarding the tech world. I came from a non cs background so it’s expected for me to have a hard time at first. I noticed that there are two cases on how to approach it.
Either I:
- (1) Focus on retaining the syntax and hope that the concept would just come and go through time.
- (2) Focus on understanding the concept and less on retaining the syntax so I could have a better understanding of how things work.
I will turn this into a series so part 2 will be in another blog.
What I learned on retaining the syntax
- (1) You will rely too much on your instinct/intuition
If you keep on practicing the syntax and you have less understanding of how things work, you will rely too much on pattern recognition. To give an example, consider this problem that you have practiced many times:
Given two integers a
and b
. Write a C++ code that prints the sum
of two integers and multiply
the sum
by 2 if even
and 3 if odd
.
#include <iostream>
using namespace std; int main() { int a, b; cout << "Enter two integers: "; cin >> a >> b; int sum = a + b; if (sum % 2 == 0) { sum *= 2; } else { sum *= 3; } cout << "The result is: " << sum << endl; return 0;
}
Now. Given a problem that you have practiced multiple times similar to this one:
Given two integers b
, c
, and a decimal a
. Write a C++ code that prints the sum
of two integers b
and c
and if the sum
is even
, multiply by a decimal number a
that is approximately close to the left hand side of the integer b
. If the sum
is odd
, multiply by a decimal a
that is approximately close to the right hand side of integer b
.
Constraint:
Left/Right hand side of b should have a decimal point of upto 5 only. Sample: 5.99999, 56.99999….
Sample:
b = 2
c = 2
b + c = 2 + 2 = 4
Since even, left hand side that is approximately close to b is 1.99999 so:
(b + c) * a = (2 + 2) * 1.99999
Sample:
b = 3
c = 2
b + c = 3 + 2 = 5
Since odd, right hand side that is approximately close to b is 3.00001 so:
(b + c) * a = (3 + 2) * 3.00001
#include <iostream>
#include <iomanip> using namespace std; int main() { int b, c; double a; cout << "Enter integer b: "; cin >> b; cout << "Enter integer c: "; cin >> c; int sum = b + c; if (sum % 2 == 0) { a = (b - 1) + 0.99999; } else { a = b + 0.00001; } cout << fixed << setprecision(5); cout << "The result is: " << sum * a << endl; return 0;
}
What I am trying to point out here is the thought process. You have practiced the first problem multiple times that you are seeing patterns on how you could crack the second one.