C++ Programming 1 Problem Solving and Algorithms

Download Report

Transcript C++ Programming 1 Problem Solving and Algorithms

Al-Imam Mohammad Ibn Saud University
CS140: C++ Programming 1
Lecture 01:
Problem Solving and Algorithms
Dr. Anis Koubaa
1
18-Jul-15
Who I Am?

Dr. Anis Koubâa





Background



Assistant Professor at CS Dept.
Research Associate at ISEP/IPP-HURRAY! (Portugal)
Email (Teaching): [email protected]
Phone: 25 86 720
PhD from INPL/LORIA, Nancy (France)
Master from UHP Nancy I (France)
My Research


Main: Wireless Sensor Networks, Cyber-Physical Systems, Real-Time
Embedded Systems, Quality-of-Service, Performance Evaluation
Other: Security
Teaching Assistant

Adurrahman Alosaimy

Contact info: see website
Texbook


C++ How to Program, Fourth Edition
ISBN:0130384747
© 2002
pages: 1408
Grading

Grading is as follow







Mid-term, 20 points
Final Exam, 40 points
Project, 10 points
Lab Exams 01, 10 points
Lab Exam 02, 10 points
Participation, Quizzes, Homework, 10 points
Bonus, 5 points (-1 point for each absence, delayed or non
returned homework, it becomes 0 for any cheating)
For more information …

Official course website



http://coins.csrlab.org/imamu/akoubaa/cs140/
All announcements will be posted on the website
Interaction with students

Mailing list:


[email protected]
To register:

http://groups.google.com/group/ccis-cs140-spring2011
Goals
Review the concept of Problem Solving
 Learn the basics of an algorithm

7
Definitions
Problem Solving


Find a solution to particular compting problem
Algorithm


8
A set of steps to solve a compting problem
Examples
Problem 1


Find a solution to the sum of two integers X = 3 and Y = 2

Simple: X + Y = 5
Problem 2


Find a solution X such that F(X) = a*X - b = 0

9
Simple: X = b/a
Examples

Problem 3

Find a solution X to this quadratic equation
a×x 2 + b ×x + c = 0

Solution
ìï
ïï
b+
ïï X 1 =
ïí
ïï
ïï X = b ïïî 2
10
b 2 - 4a ×c
2×a
b 2 - 4a ×c
2×a
Examples

Problem 4

Find the sum of two matrices A and B
A+
éa11
ê
êa21
ê
êa
ë 31

B=C
a12 a13 ù éb11 b12 b13 ù éc11 c12 c13 ù
ú ê
ú ê
ú
a22 a23 ú+ êb21 b22 b23 ú= êc 21 c 22 c 23 ú
ú ê
ú ê
ú
êb
ú êc
ú
a32 a33 ú
b
b
c
c
31
32
33
31
32
33
û ë
û ë
û
Solution
cij = aij + bij
11
Examples

Problem 5

Find the product of two matrices A and B
A´
éa11
ê
êa21
ê
êa
ë 31

B=C
a12 a13 ù éb11 b12 b13 ù éc11 c12 c13 ù
ú ê
ú ê
ú
a22 a23 ú´ êb21 b22 b23 ú= êc 21 c 22 c 23 ú
ú ê
ú ê
ú
êb
ú êc
ú
a32 a33 ú
b
b
c
c
31
32
33
31
32
33
û ë
û ë
û
Solution
3
c ij =
å
k =1
12
aik ´ bkj
Examples

Problem 5

Routing: Find the shortest path between two cities
1
140
230
2
250
250
400
9
50
150
110
50
210
70
5
6
8
450
13
3
100
7
4
Examples

Problem 6

Travelling Salesman Problem (TSP): Given a list of cities and
their pairwise distances, the task is to find a shortest possible
tour that visits each city exactly once.
1
250
140
230
2
210
9
50
150
6
8
450
14
70
50
250
300
110
3
5
100
7
4
Algorithm

Solving a problem requires



An algorithm




iterative computing operations
a strategy to reach the solution
is a set of actions/operations organized in a particular order
computes a solution to a problem
defines a strategy to reach the solution
Pseudo Code

15
is a human language used to describe an algorithm
‫‪Let us write it better‬‬
‫‪Problem 1‬‬
‫‪Find a solution to the sum of two integers X = 3 and Y = 2‬‬
‫‪‬‬
‫‪Pseudo Code‬‬
‫خوارزمية‬
‫البداية‬
‫المتغيرات‪ :‬أ ‪ ،‬ب‬
‫أ = ‪2‬‬
‫ب = ‪3‬‬
‫النتيجة = أ ‪ +‬ب‬
‫اكتب (النتيجة)‬
‫النهاية‬
‫المدخالت‬
‫أ‪ ،‬ب‬
‫‪‬‬
‫‪‬‬
‫المخرجات‬
‫الخوارزمية‬
‫النتيجة‬
‫‪16‬‬
Examples

