C++ Arithmetic operations

(UPDATED 22.6.2013)

Some basic arithmetic operations. The code that is written in the compiler is in bold. The output in the console is after the “OUTPUT:” word. Words after the comment syntax (“//”) on the same line is ignored by the compiler. Remember the real life mathematical rules.
In my next post I will perhaps have some string concatenations and some string handling.
In the end of this post I noticed that it became too long so I cut the upper part with the brackets, system(“PAUSE”)  and return 0. After the first post you just have to add the codes in order for it to work.

I also must emphasize something that I forgot to mention earlier but noticed as I mirrored from the materials from other source’s that although you can do every kind of mathematical operation with variable type strings (and I will show you how) and process strings in many different way’s, you cannot do arithmetic with strings as such for example
when trying to do arithmetic with string, it would just concatenate or give an error message depending on the operation. Please look up for programming language syntax and semantics if you want more information regarding to this.

I also forgot to mention that the compiler will execute the code in linear manner depending on the operation, but mainly in linear. The compiler might reference to a variable that is written before the executed line. We also might have method calls (I know that you might have no idea what this means but just try to keep the logic in mind) where the program might send to a method (that might be anywhere in the .cpp file or in another class) where the value/information is being sent back and forth to achieve something by processing it in another place where the method is reusable again and again without the user having to write the same code all over again. I will get into methods in the following tutorials.

Anyway, my point was that keep in mind that the program, when compiled will execute (Although it is compiled as a whole) one line at a time, where it will not know what will happen later in the .cpp file. For example if you were to write a variable before you try to create a program to get input from the user, the compiler will throw an error message because you need to have the variable either set up and initialized before you use it in the program line even if you would have it below the line. You can however have it initialized on the same line where you want to use it.

Before you dive in, I thought to update this section with more theory. In C++ and many other programming languages when we do mathematical/arithmetic operations/calculations (expressions), we can do it by remembering simple rules that we learned from elementary school, but there are few exceptions. In programming languages we have to be very detailed on what we want to do with values and variables for example

int a = 15;

int calc = 16a + 2;

Will give an error because not all the rules of the school can be applied in C++. You must have thought that you could multiply the value 16 with the value that is stored in a by just doing what you always did in your mathematics class, but unfortunately that will not work here. C++ will probably see the above example as an variable because of the a next to 16 but this is not syntactically valid if you also remember the naming conventions and what are the valid ways to name your variables.You must be specific with your arithmetic calculations thus the above example must be written in the following way

int a = 16 * a + 2;

I will later add a better theory and description of the program logic.

—————————————

#include <iostream>
using namespace std;
int main()
{
int i = 2;
int b = 3;
int operation;

operation = ((i + (b++)) – 1) * 5;

cout << operation << endl;

system(“PAUSE”);
return 0;
}

OUTPUT: 20

—————————————

#include <iostream>
using namespace std;
int main()
{
int i = 2;
i++;
cout << i << endl;
i++;
cout << i << endl;
i–;
cout << i << endl;
i = 1 + 1;
cout << i << endl;
i = 2 – 1;
cout << i << endl;
i++;
cout << i << endl;
i = i + 10;
cout << i << endl;

system(“PAUSE”);
return 0;
}

OUTPUT:
3
4
3
2
1
2
12

—————————————

#include <iostream>
#include <cmath>

using namespace std;
int main()
{
double a = 1.2; // Variable that can take values with decimals
double b = 2.3;
double sum = a + b;
cout << sum << endl;
system(“PAUSE”);
return 0;
}

OUTPUT: 3.5

—————————————

double a = 1.2;
double b = 2.3;
double sum = a – b;
cout << sum << endl;

OUTPUT: -1.1

—————————————

double a = 1.2;
double b = 2.3;
double sum = ((a / b) + 2 )*4;
cout << sum << endl;

OUTPUT: 10.087

—————————————

int a = 10;
double operation;
operation = a * 2;
cout << operation << endl;

OUTPUT: 20

—————————————

double a = 10; // power function needs a double variables

cout << pow(a,2) << endl; // 10 to the power of 2

OUTPUT: 100

—————————————
// In the below example we are using the modulo operator

int a = 10;
int b = 2;
int operation = a % b; // remainder will be insterted in variable

cout << operation << endl;

OUTPUT: 0

—————————————

int a = 11;
int b = 2;
int operation = a % b;

cout << operation << endl;

OUTPUT: 1

—————————————

int a = 11;
int b = 2;
double operation = a / b;

cout << operation << endl;

OUTPUT: 5

—————————————

double a = 11;
double b = 2;
double operation = a / b; // remember that int variables only take whole numbers

cout << “The output of this operation of variable << a << “\n”;
cout << “and ” << b << ” is \n”; // where backslash n is newline
cout << operation<< endl;

OUTPUT:
The output of this operation of variable 11
and 2 is
5.5

—————————————

int a = 1;
int line = 1;
cout << a << ” on line ” << line << endl;
a = 2;
line++;
cout << a << ” on line ” << line << endl;
a++;
line++;
cout << a << ” on line ” << line << endl;
a–;
line++;
cout << a << ” on line ” << line << endl;
a += 2;
line++;
cout << a << ” on line ” << line << endl;

OUTPUT:
1 on line 1
2 on line 2
3 on line 3
2 on line 4
4 on line 5

—————————————

double a = 10;
double b; // creating variable b
b = a; // b equals a meaning b = 10
b *= b; // b = b * b
cout << b << endl;

OUTPUT: 100

—————————————
double a = 10;
cout << log(a) << endl; // Logarithm

OUTPUT: 2.30259
—————————————
float pi = 3.14159; // Do not trust my pi and remember the dot instead decimal
float radius = 10;
float area = pi * (pow(radius, 2));
cout << area << endl;

OUTPUT: 314.159
—————————————
float diameter = 12;
float radius = diameter / 2;
cout << radius << endl;
OUTPUT: 6
—————————————
float a = 100;
cout << sin(a) << endl;
OUTPUT: -0.506366

—————————————

int a, b, c, d, sum;
a = 1; b = 1; c = 1;
d = 1;
sum = a+b+c+d;

cout << sum << endl;

OUTPUT: 4

—————————————

cout << sqrt(10) << endl;
cout << cos(10) << endl; // add a before cos for inverse
cout << sin(2) << endl; // same as above
cout << tan(5) << endl; // ..-..

OUTPUT:
3.16228
-0.839072
0.909297
-3.8052

—————————————

In all of the examples you could have also made them negatives by just adding – before the value or variable like int a = -s; or int a = -14;

You should also remember for future material searches that in C++ the increments and decrements are called unary operations.

You also must have noted that if you wanted the C++ to do some operations first or aside from other operations, you should put them inside the brackets.

I also failed to remember completely that you can add ++ and — operators before the value/variable that will output the value before incrementing it for example if you wanted to print the value the same time you increment or decrement it. This operations is also useful in your future studies where you have to create / come up programs that can do fibonacci numbers, hanoi towers, poker cards etc.

I also want to emphasize on the more unused operators for example +=, -=, *=, %=, /=
Below is the example on the meaning of these operators. Suppose for example we have an arithmetic operation of

int a = 10;
a = a + 1;

The above can be written as

int a = 10;
a += 1;

I will add more operations later, but I will publish this now so I there is something and as I add more operations, you still would have the old ones.

References:

http://www.cl.cam.ac.uk/teaching/0809/Semantics/notes-mono.pdf (Random Google find for semantics)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s