AE108 Lecture04
AE108 Lecture04
INTRODUCTION TO
COMPUTER
PROGRAMMING
Lecture 4
1
Contents
Basic Operators
Basic Strings
Header Files
Basic Intrinsic Functions
Examples
2
Basic Operators
Operators are special symbols that perform operations with variables
and constants.
Arithmetic operators
int x, y;
x = 2;
y = 5*x; // y = 10
x = x + 4; // x = 6
y = y/2; // y = 5
Chained assignment
m = (n = 66) + 9; // n = 66 and m = 75
x = y = 22; // x = 22 and y = 22
4
Compound assignment operators (+=, -=, *=, /=, %=)
5
Increase and decrease by 1 (++, --)
a = 5;
b = ++a; assigns a=6 and then b=6
6
Integer division
int i, j, k;
double p, q;
i = 4/2; results in the assignment i=2
j = 5/2; results in the assignment j=2
p = 5/2; results in the assignment p=2.0
p = 5/2.0; results in the assignment p=2.5
q = i + p; results in the assignment q=2.0+2.5 = 4.5;
k = 25.0/2; results in the assignment k=12
Type casting
int i;
double d;
i = int(7.25); results in the assignment i=7
d = double(5); results in the assignment d=5.0
7
The sizeof() operator
The operator sizeof() is used to calculate the size in bytes
of data types, variables, arrays or literals.
For example
int i;
double d;
cout << "sizeof(int) = " << sizeof(int) << " bytes" << endl;
cout << "sizeof(float) = " << sizeof(float) << " bytes" << endl;
cout << "sizeof(double)= " << sizeof(double)<< " bytes" << endl;
cout << "sizeof(i) = " << sizeof(i) << " bytes" << endl;
cout << "sizeof(d) = " << sizeof(d) << " bytes" << endl;
Output
sizeof(int) = 4 bytes
sizeof(float) = 4 bytes
sizeof(double)= 8 bytes
sizeof(i) = 4 bytes
sizeof(d) = 8 bytes
8
Basic Strings
A string is a series of characters, such as “Hello
World!”
9
Example: Using strings
#include <iostream>
#include <string>
using namespace std;
int main () {
string name;
Output
What is your name? Mert
Hello Mert
10
Header Files
The #include directive allows the program to use source code from
another file.
#include <iostream>
refers to an external file named iostream, and tells the preprocessor to
take the iostream file and insert in the current program.
12
13
Example program: Using trigonometric functions
#include <iostream>
#include <cmath>
using namespace std;
int main () {
double beta;
cout << "Input an angle in degrees: ";
cin >> beta;
beta = beta * M_PI/180.0; // convert to radians
cout << "sin(beta) = " << sin(beta) << endl;
cout << "cos(beta) = " << cos(beta) << endl;
cout << "tan(beta) = " << tan(beta) << endl;
}
double x;
cout << “Input a value: ";
cin >> x;
cout << "log(x) = " << log(x) << endl;
cout << "log10(x) = " << log10(x) << endl;
cout << "exp(x) = " << exp(x) << endl;
cout << "pow(x,2.5)= " << pow(x,2.5) << endl;
P2(x2,y2)
P1(x1,y1)
16
#include <iostream>
#include <cmath>
using namespace std;
int main () {
double x1,x2,y1,y2,distance;
cout<<"input the coordinates of first point"<<endl;
cin>>x1>>y1;
cout<<"input the coordinates of second point"<<endl;
cin>>x2>>y2;
distance=sqrt(pow(x2-x1,2.)+pow(y2-y1,2.));
cout<<"the distance is "<<distance<<endl;
system("pause");
}
17
Example:
Find the deflection of a cantilever beam under
the action of an end load as shown in Figure.
Hint: 3
P*L
Deflection
3* E * I
P
E, I
18
#include <iostream>
#include <cmath>
using namespace std;
int main () {
double P,l,E,In,deflection;
cout<<"input the load\n";
cin>>P;
cout<<"input the length of beam\n";
cin>>l;
cout<<"input the inertia\n";
cin>>In;
cout<<"input the Young's modulus\n";
cin>>E;
deflection=P*pow(l,3.)/(3*E*In);
cout<<"deflection is "<<deflection<<endl;
system("pause");
}
19