-
Notifications
You must be signed in to change notification settings - Fork 0
Solve Me First
HackerRank starts things off at a very slow walk, nothing extremely difficult for this introductory problem.
Compute the sum of two integers.
Let us take a look at a custom solution using C++:
/* Custom solution for Problem 1.
* HackerRank Solutions Wiki
*
* Written by Jordan Sola
*/
#include <iostream>
int main()
{
int num_a, num_b;
std::cin >> num_a >> num_b;
std::cout << num_a + num_b << std::endl;
return 0;
}
Solution Breakdown
First off we have a multi-line comment c++ style, nothing too intense here. We import c++ standard input/output stream using #include <iostream>
, this is an include directive, it belongs to the c++ preprocessor. The preprocessor places commands in a source document before any code is executed. C++ standard input/output stream contains methods that can print information to the standard output stream and take input from the standard input stream, among many other things. The methods we use in this program that are included in <iostream>
are:
-
std::cout
: Prints information to the standard output stream. -
std::cin
: Takes information from the standard input stream. -
std::endl
: Prints a newline to the output stream, then flushes the stream buffer.
Next we declare our main function int main()
. Every c++ program needs a function that determines the main entry point for program execution. C++ is a strongly typed language, every variable, object, function etc. needs a return value, for a main()
function to execute properly, we need to give it a return type int
. We will see more types later. Scope is explicity defined in c++ by using {}
. The scope of our main()
function is where we will execute our instructions.
Now we have finally made it to the meat of our program:
int num_a, num_b;
std::cin >> num_a >> num_b;
std::cout << num_a + num_b << std::endl;
return 0;
Let us look at int num_a, num_b;
. An important point to make here is that most every statement in c++ needs a terminating character ;
. This tells c++ when to stop executing the current instruction and move on to the next available instruction. This instruction declares two variables of type int
, num_a
and num_b
. These variables are declared inline using a comma to let c++ know they are to be treated as seperate empty objects containing nothing of value in memory.
We can easily see the practicle usage of std::cin
std::cout
and std::endl
.
std::cin >> num_a >> num_b;
Tells our program to stop execution here and take user input into the values num_a
and num_b
.
std::cout << num_a + num_b << std::endl;
Tells our program to print the sum of num_a + num_b to the standard output stream, then print a newline and flush the stream buffer.
Finally return 0;
tells our program to stop executing and return the value of 0
to the system.
We're finished! Now that was a truly easy problem to solve, and c++ allowed us to write a concise, fast, and easily readable solution to give to the community. Best of luck trying this one out yourself, we'll see you on the next one!