Programming for
Engineers
Dr. Sohail Abbas
Essentials
Course Description:
This course covers introductory concepts in computer programming
using C++. We assume that students have no programming
experience. There is an emphasis on both the concepts and
practice of computer programming. This course covers principles of
problem solving and requires a number of labs and programming
assignments. You should expect to spend at least 8 hours on
average per week on this course.
Textbook:
C++ Programming: From Problem Analysis to Program Design.
Fourth / latest Edition, D. S. Malik. (Other versions are also suitable).
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 2
Essentials
Assessment:
• Labs 10% (average of best 10) – No makeup
• Theory Quizzes 10% (average of best 3) – No makeup
• Lab Quizzes 5% (average of best 2 out of 3) – No makeup
• Homework 10% (4 homeworks)
• Mid-Term Exam 20%
• Final Exam (Theory 35% + Lab 10%): 45%
• NO Plagiarism / Cheating! If found, that item will be cancelled.
Attendance:
• 1st warning at 10%.
• 2nd warning at 15%.
• No final exam at 20%.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 3
Introduction
• Computer means to compute/calculate.
− Main Components
10 + 20
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 4
Introduction
• Programming means talking to a computer.
• Programming is used to create a software
• Without software, the computer is useless
• Software developed with programming
languages
− C++ is a programming language
• C++ suited for a wide variety of programming
tasks
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 5
The Language of a Computer
• Digital signals are sequences of 0s and 1s
• Machine language: language of a computer
• Binary digit (bit):
− The digit 0 or 1
• Binary code:
− A sequence of 0s and 1s
• Byte:
− A sequence of eight bits
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 6
Coding Schemes
• ASCII (American Standard Code for
Information Interchange)
− 128 characters
− A is encoded as 1000001 (66th character)
− 3 is encoded as 0110011
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 7
8
The Evolution of Programming
Languages
• Early computers were programmed in
machine language
• To calculate wages = rates * hours in
machine language:
100100 010001 //Load
100110 010010 //Multiply
100010 010011 //Store
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 9
Assembly Language
• Assembly language instructions are mnemonic
• Assembler: translates a program written in
assembly language into machine language
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 10
Assembly Language (continued)
• Using assembly language instructions,
• wages = rates • hours can be written as:
LOAD rate
MULT hour
STOR wages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 11
High-Level Languages
• High-level languages include Basic,
FORTRAN, COBOL, Pascal, C, C++, C#, and
Java
• Compiler: translates a program written in a
high-level language to machine language.
• The equation wages = rate • hours can
be written in C++ as:
wages = rate * hours;
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 12
Let’s write our first C++ Program
Write a program in C++ that prints your name on the screen.
Hint: we will use a special statement (command) that is used to print
something on the screen, called “cout”.
You MUST know about each command/statement:
1. format/syntax, 2. usage/purpose
#include <iostream>
using namespace std;
int main() Purposes of cout statement
{ 1. Printing strings/text
2. Evaluate arithmetic expressions
cout<< “Sohail” ; 3. Formatting using manipulators
cout<<endl;
cout<< “Abbas”;
return 0;
}
[Link] 13
Another C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "My first C++ program." << endl;
cout << "The sum of 2 and 3 = " << 5 << endl;
cout << "7 + 8 = " << 7 + 8 << endl;
return 0;
} Purposes of cout statement
1. Printing strings
Sample Run: 2. Evaluate arithmetic expressions
My first C++ program. 3. Formatting using manipulators
The sum of 2 and 3 = 5 4. Printing value of a variable
7 + 8 = 15
14
Running a C++ Program
Editor (Write a C++ Program)
C++ Compiler
Syntax
Error
Compiler
(Translate to Machine Language)
Execution
(Computer Follows
the Instructions)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 15
Processing a Program
• To execute a C++ program:
− Use an editor to create a source program in C+
+
− Use the compiler to:
• Check that the program obeys the rules
• Translate into machine language (object program)
− The last step is to execute the program
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 16
Programming with the Problem
Analysis–Coding–Execution Cycle
• Programming is a process of problem solving
• One problem-solving technique:
− Analyze the problem
− Outline the problem requirements
− Design steps (algorithm) to solve the problem
• Algorithm:
− Step-by-step problem-solving process
− Solution achieved in finite amount of time
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 17
Programming with the Problem
Analysis–Coding–Execution Cycle
• Step 1 - Analyze the problem
− Outline the problem and its requirements
− Design steps (algorithm) to solve the problem
• Step 2 - Implement the algorithm
− Implement the algorithm in code
− Verify that the algorithm works
• Step 3 - Maintenance
− Use and modify the program if the problem
domain changes
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 18
Programming
Problem
Analysis
Algorithm
Editor (Write a C++ Program) C++ Compiler
Error
Compiler (Translate to Machine Language)
No Error
Error
Execution (Computer Follows the Instructions)
No Error
Results
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 19
Analyze the Problem
• Thoroughly understand the problem
• Understand problem requirements
− Does program require user interaction?
− Does program manipulate data?
− What is the output?
• If the problem is complex, divide it into subproblems
− Analyze each subproblem as above
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 20
Design an Algorithm
• If problem was broken into subproblems
− Design algorithms for each subproblem
• Check the correctness of algorithm
− Can test using sample data
− Some mathematical analysis might be
required
In an algorithm you need to determine:
1. Input
2. Processing
3. Output
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 21
In an algorithm you need to determine:
1. Input
Design an Algorithm 2.
3.
Processing
Output
1. Get rate, hours
2. Use the following equation to calculate the wages:
wages = rate * hours
3. Output wages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 22
Write the Code
• Once the algorithm is designed and correctness verified
− Write the equivalent code in high-level language
• Enter the program using text editor
1. Get rate, hours
2. Use the following equation to calculate the wages:
wages = rate * hours
3. Output wages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 23
Compiling and Execution
• Run code through compiler
• If compiler generates errors
− Look at code and remove errors
− Run code again through compiler
• If there are no syntax errors
− Compiler generates equivalent machine code
• The final step is to execute the program
• Compiler guarantees that the program follows the rules of the
language
− Does not guarantee that the program will run correctly
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 24
Example 1-1
Design an algorithm to find the perimeter and
area of a rectangle
• The perimeter and area of the rectangle are
given by the following formulas:
perimeter = 2 * (length + width)
area = length * width
Hint: In an algorithm you need to determine:
1. Input
2. Processing
3. Output
25
Example 1-1 (continued)
• Algorithm:
1. Input
− Get length of the rectangle 2. Processing
3. Output
− Get width of the rectangle
− Find the perimeter using the following
equation:
perimeter = 2 * (length + width)
− Find the area using the following equation:
area = length * width
− Print Perimeter and Area on the screen
26
Example 1-2
• Find the Total Salary of a Salesperson
• Every salesperson has a base salary
• Salesperson receives $10 bonus at the end
of the month for each year worked if he or
she has been with the store for five or less
years
• The bonus is $20 for each year that he or she
has worked there if over 5 years
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 27
Example 1-2 (continued)
• Additional bonuses are as follows:
− If total sales for the month are $5,000-
$10,000, he or she receives a 3% commission
on the sale
− If total sales for the month are at least
$10,000, he or she receives a 6% commission
on the sale
Overall idea:
totalSalary = baseSalary + bonus + additionalBonus
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 28
Example 1-2 (continued)
• Get baseSalary 1. Input
2. Processing
• Get noOfServiceYears 3. Output
• Calculate bonus using the following formula:
if (noOfServiceYears is less than or equal to
five)
bonus = 10 * noOfServiceYears
otherwise
bonus = 20 * noOfServiceYears
• Get totalSales
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 29
Example 1-2 (continued)
• Calculate additionalBonus as follows:
if (totalSale is less than 5000)
additionalBonus = 0
otherwise
if (totalSale is greater than or equal to
5000 and totalSale is less than 10000)
additionalBonus = totalSale * (0.03)
otherwise
additionalBonus = totalSale * (0.06)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 30
Example 1-2 (continued)
• Calculate totalSalary using the equation
totalSalary = baseSalary + bonus
+ additionalBonus
• Output totalSalary
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 31
Example 1-3 Homework
• Calculate taxi fare
• First 5 kilometers are 10 dirhams.
• Next 10 kilometers are 3 dirhams per
kilometer.
• Any additional kilometer will be 2 dirhams per
kilometer.
• Input Total Distance and calculate the fare.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 32
Example 1-3 (Algorithm)
• Input totalDistance
• if totalDistance is less than or equal to 5 then
− Fare = 10
• Otherwise, if totalDistance is greater than 5
and less than or equal to 15 then
− Fare = 10 + (totalDistance-5)*3
• Otherwise
− Fare = 10 + 30 + (totalDistance-15)*2
• Output Fare
C++ Programming: From Problem Analysis to Program Design, Fourth Edition 33
Introducing Variables
• Declare (introduce) a variable before use.
• Data Types:
− Integer: 10, 50…. (int) 10 + 15
− Decimal: 10.78….(float or double)
− Character: a, b, c….. (char)
− Boolean: yes, no….(bool)
− Words: ali,… (string)
• How to declare a variable:
• ??? x 10.78
34
Introducing Variables
• Declare (introduce) a variable before use.
• Data Types: 3 things about a variable
− Integer: 10, 50…. (int) 1. Type: float
2. Name: x
− Decimal: 10.78….(float or double) 3. Value: 10.78
− Character: a, b, c….. (char)
− Boolean: yes, no….(bool)
− Words: ali,… (string)
• How to declare a variable:
10.78
Type name = value; x
Ex: float x = 10.78; int z;
….
int y = 5; z = 90;
Assignment operator 35
Introducing Variables
• Declare (introduce) a variable before use.
• Example: 3 things about a variable
• How to declare a variable: 1. Type: float
2. Name: x
Type name = value; 3. Value: 10.78
How to get value from the user:
cin >> x; x
• Assignment operator: =
36