(C++) Arithmetic expressions


C++ Assignment operator

We already know that you can set the value of a variable using the input staememt.
The input statement is used in cases where the value is set by the user during program execution.

But very often we need to set a new value for a variable, calculating it according to a certain formula. In this case, the assignment operator will help us

The general form of the assignment operator is as follows:
<variable> = <expression>;

The assignment operator works as follows:
1. First, the expression to the right of the assignment sign is calculated.
2. The received value of the expression is saved (they say "assigned") in the variable standing to the left of the assignment sign. In this case, the old value of the variable is erased.

For example, if we need to set the variable c to the double value of the variable b, then we will have to write it like this:
с = 2 * b;

Remember that in programming you cannot omit the multiplication signs in an expression. Otherwise, the computer does not understand what you want to multiply.
For example, you can’t just write c = 2b, it will be wrong!

Arithmetic operators and expressions

The expression on the right side of the assignment operator allows you to calculate values using various formulas.

What expression can contain
1) integer and real numbers (in real numbers, the integer and fractional parts are separated by a dot, not a comma, as is customary in mathematics)
2) signs of arithmetic operators:
+ addition
- subtraction
* multiplication
/ division
% remainder of the division

3) standard mathemtical functions (we can use the math module math.h write at the top of the program #include<math.h>)
 abs(i) - |i|  - absolute value of an integer i 
 fabs(x) - |x|  - absolute value of a real числа x
 sqrt(x) -  \(\sqrt x\)
 pow(x,y) - xy
 
4) parentheses to reorder

Built-in functions

Any programming language includes many built-in functions that can be used in arithmetic expressions. To use additional functions, you often have to include maths library: 
#include<cmath> 

For example, the most commonly used standard mathematical functions and their writing in C and C ++
 abs(i) - |i|  - absolute value of an integer i 
 fabs(x) - |x|  - absolute value of a real числа x
 sqrt(x) -  \(\sqrt x\)
 pow(x,y) - xy
 
Remember!
The function argument is always written in brackets.
For these functions to work, you need to include maths library:  #include<cmath> 

Rules for coding arithmetic expressions


Suppose we need to calculate the value of the expression written in mathematical form:
\({ 2\ \cdot\ 17,56^2 \over {7\ \cdot\ 2,47\ \cdot\ 0,43}}\)
 
Before writing a program that will calculate the result for us, we formulate the RULES for writing algebraic expressions in a programming language:
1. Expressions contain numbers, names of other variables, operators, parentheses, function names
2. Arithmetic operators (+, -, *, /, %)
3. The separator of the integer and fractional parts is the point.
4. The expression is written one per line (linear recording of expressions), the characters are sequentially arranged one after another,  for EVERYTHING operation signs are affixed; parentheses are used.


Thus, following the rules for coding arithmetic expressions, we must translate this (mathematical notation) fraction into a line.
Because the numerator and denominator are complex (that is, they contain two or more operators), then when writing to a linear form, it is necessary to take the expressions in the numerator and denominator in brackets.
Thus, a linear record of such an expression will look like this:

(2*17.56*17.56)/(7*2.47*0.43)

We will write a program to calculate the value of the expression: for this we decide on the input and output data

Input  
Because we know all the values, then you don’t need to input anything from the keyboard, therefore there will be no input data.

Output 
The program must output the value of the arithmetic expression (you can put it into any variable, or you can immediately output the value).


We will immediately output the value of the expression without saving it in any variable.
Because if we have a fraction, then the result will be a real number
#include<iostream>
using namespace std;

int main()
{ 
  cout << (2*17.56*17.56) / (7*2.47*0.43);
  return 0;
}
Run the program on the computer and make sure that it produces a result equal to 82,949843.

After that, complete the task.

Peculiarities of the division operation in the C and the C++


In the C ++ programming language, there are two division operations:
/ division, and % calculation of the remainder of the division.
 
Remember!
1) The operation of calculating the remainder of division (%) is performed ONLY on integers!
2) The result of the division operation (/) depends on the type of operands.

The rule here is as follows
When dividing an integer by an integer, the fractional part is always discarded, regardless of what type of variable we store the value!
When saving the calculating result into an integer variable, the fractional part will also be discarded.


Let's analyze examples of performing division operations:
#include<iostream>
using namespace std;
int main()
{
  int i, n;
  double x;
  i = 7;
  x = i / 4; // x=1, divides the integer into the integer
  x = i / 4.; // x=1.75, divides the integer into the real 
              // (4 - without a dot is perceived as an integer, 
              // 4. (with a dot) - it's a real number!)
  x =(double) i / 4; // x=1.75, divides the real into the integer  
                    // - here the variable i is converted to a real number 
                    // - this is an explicit conversion of type
  n = 7. / 4.;      // n=1, the result is written to an integer variable
  return 0;
}