Topic_04 : Simple operators and variables


The main reason for inventing computer is to make use of it in high computations.  In this tutorial, we are going to see, how we can ask the computer to make calculations for us.

PRE-REQUIREMENTS:
I expect that, you know simple basics like difference between integers and rational numbers.  I assume that, you are familiar with mathematical statements like "Let 'x' be an integer" or "Let the value of 'a' is 2.5".


The program:
____________________________________
void main( )
{
    int x;
    x = 25 * 35;
 
}
_____________________________________

Program explanation...

In the program, you can see, a line like "int x;"
It says to the computer "Let x be an integer".

In the next line, you can see a line " x = 25 * 35;"
It is same as it's mathematical meaning.  Which makes the computer to assume that x has the value, equal to the product of 35 and 25.  Please note that, in the program, we represent multiplication using the "Asterik" symbol (*).
In this single line, we are asking the computer to do 2 things..
1) calculate the product of 35 and 25
2) assume the value of x to be the result above

Watching the work in live..
1)Start debugging by pressing "F10".  As soon as the yellow arrow (shown in the figure) crosses the start of curly brace of main function, the program execution starts.  Press "F10" to cross the line "int x;".  If you keep the mouse near "x", you can watch a popup, that shows the value of x(as shown in the figure)
Dbg_05_VariableValue

We can also watch the value of x by
1) selecting "x" and right clicking it
2) choose "Add watch" (as shown in the figure)
Dbg_06_AddWatch

the action above makes the "Watch window" to appear.  You can see the details of the elements you added to the watch.  Since we didn't say anything about the value of x to the computer, computer assumes some randomly possible value for x.  In the watch window, we can see that random value
Dbg_07_Watch

Now, if you proceed in the debugging process, you can notice that the value of x in the watch window, changes as soon as the yellow arrow crosses the line "x = 25 * 35"
Dbg_08_VarWatchChanged
__________________________________________

There is a little difference between computers and maths in this context.  In maths, if we assume that, the value of x is something, say 25, we normally don't assume other value for 'x' (unless you are working with trial and error/ induction  method etc to solve something)

But it is very common in computers that, we assume different values for 'x' at different parts of the program.  In other words, x can have varying values, or x is a variable.

Explanation on variables:

Are you wondering, why should we change the value of 'x', unnecessarily???  Consider this example

Suppose that, we are intrested in finding two values
1) product of 235 and 456
2) difference of 345 and 879.
Then there is no need for us to take 2 things like 'x' and 'y' for each of the above things.

We can simply write a program like

void main( )
{
     int x;
     x = 235 * 456;
     x = 345 - 879;
}

If x was added to the watch, we can notice the varying values of the variable 'x', while debugging. 


I used simple calculations like substraction and multiplication. 

We can also ask the computer to perform other calculations like addition, reminder and division.

We can use '+' symbol for addition.

There is one more operator, called mod operator, denoted by %.  It gives the remainder of a division.
When we write code like "x = 23 % 5;", it changes x to 3, which is nothing but the reminder we get, after dividing 23 with 5;


Division needs some special treatment here..

Suppose we write code like "x = 23/5;"
we expect the value of x to be 4.6; but in our program, we said to computer to treat x as an integer.  So, computer simply ignores the fractional part of the result.
so, if we write x = 23/5, it changes x to '4'.

To get even the fractional part, we should not use integers, but real numbers.

this can be done by the following program
_______________________________________
void main( )
{
    double x;
    x = 23.0/5;
}

explanation:
The program above may not be a surprise to you, except the word, "double".  Recollect that computer can understand only ones and zeros.  Computer people invented some notation to represent real numbers, using ones and zeros.
double is one of such representations.  You'll come across such representations, in the later parts of the tutorials.

For time being, you just take it for granted that, double means "Real number".

Please note that, 23 is used as 23.0.  If we simply use 23, then the computer ignores the fractional part.  In order to force the computer to consider the fractional part also, we specify 23.0 instead of 23.
________________________________________


It is also possible to make complex calculations using brackets.  Suppose that we are calculating simple intrest related things.
Recollect that A = P(1+TR/100).

This can be done by the following program

void main( )
{
    double P = 10000;
    double T = 3.5;
    double R = 20/100;
    double A;

     A = P*( 1 + T*R/100 );

}

by writing double T = 3.5, we say to the computer to assume that 'T' is a double(real number), and let the value of 'T' be 3.5.
____________________________________

We can also make some scientific calculations like...

# include <math.h>

void main( )
{
    double PI = 22.0/7.0;
    double x = sin( PI/2 );
    double y = sin( PI/3 );
}

Recollect from the previous tutorial what is the meaning of the first line "# include ...".  And also note that, the value used with sin function is in radians, but not in degrees.

    .