Introduction: Basic Arithmetic in C++

Computer programming is a relatively new field of study. One of the most common and basic coding languages is called C++. This is used by big companies such as Google, Apple, and Microsoft to create applications. One example is the Google Doodle that appears on the Google homepage. This animation is often based on programming code in C++. One thing you may not realize is how simple it is to get started learning computer programming.

Throughout these instructions you may see an option below a step labeled "Advanced." This means that it is optional to read. You do not need to read this portion to complete the exercise.

In this set of instructions you will learn to apply basic arithmetic functions and to output the results using C++.

Step 1: This will list the materials you need.

Step 2: You will be directed to a website that will run your code.

Step 3: You will declare and initialize your variables (give numbers to variables x and y.)

Step 4: You will perform simple addition and the sum will appear in the output box.

Step 5: You will test other arithmetic functions.

Step 6: You will combine arithmetic functions.

Step 1: Materials

You will need a computer, phone, tablet, or any other device that can access a webpage and a keyboard. You will also need Internet access at the time you are following the instructions.


Advanced:

If you have more experience or ambition to further your studies in computer science, you may use your own compiler to carry out these instructions. For example, Visual Studio is my preferred compiler and Code Blocks is another popular compiler.

Step 2: Set Up

Go to the Code Chef website (https://www.codechef.com/ide) as it is an online program that will allow you to test your code from any device. Keep in mind, this website does not save your work!

The window in the middle contains the default set up code. This is also displayed in the figure. The meaning of this code is not necessary to complete the instructions, however if you are interested, they are described below.


Advanced:

#include
using namespace std;

These lines of code are necessary to import a library of information for the computer defined commands we will be using. Just like you would use a school library to locate and define unknown information, a compiler uses its libraries to figure out what each command you provide means. "iostream" in particular is the default library. It stands for "input/output stream." This tells the computer how to read in input from the user, and how to send output to the screen.

int main( ) {
//your code goes here
return 0;
}

This part of the code sets up the main function. A function is a set of code that you, as the programmer, would create to do something useful. The main function is where you put all of the code you want to be tested.

int - This stands for integer and tells the computer that you will be returning a whole number.

main ( ) - The empty parenthesis implies that the function does not need extra information to run (these would be called parameters.)

{ } - Curly braces are used to group a function's code. The open bracket indicates the start of a group of code, and the close bracket indicates the end.

return 0; - This tells the computer to output the integer 0, indicating that the main function had no issues. If something major went wrong with the code, it would output -1 instead. We will not encounter this issue.

// your code goes here - Any line beginning with two forward slash marks is called a "comment." These lines are not seen by the computer at all. They are meant only for the programmer to use in explaining the functionality of the code.

Step 3: Declare and Initialize Variables

First, you will create two integer variables. These variables can be named any single letter. I chose to use "x" and "y" as my variable names.

Next, you assign the variables a number. You can test several numbers at the end of this Instructable (in fact I encourage that!) but for now I recommend simple numbers. For example, I used "4" and "8."

The key is to make your screen look just like my image!! Don't forget the semicolons on the end!


Advanced:

The variable names can be any set of letters and/or numbers that are not recognized by the computer as keywords. Spaces are not permitted but underscores are often used to separate words instead. For example, "unit_cost" would be a good variable name.

Step 4: Output the Sum

The command used for printing something to the screen (output box) is cout << followed by what you want to print. First you will print the sum of your variables. This is displayed in the figure as cout << x + y;

Now, press the "Run" button at the bottom right of your CodeChef screen. This should compile and run your code, displaying the sum of your numbers in the output box below your coding box. You may need to scroll down to see it. Mine displayed the number 12.

Step 5: Other Arithmetic Operations

Try adding other arithmetic functions such as subtraction, multiplication, and division. Use the same cout command but with operations - , * , and / in place of the + .

I chose to do y / x because y holds the larger value and would therefore give a simpler answer than x / y.

When you are finished editing your code, press "Run" again to view the new output.


Advanced:

If you were to do x / y instead of how I wrote it above, you would find the output as 0. Although you would expect one half or 0.5 , the integer type only allows whole numbers. Therefore, when the answer is 0.5 , it will drop all of the decimal values and give you 0. The same is true for if you perform 25 / 2. You will get 12 instead of 12.5 .

Another interesting operation is % which returns the remainder in a division. So using the example above 25 % 2 would give you 1 because 25 divided by two is 12 with a remainder of 1.

Step 6: Combining Operations

C++ allows you to also combine arithmetic operations. The compiler follows the order of operations. If you do not know the order of operations, see this link: http://www.purplemath.com/modules/orderops.htm . In this example, you make another integer variable "z" and set it equal to "7." Next, you perform the operation x + y * z and obtain the result of "60." This shows that CodeChef performed the multiplication of "y" and "z" first and then the addition of "x."


Advanced:

You are welcome to include parenthesis as well. For example, ( x + y ) * z would output 84 because it performed the addition of "x" and "y" first.

Step 7: Conclusion

Now you know how to output solutions to arithmetic operations using integer variables. You are able to create as many integer variables and operations as you like.

If you would like to develop your C++ coding skills further, here is a helpful link:

http://www.penguinprogrammer.co.uk/c-beginners-tutorial/introduction/ - There are 9 pages of information explaining the basics of coding in C++. If you make it through all 9 pages, you will have covered many if not all of the topics covered in an average Intro to Computer Science semester-long course.