# 3. Arithmetic Operations

``````#include <cmath>

std::pow(base,exponent)

//pi is equal to M_PI
//M_PI = 3.14159265358979323846
area = M_PI * r * r

``````
``````/*Goal: practice arithmetic operations in C++
**Write a program that calculates the volumes of:
**a cube, sphere, cone.
**Cube Volume = side^3
**Sphere Volume = (4/3) * pi * radius^3
**Cone Volume = pi * radius^2 * (height/3)
**Write the values to the console.
*/

#include<iostream>
#include <cmath>
using namespace std;

int main()
{
//Dimension of the cube
float cubeSide = 5.4;
//Dimension of sphere
//Dimensions of cone
float coneHeight = 14;

float volCube, volSphere, volCone = 0;

volCube = pow(cubeSide,3);
volSphere = (4.0/3.0) * M_PI * pow(sphereRadius,3);
volCone = M_PI * pow(coneRadius,2) * (coneHeight/3);

cout << volCube << endl;
cout << volSphere << endl;
cout << volCone << endl;
return 0;
}
``````

``````157.464
52.9854
857.985
``````

# 6. Variable Assignment

1. The type of every variable must be known at compile time.
2. C++ allows for some implicit conversions 隐式转换.
``````/*Goal: understand C++ implicit variable assignments.
**Run the program to see implicit variable assignment at work.
*/

#include<iostream>

int main()
{

int a = 65;
char charA = 65;
char charB = 'B';
char charC = 67;
int integer = 80;
float floatNumber = 0.0;

std::cout<<"a = "<<a<<"\n";
std::cout<<"charA = "<<charA<<"\n";
std::cout<<"charB = "<<charB<<"\n";

//we can assign an integer to a float
floatNumber = integer;
std::cout<<"integer = "<<integer<<"\n\n";
std::cout<<"floatNumber = integer = "<<floatNumber<<"\n";

//we can assign a char to a float
floatNumber = charB;
std::cout<<"floatNumber = charB = "<<floatNumber<<"\n";

//But assigning a float to a char doesn't quite work

//assigning a float to an interger, results in the float being truncated
std::cout<<"integer = floatNumber = "<<integer<<"\n";
return 0;
}

``````
``````a = 65
charA = A
charB = B
integer = 80

floatNumber = integer = 80
floatNumber = charB = 66
integer = floatNumber = 16
``````
``````  #include <iostream>

int main()
{
float width = 4.5;
float height = 5.5;

int area = width * height;
std::cout << "area = " << area;
return 0;
}
``````

``````#include <iostream>
int main(void)
{
int numerator = 4;
int denominator = 5;

float answer = numerator / denominator;
return 0;
}

``````

``````  #include <iostream>
int main(void)
{
int numerator = 4;
float denominator = 5;

return 0;
}
``````

# 11. PreFix and PostFix

Incrementing：

• prefix: ++a
• postfix: a++

Decrementing：

• prefix: –a
• postfix: a–
1. Prefix operators increment the value of the variable, then return the reference to the variable.
2. Postfix operators create a copy of the variable and increments the value of the variable. Then it returns a copy from BEFORE the increment.

``````/*Goal: Practice PreFix and PostFix
**
*/

#include<iostream>

using namespace std;

int main()
{
int a, b = 0;
int post, pre = 0;
cout<<"Inital values: \t\t\tpost = "<<post<<" pre= "<<pre<<"\n";
post = a++; // post = 0, a = 1
pre = ++b; // pre = 1, b = 1
cout<<"After one postfix and prefix: \tpost = "<<post<<" pre= "<<pre<<"\n";
cout<<"After one postfix and prefix: \ta = "<<a <<" b= "<<b<<"\n";
post = a++; // post = 1, a = 2
pre = ++b; // pre = 2, b = 2
cout<<"After two postfix and prefix: \tpost = "<<post<<" pre= "<<pre<<"\n";
cout<<"After one postfix and prefix: \ta = "<<a <<" b= "<<b<<"\n";
return 0;
}
``````

``````Inital values: 			post = 0 pre= 0
After one postfix and prefix: 	post = 0 pre= 1
After one postfix and prefix: 	a = 1 b= 1
After two postfix and prefix: 	post = 1 pre= 2
After one postfix and prefix: 	a = 2 b= 2
``````

# 13. Variable Assignment Operators

``````/*Goal: understand variable assignment operators*/

#include<iostream>

int main()
{
int a = 0;
std::cout<<"Variable\t\tOperation\tResult\n";
std::cout<<"a = "<<a;
a += 2;
std::cout<<"\t\t\ta += 2 \t\t a = "<<a<<"\n"; // 2

std::cout<<"a = "<<a<<" : ";
a -= 4;
std::cout<<"\t\ta -= 4 \t\t a = "<<a<<"\n"; // -2

int b = 3;
std::cout<<"a = "<<a<<", b = "<<b<<" : "; // -2,3
a *= b;
std::cout<<"\ta *= b \t\t a = "<<a<<"\n"; // -6

std::cout<<"a = "<<a<<", b = "<<b<<" : "; // -6, 3
a /= b;
std::cout<<"\ta /= b \t\t a = "<<a<<"\n"; // -2

return 0;
}
``````

``````Variable		Operation	Result
a = 0			a += 2 		 a = 2
a = 2 : 		a -= 4 		 a = -2
a = -2, b = 3 : 	a *= b 		 a = -6
a = -6, b = 3 : 	a /= b 		 a = -2
``````