Sie sind auf Seite 1von 5

Lecture 5 programming

(C ++) programming
Data type Data
int 5, -5 , 20 , -12 (may be positive or negative)
long 5, -5 , 20 , -12 (may be positive or negative)
unsigned int
Positive only
unsigned long
float
0.___
double
char 'A' , 'B' , 'C'
string "Hello" → array of characters

Variable declaration:
int x; int x,y; int main
int y; (void)

float a,b,c; int x,y;


double x1,x2,x3;
float z;

char c;
Rules of naming variables:
1) Length is less than 31 characters
2) Not from the reserved words
3) Should start with alphabet
4) Any following character can be alphanumeric
5) Should not contains any space or special character (symbols)
6) The only allowed special character is under score (_)
7) Variable are case sensitivity (UPPER CASE \ lower case)

Examples:
 Int →  (the reserved is "int" and not "Int")
 Flaot →  (check spelling , it's not "float")
 MAX value →  (there is a space)
o MAX_value →  (correction)

|Page1
Lecture 5 programming

 Money$ →  (special character)


 MoNeY → 
 1st_value →  (doesn't start with alphabet)
o _1st_value →  (correction)
 X12A_B → 

Variable assignment:
int x;

x=5;

x=10;

Arithmetic expression:

expression Name
+ Addition

- Subtraction

* Multiplication

/ Division

% Modulo

++ Increment

-- Decrement

Example
Y=2+5*4/2

Priority of operators:

a) ( brackets )
b) [ * , / , %] (from left to right)
c) [+ , -] (from left to right)

|Page2
Lecture 5 programming

Ex: what is the value of y?


( ( 6 + 4 ) / 2 * 4 ) / ( 4 + ( 3 * 2 ) / ( 2 – 1 ) )

= ( 10 / 2 * 4 ) / ( 4 + ( 3 * 2 ) / ( 2 – 1 ) )

= ( 10 / 2 * 4 ) / ( 4 + 6 / ( 2 – 1 ) )

= ( 10 / 2 * 4 ) / ( 4 + 6 / 1 )

= ( 5 * 4 ) / ( 4 + 6 / 1 )

= 20 / ( 4 + 6 / 1 )

= 20 / ( 4 +6)

= 20 / 10

=2

Operation in C++

math Programming
𝐚
Y= Y = a/b
𝐛
𝐚+𝐛
Y= Y = (a+b)/(c+d)
𝐜+𝐝
𝐚
Y =3 Y = 3*(a/b)
𝐛
𝐱
Y = 1+ Y = 1+ x/(y+z)
𝐲+𝐳

Integer arithmetic:
5/2 = 2 → (integer division)

5./2 = 2.5

int ∆ int = int

|Page3
Lecture 5 programming

int ∆ long = long

int ∆ float = float

float ∆ double = double

Modulo operator (%):


integer only

Integer is contains long


int x; and characters int x;
int y; float y;
Error : type mismatch
x=y%5;
x=y%5;

Y=x%n; Is a periodic function with period (n) and values from zero to (n-1)

Mathematical functions
#include <math.h>

math C++
Y = |x| Y=abs(x);
Y = 𝐬𝐢𝐧 𝐱 Y=sin(x);
Y = 𝐜𝐨𝐬 𝐱 Y=cos(x);
Y = 𝐭𝐚𝐧 𝐱 Y=tan(x);
Y = 𝐬𝐢𝐧−𝟏 𝐱 Y=asin(x);
Y = 𝐜𝐨𝐬 −𝟏 𝐱 Y=acos(x);
Y = 𝐭𝐚𝐧−𝟏 𝐱 Y=atan(x);
Y = 𝐞𝐱 Y=exp(x);
Y = 𝐥𝐨𝐠 𝐱 Y=log10(x);
Y = 𝐥𝐧 𝐱 Y=log(x);

|Page4
Lecture 5 programming

Y = 𝐱𝐚 Y=pow(x,a);
Y = √𝐱 Y=sqrt(x);
Y = ⌊ 𝐱⌋ Y = floor(x);
Y = ⌈ 𝐱⌉ Y = ceil(x);

X10 = pow(x,10) ≠ pow(10,x) ≠ x^10

−𝒃 ± √𝒃𝟐 − 𝟒𝒂𝒄
𝟐𝒂

= (-b+sqrt(pow(b,2)-4*a*c)/2*a)

|Page5

Das könnte Ihnen auch gefallen