Академический Документы
Профессиональный Документы
Культура Документы
Lecture 6
Summary
and Solved Problems
for the first mid-term exam
They include:
C++ Structure
Basic I/O
Data types
Operators
Intrinsic functions
Strings
Control structures:
Selection and Loops
5/29/2011
C++ Structure
The main function.
Input Calculate Output
#include <iostream>
#include <cmath>
int main() {
double circleArea;
cin >> circleArea;
double r = sqrt(circleArea/M_PI);
iostream
and cmath
optional
}
summary and solved problems for MT1
Basic I/O
#include <iostream>
using namespace std;
int main() {
Example run:
Input two reals and two integers: 34.5 6.234 78 36
a = 34.5, b = 6.234, c = 78, d = 36
summary and solved problems for MT1
5/29/2011
Data Types
The are many data types and qualifiers in C++.
You can solve most problems using only two: double and int.
int
Use this data type for representing integer numbers
Remember the range is limited to:
-2,147,483,648 (232) to 2,147,483,647 (232-1)
Examples:
int k, i=174;
k = -456;
double
Use this data type for representing floating point numbers.
Remember that the precision is limited to about 15 significant figures,
and the range is limited as follows:
negative
huge
tiny
positive
huge
-10+308
-10-324 10-324
+10+308
Examples:
double Weight;
double electronMass = 9.10938188e-31;
5/29/2011
Operators
There are many operators in C++ but all you need are:
Operator
Description
Example
Result
Addition
13 + 5
18
Subtraction
13 5
Multiplication
13 * 5
65
Division
13.0/5
13 / 5
2.6
2
%
(integers only)
Modulo
a % b
13 % 5
(remainder of a/b)
3
++
Increment
k++
add 1 to k
Important!
be careful with integer division and assignments:
13/5 results in 2 (the division of two integers results in an integer)
but 13.0/5 results in 2.6 because integer 5 is automatically cast to 5.0
but int n=2.6 results in 2 being assigned to n because n is type integer!
Sometimes you
need to cast:
double y;
int k=8, n=5;
y=k/double(n);
Intrinsic functions
There are many intrinsic functions in C++, a common group being math
functions provided by the cmath header:
#include <cmath>
With this inclusion you can now use the following functions:
cos(), sin(), exp(), sqrt(),
log(), log10(), pow(), .....
full list at
http://www.cplusplus.com/reference/clibrary/cmath/
5/29/2011
Intrinsic functions
Example:
// Velocity of a body after falling s metres
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double g = 9.81; // m/s^2
double s = 100.; // m
double v = sqrt(2*g*s);
cout << v << " m/s" << endl;
return 0;
}
Output 44.2945 m/s
summary and solved problems for MT1
Strings
Example
#include <iostream>
using namespace std;
int main () {
string s1 = "one_", s2, s3;
s2 = "two";
s3 = s1 + s2;
cout << s3 << endl;
return 0;
}
Output one_two
10
5/29/2011
Control: Selection
Three basic selection control structures:
if (condition) {
if (condition) {
statements
.
.
if (condition1) {
statements1
.
.
statements1
.
.
} else {
If a condition is true
then the block, defined
by the braces {...},
is executed.
} else if (condition2) {
statements2
.
.
statements2
.
.
} else {
statements3
.
.
}
11
(
(
(
(
(
(
x
x
x
x
x
x
< y )
<= y )
> y )
>= y )
== y )
!= y )
{
{
{
{
{
{
...
...
...
...
...
...
}
}
}
}
}
}
if x is less than y
if x is less than or equal to y
if x is greater than y
if x is greater than or equal to y
if x is equal to y
if x is not equal to y
if ( x > 2 || y == 3 ) { ... }
if ( !(x < y) ) { ...
}
if ( x ) { ... }
if x is greater than 2
and y is equal to 3
if x is greater than 2 or y is equal to 3
if x is not less than y
this is false if x==0, otherwise it is true
Not that the operator == makes a comparison while the operator = makes an
assignment. Common mistake:
if ( x = y ) { ...
DONT DO THIS!
12
5/29/2011
start
Input a
int main() {
double a, b, c;
cin >> a;
b = a + 2.0;
if ( a > 2.0 ) {
c = log(a+b);
} else {
c = pow(b,5.0);
}
cout << a << " "
<< c << endl;
return 0;
}
b=a+2
c = b5
a >2
c = ln(a+b)
Output a, c
end
13
Control: Loops
Three basic loop (repetitive) structures:
while (condition) {
statements
.
.
}
do {
statements
.
.
} while (condition);
14
5/29/2011
int i=1;
while (i<7) {
double x = 0.1*i;
cout <<i<<" "<<x<<endl;
i++;
}
Pseudo code:
loop i = 1 to 6
x = 0.1i
output i, x
end loop
int i=1;
do {
double x = 0.1*i;
cout <<i<<" "<<x<<endl;
i++;
} while (i<7);
}
int i=1;
while (1) {
double x = 0.1*i;
cout <<i<<" "<<x<<endl;
if (i==6) break;
i++;
}
}
0.1
0.2
0.3
0.4
0.5
0.6
#include <iostream>
using namespace std;
int main() {
}
#include <iostream>
using namespace std;
int main() {
1
2
3
4
5
6
#include <iostream>
using namespace std;
int main() {
15
+
Dont forget to bring your
calculator to the exam!
16
5/29/2011
Solved Problems
This section presents problems and their solutions.
Problems:
1. Exponential growth of bacteria (revisited)
2. Heat flow in foods
1. Exponential growth
of bacteria (revisited)
17
begin
= 0.468
input
, Q, t1, t2
m = Q t1
n = m e t2
output
m, n
end
18
5/29/2011
#include <iostream>
#include <cmath>
using namespace std;
int main () {
double b = 0.468;
// growth constant [1/hr]
double a, Q, t1, t2; // inputs
cout << "Input the volume number of bacteria [1/L]:
cin >> a;
cout << "
Input the volume flow rate [L/hr]:
cin >> Q;
cout << "
Input is the filtration duration [hr]:
cin >> t1;
cout << "
Input the growth duration [hr]:
cin >> t2;
double m = a*Q*t1;
double n = m*exp(b*t2);
";
";
";
";
19
0.25
36
5
19 24 hrs
0.25
36
4
20 24 hrs
Lets rewrite the program to use a loop to search values of t1 to find the
maximum value of n. We can make the values of and Q constant so
that the only variable is t1 with t2 = 24 - t1.
summary and solved problems for MT1
20
10
5/29/2011
t1 = i*0.2;
//
t2 = 24-t1;
//
m = a*Q*t1;
n = m*exp(b*t2);
cout << fixed << "t1=" << t1 << " n=" << n << endl;
} // end loop
return 0;
}
summary and solved problems for MT1
n=123770.789824
n=225422.984054
n=307921.128718
n=373876.581528
n=425586.959955
n=465071.359521
n=494101.783271
n=514231.178315
n=526818.434839
n=533050.666589
n=533963.059029
n=530456.541976
n=523313.517015
n=513211.846226
n=500737.287359
n=486394.541410
n=470617.061292
n=453775.754781
n=436186.701040
n=418117.987514
21
n=526818.434839
n=528929.386168
n=530660.886035
n=532029.357415
n=533050.666589
n=533740.140107
n=534112.581268
n=534182.286124
n=533963.059029
n=533468.227729
n=532710.658031
n=531702.768037
n=530456.541976
n=528983.543623
n=527294.929345
22
11
5/29/2011
23
Discussion
24
-t1 ]
=0
That was not too difficult; but often an analytical solution is difficult
or impossible to obtain and so we use a numerical method
requires Computer Programming!
Also our numerical method shows us that a wide range of values are
close to optimal, i.e. we gained some extra information by using a
numerical method.
summary and solved problems for MT1
24
12
5/29/2011
The Problem
A solid food has a temperature
difference T between two sides.
Heat flows via conduction at a
rate Q through a surface area A
according to the equation:
A [m2]
T [oC]
L [m]
Q = k A T / L
where k is the thermal conductivity given by:
k = 0.25 mc + 0.155 mp + 0.160 mf + 0.135 ma + 0.580 mm
where m are the food mass fractions (mc + mp + mf + ma + mm = 1)
c = carbohydrate, p = protein, f = fat, a = ash, and m = moisture.
See Fourier's Law of Conduction
summary and solved problems for MT1
25
The Task
Write a program that inputs the required data and
outputs the rate of heat flow through the food.
Analysis
input: A, T, L
We want Q = k A T / L
[ 0.25 mc + 0.155 mp + 0.160 mf + 0.135 ma + 0.580 mm ]
input: mc , mp , mf , ma , mm
26
13
5/29/2011
Flow chart
start
input:
mc , mp , mf ,
ma , mm
input:
A, T, L
false
mc+mp+mf +
ma+mm = 1 ?
Q = k A T / L
true
output: Q
end
27
Algorithm
(Follow the flow chart, but add some instructions to the user)
28
14
5/29/2011
Implementation in C++
<<
<<
<<
<<
<<
"Input
"Input
"Input
"Input
"Input
cin
cin
cin
cin
cin
>>
>>
>>
>>
>>
Mc;
Mp;
Mf;
Ma;
Mm;
29
30
15
5/29/2011
Example run:
Input the Carbohydrate mass fraction: 0.15
Input the
Protein mass fraction: 0.18
Input the
Fat mass fraction: 0.23
Input the
Ash mass fraction: 0.02
Input the
Moisture mass fraction: 0.52
Mass fraction sum (1.1) should equal 1!
Input the Carbohydrate mass fraction: 0.15
Input the
Protein mass fraction: 0.08
Input the
Fat mass fraction: 0.23
Input the
Ash mass fraction: 0.02
Input the
Moisture mass fraction: 0.52
Input the conduction surface area [m^2]: 0.2
Input the
temperature difference [C]: 25.
Input the
conduction length [m]: 0.05
Rate of heat flow = 39.1 Watts
31
Finally
In this lecture we summarised the C++ syntax and structure you
need for the first mid-term exam.
You can find the lecture slides in the course website:
http://eee241.gantep.edu.tr/
For further reading, search for the relevant sections at:
http://cpp.gantep.edu.tr/
and
http://www.learncpp.com/
32
16
5/29/2011
33
Selection structures
Loop structures [i = 1 to 6]
if (condition) {
statements
}
if (condition) {
statements1
} else {
statements2
}
int i=1;
while (i<7) {
statements
i++;
}
if (condition1) {
statements1
} else if (condition2) {
statements2
} else if (condition3) {
statements3
} else {
statements4
}
int i=1;
while (true) {
statements
if (i==6) break;
i++;
}
logical operators
if
if
if
if
if
if
if
if
(
(
(
(
(
(
(
(
x < y ) ...
x <= y ) ...
x > y ) ...
x >= y ) ...
x == y ) ...
x != y ) ...
!(x < y) ) ...
x ) ...
int i=1;
do {
statements
i++;
} while (i<7);
Also the continue statement
Intrinsic functions
sin()
asin()
fabs()
exp()
int()
cos()
acos()
sqrt()
log()
double()
tan()
atan()
pow()
log10()
rand()
17