01 - Introduction
01 - Introduction
Engineers
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).
− Main Components
10 + 20
LOAD rate
MULT hour
STOR wages
#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;
}
https://www.onlinegdb.com/online_c++_compiler 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
Syntax
Error
Compiler
(Translate to Machine Language)
Execution
(Computer Follows
the Instructions)
Analysis
Algorithm
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
Design an Algorithm 2.
3.
Processing
Output
3. Output wages
3. Output wages
26
Example 1-2
• Find the Total Salary of a Salesperson
Overall idea:
• Output totalSalary
• 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
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
cin >> x; x
• Assignment operator: =
36