Problem 1


Find a solution to the sum of two integers X = 3 and Y = 2
Pseudo Code
Algorithm
BEGIN
Variable X, Y, RESULT;
X= 2;
Y=3;
INPUT
X,Y
RESULT = X + Y;
Write (RESULT);
END
17
OUTPUT
RESULT
Algorithm
Examples

Problem 1


Find a solution to the sum of two integers X = 3 and Y = 2
Pseudo Code
Algorithm
BEGIN
Variable X, Y, RESULT;
X= 2;
Y=3;
RESULT = X + Y;
Write (RESULT);
END
18
X
Y
RESULTS
X=2
Y=3
RESULTS=5
Let us write it better

Problem 1


Find a solution to the sum of two integers X and Y
Pseudo Code
‫خوارزمية‬
‫البداية‬
‫ ب‬، ‫ أ‬:‫المتغيرات‬
‫إدخال المتغير أ‬// )‫اقرأ(أ‬
)‫اقرأ(ب‬
‫ ب‬+ ‫النتيجة = أ‬
)‫اكتب (النتيجة‬
‫النهاية‬
19
Examples

Problem 1

Find a solution to the sum of two integers X = 3 and Y = 2
Algorithm
BEGIN
Variable X, Y, RESULT;
Read(X);
Read(Y);
RESULT = X + Y;
Write (RESULT);
END
20
Let us write it better

Problem 1

Find a solution to the sum of two integers X = 3 and Y = 2
Algorithm
BEGIN
Integer X, Y, RESULT;
Read(X);
Read(Y);
RESULT = X + Y;
Write (Result);
END
21
Practice

Problem 2


Find a solution X such that F(X) = a*X - b = 0
Solution

X = b/a
Algorithm Problem2_version1
BEGIN
float a, b, RESULT;
a = 2.0;
b =3.0;
RESULT = b/a;
Write (RESULT);
END
22
Practice

Problem 2


Find a solution X such that F(X) = a*X - b = 0
Solution

X = b/a
Algorithm Problem2_version2
BEGIN
float a, b, RESULT;
Read (a);
Read (b);
RESULT = b/a;
Write (RESULT);
END
23
ìï
ïï
b+
ïï X 1 =
ïí
ïï
ïï X = b ïïî 2
Practice

Problem 3

Write an algorithm that reads three parameters a, b and c and returns the
solution X1 and X2 to this quadratic equation
a×x 2 + b ×x + c = 0
Algorithm Problem3_version1
BEGIN
Variable a, b, c, X1, X2;
Not always correct
Read (a);
Read (b);
Read (c);
X1= (b + square root(b*b – 4*a*c))/2*a;
X2= (b - square root(b*b – 4*a*c))/2*a;
Write (X1, X2);
END
24
b 2 - 4a ×c
2×a
b 2 - 4a ×c
2×a
A better solution

Problem 3


Write an algorithm that reads three parameters a, b and c and
returns the solution X1 and X2 to this quadratic equation
Solution
a×x 2 + b ×x + c = 0
delta = b 2 - 4 ×a ×c
if delta p 0, there are no solution
if delta = 0, there is one solution X =
25
b
2a
2
ìï
ïï X 1 = b + b - 4ac
ïï
2a
if delta f 0, there are two solutions í
2
ïï
b
b
- 4ac
ïï X 2 =
ïïî
2a
delta = b 2 - 4 ×a ×c
if delta p 0, there are no solution
if delta = 0, there is one solution X =
Practice
Algorithm Problem3_version1
BEGIN
Variable a, b, c, X1, X2, delta;
Read (a); Read (b); Read (c);
delta = b–4*a*c;
if (delta > 0) then write )“no result”(;
if (delta = 0) then
BEGIN-IF
X1=b/2*a;
write (X1);
END-IF
if (delta = 0) then
BEGIN-IF
X1= (b + square root(b*b – 4*a*c))/2*a;
X2= (b - square root(b*b – 4*a*c))/2*a;
Write (X1, X2);
END-IF
END
26
b
2a
2
ìï
ïï X 1 = b + b - 4ac
ïï
2a
if delta f 0, there are two solutions í
2
ïï
ïï X 2 = b - b - 4ac
ïïî
2a
Practice

Problems to reckon …




27
Write an algorithm that reads two parameters a, b and returns
maximum value
Write an algorithm that reads three parameters a, b and c and
returns their average
Write an algorithm that reads one value and returns its square
root.
Write an algorithm that read two values length and width and
return the surface and perimeter of the corresponding
rectangle.
Review


Problem solving: process for finding a solution to a problem
Algorithm: set of action to solve a problem
28