Let's write a program that solves Addition. Compared to some of the other problems on this training site, the solution isn't too complicated, so this will be a good opportunity to get some practice writing programs that handle I/O (input/output) correctly. Many of the basic techniques used to solve this problem are universal to all informatics problems, so the sooner we get familiar with them, the better.
Before writing even a single line of code, we should make sure we know exactly what the program needs to do. (After all, if we don't plan ahead, there's a good chance we'll end up making mistakes or wasting time and effort.) Of course, all programs are different and there is no definitive solution, but all proper solutions to Addition will need to do the following things:
Open up a new file and write a few lines of code in the language of your choice. The program doesn't actually have to do anything right now: we're going to use it as a framework and insert all the really important code around it.
Below are a few examples in various languages supported by the training site. If your language of choice isn't there, don't panic - the instructions translate easily to most programming languages you'll encounter.
# Declare variables
# Read the input
# Calculate the answer
# Write the output
#include <cstdio>
/*Declare variables*/
int main() {
/*Read the input*/
/*Calculate the answer*/
/*Write the output*/
return 0;
}
As you can see, there are a lot of comments in the sample code. Notice how they roughly correspond to the dot points in the previous section? Since we went to the trouble of planning out the program beforehand, this extra measure makes sure that we stick to the plan. (Sure, little reminders like 'declare variables' or 'calculate the answer' aren't going to make or break a solution, but they make the code more readable and easier to understand.)
Our goal is to read the two integers from the problem statement, a and b, which we will eventually add together. But before we can do this, we need somewhere to store these values during the calculation. Let's declare a couple of variables.
Let's backtrack and insert the following code at the bottom of the "Variables" section:
# Python doesn't need you to declare variables like most other languages do, but we'll do it anyway.
#
# The below code declares three different variables to be used
# later on in the code. The first two, "a" and "b", will be used to store the values
# from the input.
#
# As for the third? That's for later. We'll use "total" to store the value of "a+b".
#
# We'll set them to "0" now to remind us these variables are supposed to store integers.
# We could also have set them to "None", which is a special value that means "this has no value" (at the moment).
a = 0
b = 0
total = 0
/*
The below code declares three different integer-type variables to be used
later on in the code. The first two, "a" and "b", will be used to store the values
from the input.
As for the third? That's for later. We'll use "total" to store the value of "a+b".
*/
int a, b, total;
You may not agree with the variable names in the example - there is no one correct way to name variables, so give them whatever names you want. a, b, total
is just as legitimate as firstNumber, secondNumber, answer
so long as the names of the variables remind you exactly what they're for.
Now that we have our variables ready, we can now read the input! Insert the following code at the bottom of the "Input" section:
# This line tells the computer several things:
# - "input()" tells it to read an entire line of input.
# - ".split()" tells it to take that line and split it into parts, using
# spaces as separators.
# - "a, b = map(int, ...)" tells it to take each part of the line, turn it
# into an integer, and store them in "a" and "b" (in that order)
a, b = map(int, input().split())
/*
This line tells the computer several things:
- "scanf(...)" tells it to read input.
- "%d%d" tells the computer to expect two integers.
- The rest of the line ("&a, &b") tells it to store these integers
in "a" and "b" (in that order).
*/
scanf("%d%d", &a, &b);
By now our code should be beginning to resemble a full program. It might look a bit like this:
# Declare variables
a = 0
b = 0
total = 0
# Read the input
a, b = map(int, input().split())
# Calculate the answer
# Write the output
#include <cstdio>
/*Declare variables*/
int a, b, total;
int main() {
/*Read the input*/
scanf("%d%d", &a, &b);
/*Calculate the answer*/
/*Write the output*/
return 0;
}
Before we go on you might want to give the code a quick test run. Compile it (if applicable - Python doesn't need compiling) and run it. If it compiles/runs properly and asks you for input, then we're doing good.
As it turns out, the code to add the two numbers together is probably the simplest part of the program! Adding numbers is a fundamental computer operation, and most programming languages can tell the computer to add two numbers without any hassles. If you're still following the skeleton code structure, you'll want to put this in the "Calculation" section.
total = a + b
total = a + b;
It does exactly what it looks like - the value of total
is set to the sum of the two other variables. Since these variables have just been inputted, the resulting number is the answer we're after!
Finally, we need to write the value of total
.
This is called printing, and can be done using the next line of code.
# This line tells the computer to write out the sum we calculated earlier.
print(total)
/*
The syntax is similar to scanf from before. "%d" tells the computer that
we are printing an integer; "\n" adds a linebreak.
*/
printf("%d\n", total);
And with that, we've finished coding our solution!
The code below is (to the best of my knowledge) a working solution for Addition, and should score you 100% marks. A lot of the input and output related techniques used in the program will work for most other problems you find on the training site. Feel free to use this as a starting point for future problems!
# Declare variables
a = 0
b = 0
total = 0
# Read the input
a, b = map(int, input().split())
# Calculate the answer
total = a + b
# Write the output
print(total)
#include <cstdio>
/*Declare variables*/
int a, b, total;
int main() {
/*Read the input*/
scanf("%d%d", &a, &b);
/*Calculate the answer*/
total = a + b;
/*Write the output*/
printf("%d\n", total);
return 0;
}