0% found this document useful (0 votes)
15 views19 pages

AE108 Lecture04

This document covers the basics of C++ programming, focusing on operators, strings, header files, and intrinsic functions. It explains operator precedence, assignment, and provides examples of using strings and mathematical functions. Additionally, it includes example programs demonstrating how to calculate distances and deflections in engineering contexts.

Uploaded by

zhems s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views19 pages

AE108 Lecture04

This document covers the basics of C++ programming, focusing on operators, strings, header files, and intrinsic functions. It explains operator precedence, assignment, and provides examples of using strings and mathematical functions. Additionally, it includes example programs demonstrating how to calculate distances and deflections in engineering contexts.

Uploaded by

zhems s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

AE 108

INTRODUCTION TO
COMPUTER
PROGRAMMING

Lecture 4

C++ operators, intrinsic functions, and


strings

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

Operator precedence: first ( ), then * and / , and finally + and –


2 - 3 * 4 + 2 = -8
2 * 3 + 4 - 2 = 8
2 * (3 + 4) - 2 = 12
3 * 5 / 3 = 5
10 / 2 * 3 = 15 evaluate left-to-right!
(5 + (11-5) * 2) * 4 + 9 = 77
3
The assignment operator (=)

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 (+=, -=, *=, /=, %=)

Note that x *= a+b expands to x = x * (a+b)


which is generally not the same as x = x * a+b
Similarly x /= a+b expands to x = x / (a+b)

5
Increase and decrease by 1 (++, --)

 The following are equivalent in functionality


x = x + 1; x = x - 1;
x += 1; x -= 1;
x++; x--;

 ++ and -- can be used both as a prefix and as a suffix.


a = 5;
b = a++; assigns b=5 and then a=6

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!”

 A string variable can be declared and assigned as


string s = "This is string";
follows:

Note that you need to include the <string> header.


string s1, s2, s3, s4;
 Some basic operations can be performed on strings.
s1 = "centi";
s2 = "meter";
s3 = s1; results in the assignment s3="centi"
s4 = s1 + s2; results in the assignment s4="centimeter"

9
Example: Using strings

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

int main () {

string name;

cout << "What is your name? ";


cin >> name;
cout << "Hello " << name << endl;

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.

 The files that are


included are
called header files.

 The C/C++ standard


library traditionally
declares standard
functions and
constants in header
files.
11
Basic Intrinsic Functions
An intrinsic or a library function is a function provided by the C+
+ language.
For example the cmath library contains mathematical
functions/constants:

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;
}

OutputInput an angle in degrees: 60


sin(beta) = 0.866025
cos(beta) = 0.5
tan(beta) = 1.73205
14
Example program: Using logarithmic functions
#include <iostream>
#include <cmath>
using namespace std;

int main (){

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;

Input a value: 1.4


Output log(x) = 0.336472
log10(x) = 0.146128
exp(x) = 4.0552
pow(x,2.5)= 2.3191
15
Example

 Write a program to find the distance between


two poins. Hint:
Distance  x 2  x12  y 2  y12

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

You might also like