Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... ·...
Transcript of Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... ·...
![Page 2: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/2.jpg)
Course Logistics – Staff
• Teachers
– Prof. Mohamed Roushdy (Dean)
– Dr. Salma Hamdy
• Contact: [email protected]
• Office: FCIS, 3rd floor, “glass room”.
• Office hours: Tuesday 11:00-13:00
Wednesday 11:00-13:00
• Course page http://piazza.com/faculty_of_computer_and_information_sciences/spring2013/csw150
Access Code: CSW150 2
![Page 3: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/3.jpg)
Course Logistics – General
• Class meets: Tuesday 8:00-11:00
Wednesday 8:00-11:00
• Lab meets: check your schedule! :-p
• (3-hour lecture + 2-hour lab) a week.
• Assessment (125 points) Final Term Examination 90
Oral Discussion 10
Practical Examination 15
Mid Term + Assignments 10
YOUR JOB read + think + code =
HW + lab tasks + exams
3
![Page 4: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/4.jpg)
Course Logistics – Textbook
• C++ How to Program, 8th edition, Deitel & Deitel, Prentice Hall- Pearson Education International, 2012.
References
• Problem Solving with C++, 8th edition, Walter Savitch, Addison Wesley-Pearson Education International, 2012.
• Object Oriented Programming in C++, 4th edition, Robert Lafore, Pearson Education, 2002.
• Internet: ENDLESS list. 4
![Page 5: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/5.jpg)
Course Logistics – Ethics
Lecture
1. Time management
2. Attendance
3. Manners
4. Mind + hands (if possible)
5. Your rights.
6. My rights.
7. Questions
Lab
1. Time management
2. Attend ONLY in your class.
3. Manners
4. Mind + hands (BOTH!!!)
5. Your rights.
6. TA’s rights.
7. Questions
Cheating and copied assignments, programs, projects, or code segments, will not be tolerated.
5
![Page 6: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/6.jpg)
Course Logistics – Outline
1. Introduction
2. Basics
3. Control Structures
4. Arrays and Structures
5. Pointers and Dynamic Arrays
6. Functions
7. Parameters, Overloading, and Reference
8. Recursion
9. Strings
10.Streams and File I/O
6
![Page 7: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/7.jpg)
Course Logistics – Outcomes
During and upon the completion of this course, you will:
- Have a basic understanding of structured programming concepts and how computers understand your code.
- Know how to design, analyze, and code simple and sophisticated programs using C++ language that execute efficiently.
- Make use of simple data structures, linear algebra, and other knowledge to solve real problems.
- Require substantial programming and good software engineering practice.
- Develop and deploy a C++ application of your own design.
- Meet deadlines and be a team player.
- Be able to present your work and describe your solutions.
7
![Page 8: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/8.jpg)
Announcement
ONLY next week’s lectures isA will be switched with the electronics lectures.
Programming:
Sunday Feb 17th, 8:00-11:00 Group 2
Monday Feb 18th, 8:00-11:00 Group 1
Electronics:
Tuesday Feb 19th, 8:00-11:00 Group 1
Wednesday Feb 20th, 8:00-11:00 Group 2 8
![Page 9: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/9.jpg)
Review
9
![Page 10: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/10.jpg)
Contents
1. Problem Solving
2. Computer Programs
3. C++ Programming (Basics)
10
![Page 11: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/11.jpg)
Why Programming?
1. ..
2. ..
3. ..
…
11
![Page 12: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/12.jpg)
1. Problem Solving
Steps of Solving a Problem:
1. Define the problem
– Understand input and output
– Change the problem
– Simplify the problem
12
![Page 13: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/13.jpg)
1. Problem Solving – (cont.)
Steps of Solving a Problem:
1. Define the problem
2. Design a solution
– How to reach output from input
– Plan a solution
– List steps of solution
Analysis
Algorithm
13
![Page 14: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/14.jpg)
1. Problem Solving – (cont.)
• An algorithm is a procedure for solving a problem in terms of the actions to be executed and the order in which those actions are to be executed.
• An algorithm is merely the sequence of steps taken to solve a problem.
• The steps are normally "sequence," "selection, " or "iteration," statements.
14
![Page 15: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/15.jpg)
1. Problem Solving – (cont.)
• A pseudo code is a kind of structured English for describing algorithms. It allows the designer to focus on the logic of the algorithm without being distracted by details of language syntax.
• The pseudocode needs to be complete. It describes the entire logic of the algorithm so that implementation becomes a task of translating line by line into source code.
15
![Page 16: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/16.jpg)
1. Problem Solving – (cont.)
• Pseudo code common format
16
NOT a language syntax
![Page 17: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/17.jpg)
1. Problem Solving – (cont.)
• Pseudo code common format
17
NOT a language syntax
![Page 18: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/18.jpg)
1. Problem Solving – (cont.)
Example 1: Student pass?
18
![Page 19: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/19.jpg)
1. Problem Solving – (cont.)
Example 1: Student pass?
1. GET Grade
2. If Grade is greater than or equal to 60 THEN
3. Print "passed"
4. ELSE
5. Print "failed"
19
![Page 20: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/20.jpg)
1. Problem Solving – (cont.)
Example 2: Average of ten grades
20
![Page 21: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/21.jpg)
1. Problem Solving – (cont.)
Example 2: Average of ten grades
1. SET Total to zero
2. SET Grade Counter to one
3. WHILE Grade Counter is less than or equal to ten THEN
4. GET the Next Grade
5. ADD the Next Grade to the Total
6. SET the Class Average to the Total divided by ten 21
![Page 22: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/22.jpg)
1. Problem Solving – (cont.)
• A flowchart is diagram of the steps you will use to reach your goal (steps of the algorithm).
• Various standard shapes.
22
![Page 23: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/23.jpg)
1. Problem Solving – (cont.)
Example 1: Student pass
Example 2: Average of ten grades
23
![Page 24: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/24.jpg)
1. Problem Solving – (cont.)
Steps of Solving a Problem:
1. Define the problem.
2. Design a solution.
3. Test on sample data trace + debug
Logical Errors
24
![Page 25: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/25.jpg)
1. Problem Solving – (cont.)
Steps of Solving a Problem:
1. Define the problem.
2. Design a solution.
3. Test on sample data trace + debug
----------------
4. Implement the “algorithm” Programming Language
25
![Page 26: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/26.jpg)
1. Problem Solving – (cont.)
Steps of Solving a Problem:
1. Define the problem.
2. Design a solution.
3. Test on sample data trace + debug
----------------
4. Implement the “algorithm”
5. Test on real data run Programming Language
26
![Page 27: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/27.jpg)
1. Problem Solving – (cont.)
Steps of Solving a Problem:
1. Define the problem.
2. Design a solution.
3. Test on sample data trace + debug
----------------
4. Implement the “algorithm”
5. Test on real data run Programming Language
27
WHY?
![Page 28: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/28.jpg)
1. Problem Solving – (cont.)
Example 1
Calculate the area of a circle given its radius.
Example 2
Convert degrees from Fahrenheit to Celsius according to 𝑪 = 𝟓(𝑭 − 𝟐𝟑)/𝟗
28
![Page 29: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/29.jpg)
29
1. Problem Solving – (cont.)
![Page 30: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/30.jpg)
1. Problem Solving – (cont.)
• Can we hire someone to do the job?
An expert maybe…
• Or even better, can we automate the job?
30
![Page 31: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/31.jpg)
2. Computers Programs
• Computers = devices (HW) that can perform computations and make logical decisions phenomenally faster than human beings can.
• They process data under the control of sets of instructions called computer programs (SW).
31
![Page 32: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/32.jpg)
2. Computers Programs – (cont.)
• Computer Organization
Input unit
Output unit
Memory unit
ALU
CPU
Secondary Storage
32
![Page 33: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/33.jpg)
2.1 Programming Languages
• Talking to your Computer
1- Machine Language
- Directly understandable by computer. - String of numbers (1s and 0s) - Machine dependant. - Too tedious for programmer. - Too many instructions.
33
![Page 34: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/34.jpg)
2.1 Programming Languages – (cont.)
• Talking to your Computer
1- Machine Language
1- Assembly Language
- English-like abbreviations. - Requires assembler. - Still too many instructions.
34
![Page 35: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/35.jpg)
2.1 Programming Languages – (cont.)
• Talking to your Computer
1- Machine Language
1- Assembly Language
3- High Level Language
35
![Page 36: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/36.jpg)
2.1 Programming Languages – (cont.)
• Talking to your Computer
1- Machine Language
1- Assembly Language
3- High Level Language C++, C, Microsoft’s .NET languages (e.g., Visual Basic, Visual C++ and Visual C#) and Java
36
![Page 37: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/37.jpg)
2. Computers Programs – (cont.)
Programming Techniques
• Unstructured Programming
• Procedural Programming
• Modular Programming
• Object Oriented Programming
• Logic Programming
37
![Page 38: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/38.jpg)
2.2 Programming Techniques – (cont.)
Unstructured Programming
• One main program.
• A sequence of commands or statements, which modify data that is global throughout the whole program.
• Disadvantages.
38
![Page 39: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/39.jpg)
2.2 Programming Techniques – (cont.)
Procedural Programming
• A single program, which is divided into small pieces called procedures.
• Combine returning sequences of statements into one single place.
• A procedure call is used to invoke the procedure.
• Use a procedure in another program?
39
![Page 40: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/40.jpg)
2.2 Programming Techniques – (cont.)
Modular (Structured) Programming
• Procedures of a common functionality are grouped together into separate modules.
• Program is now divided into several smaller parts which interact through procedure calls.
• Each module can have its own data. This allows each module to manage an internal state.
• One state per module. 40
![Page 41: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/41.jpg)
Courtesy of Dr. Mohamed Samy
Spaghetti code
41
![Page 42: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/42.jpg)
2.2 Programming Techniques – (cont.)
Object Oriented Programming (OOP)
• Treat data and the procedures that act upon the data as a single "object"--a self-contained entity with an identity and certain characteristics of its own.
– Encapsulation
– Data Hiding
– Inheritance
– Polymorphism 42
![Page 43: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/43.jpg)
2.2 Programming Techniques – (cont.)
Logical Programming
• Focus on telling the system what to do, rather than how to do it.
• Describe problems in a declarative manner (versus procedural).
• Clues: Facts and rules.
• Solve problems by asking questions.
• Deductive reasoning and Predicate logic.
43
![Page 44: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/44.jpg)
• C++ Development Environment
• C++ programs consist of pieces called classes and functions.
• Even if you don’t follow the OOP approach, you will still take advantage of the rich collections of classes and functions in the C++ Standard Library.
2. Computers Programs – (cont.)
44
![Page 45: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/45.jpg)
• C++ Development Environment
1- Creating Program
- Source code
- Popular IDEs: Microsoft® Visual Studio 2010 Express Edition, Dev C++, NetBeans, Eclipse.
2- Preprocessing
- Automatically
- Preprocessor directives
2. Computers Programs – (cont.)
45
![Page 46: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/46.jpg)
• C++ Development Environment
3- Compiling
- Object code
4- Linking
- Object code to needed parts
2. Computers Programs – (cont.)
46
![Page 47: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/47.jpg)
• C++ Development Environment
5- Loading
- With other needed libraries.
6- Executing
- One instruction at a time.
2. Computers Programs – (cont.)
47
![Page 48: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/48.jpg)
3. C++ Programming
48
![Page 49: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/49.jpg)
3. C++ Programming – (cont.)
49
Libraries and namespaces
![Page 50: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/50.jpg)
3. C++ Programming – (cont.)
50
Libraries and namespaces
functions
![Page 51: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/51.jpg)
3. C++ Programming – (cont.)
51
Libraries and namespaces
functions
Input/output
![Page 52: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/52.jpg)
3. C++ Programming – (cont.)
52
Libraries and namespaces
functions
Variables
Input/output
![Page 53: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/53.jpg)
3. C++ Programming – (cont.)
53
Libraries and namespaces
functions
Variables
Input/output
Control Structure
![Page 54: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/54.jpg)
• C++ Identifiers
– Keywords/reserved words vs. Identifiers
– Case-sensitivity and validity of identifiers
– Meaningful names!
• Variables
– A memory location to store data for a program
– Must declare all data before use in program
VARIABLE NAMING RULES
54
Variables - Declaration
![Page 55: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/55.jpg)
55
Variables – Data Types
![Page 56: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/56.jpg)
56
Variables – Data Types
![Page 57: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/57.jpg)
• Initializing data in declaration statement – Results "undefined" if you don’t!
• int myValue = 0;
• Assigning data during execution – Lvalues (left-side) & Rvalues (right-side)
• Lvalues must be variables
• Rvalues can be any expression
• Example: distance = rate * time; Lvalue: "distance" Rvalue: "rate * time"
57
Variables - Assignment
![Page 58: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/58.jpg)
• C++ offers some shorthand notations in
assignment statements.
• Incrementing a variable: var = var+1;
Shorthand increment operator:
var++; Or ++var;
• Decrementing a variable: var= var-1;
Shorthand decrement operator:
var--; Or --var; 58
Variables - Assignment
![Page 59: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/59.jpg)
Assigning Data: Shorthand Notations
59
Variables - Assignment
![Page 60: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/60.jpg)
Variables - Assignment
Are we allowed to declare a variable of some type and assign to it a value of a different type?
• Yes!
• And No!
60
![Page 61: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/61.jpg)
• Compatibility of Data Assignments
– Type mismatches • General Rule: Cannot place value of one type into variable of
another type
– intVar = 2.99; // 2 is assigned to intVar! • Only integer part "fits", so that’s all that goes
• Called "implicit" or "automatic type conversion"
61
Variables - Assignment
![Page 62: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/62.jpg)
• Two types
– Implicit—also called "Automatic" • Done FOR you, automatically
17 / 5.5 This expression causes an "implicit type cast" to take place, casting the 17 17.0
– Explicit type conversion • Programmer specifies conversion with cast operator
(double)17 / 5.5 Same expression as above, using explicit cast (double)myInt / myDouble More typical use; cast operator on variable
62
Variables – Type Casting
![Page 63: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/63.jpg)
• Literals – Examples:
• 2 // Literal constant int
• 5.75 // Literal constant double
• "Z" // Literal constant char
• "Hello World" // Literal constant string
• Considered constants: Cannot change values during execution
• Called "literals" because you "literally typed" them in your program!
63
Literals
![Page 64: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/64.jpg)
• Naming your constants – Literal constants are "OK", but provide
little meaning • e.g., seeing 24 in a pgm, tells nothing about
what it represents
• Use named constants instead – Meaningful name to represent data
const int NUMBER_OF_STUDENTS = 24; • Called a "declared constant" or "named constant"
• Now use it’s name wherever needed in program
• Added benefit: changes to value result in one fix
64
Constants
![Page 65: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/65.jpg)
65
![Page 66: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/66.jpg)
• Standard Arithmetic Operators
– Precedence rules – standard rules
66
Arithmetic Operators
![Page 67: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/67.jpg)
• Standard Arithmetic Operators
– Precedence rules – standard rules
67
Arithmetic Operators
![Page 68: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/68.jpg)
Relational Operators
Comparing Expressions
68
![Page 69: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/69.jpg)
Boolean Expressions
• Recall that declaring a variable allocates memory for that variable’s specific type.
• Arithmetic types include int and float.
• Symbolic types include char and string.
• Another type is the Boolean (only one byte) that is either true or false).
69
![Page 70: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/70.jpg)
Data Types:
true, false are predefined library constants
70
Boolean Expressions
![Page 71: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/71.jpg)
Boolean Expressions
Be careful!
• In C++, 0 is false BUT any value other than 0 is considered true.
• For example:
int x= 0; // Boolean false z-- >=3
int y = 1; // Boolean true 6==x+7
int z = 3; // Boolean true sum==x+y
sum= x+y; //Boolean true
y--; // Boolean false 71
![Page 72: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/72.jpg)
Logical Operators
Evaluating Boolean Expressions
• Logical Operators – Logical AND (&&)
– Logical OR (||)
72
![Page 73: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/73.jpg)
Operators
Operators Precedence
73
![Page 74: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/74.jpg)
Display 2.3 Precedence of Operators (1 of 4)
2-74
![Page 75: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/75.jpg)
Display 2.3 Precedence of Operators (2 of 4)
2-75
![Page 76: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/76.jpg)
Display 2.3 Precedence of Operators (3 of 4)
2-76
![Page 77: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/77.jpg)
Display 2.3 Precedence of Operators (4 of 4)
2-77
![Page 78: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/78.jpg)
• I/O objects cin, cout, cerr
• Defined in the C++ library called <iostream>
• Must have these lines (called pre-processor directives) near start of file:
– #include <iostream> using namespace std;
– Tells C++ to use appropriate library so we can use the I/O objects cin, cout, …
78
Console Input/output
![Page 79: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/79.jpg)
• cin for input, cout for output
• Differences: – ">>" (extraction operator) points opposite
• Think of it as "pointing toward where the data goes"
– Object name "cin" used instead of "cout"
– No literals allowed for cin • Must input "to a variable"
• cin >> num; – Waits on-screen for keyboard entry
– Value entered at keyboard is "assigned" to num
79
Console Input/output
![Page 80: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/80.jpg)
• What can be outputted? – Any data can be outputted to display screen
• Variables
• Constants
• Literals
• Expressions (which can include all of above)
– cout << numberOfGames << " games played."; 2 values are outputted: "value" of variable numberOfGames, literal string " games played."
• Cascading: multiple values in one cout
80
Console Output
![Page 81: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/81.jpg)
• New lines in output – Recall: "\n" is escape sequence for the char "newline"
• A second method: object endl
• Examples: cout << "Hello World\n";
• Sends string "Hello World" to display, & escape sequence "\n", skipping to next line
cout << "Hello World" << endl; • Same result as above
81
Console Output – Separating Lines
![Page 82: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/82.jpg)
1-82
• Formatting numeric values for output
– Values may not display as you’d expect! cout << "The price is $" << price << endl;
• If price (declared double) has value 78.5, you might get: – The price is $78.500000 or:
– The price is $78.5
• We must explicitly tell C++ how to output numbers in our programs!
Console Output – Formatting Numbers
![Page 83: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/83.jpg)
1-83
• "Magic Formula" to force decimal sizes: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2);
• These statements force all future cout’ed values: – To have exactly two digits after the decimal place
– Example: cout << "The price is $" << price << endl; • Now results in the following:
The price is $78.50
• Can modify precision "as you go" as well!
Console Output – Formatting Numbers
![Page 84: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/84.jpg)
• "Extend" character set
• Backslash, \ preceding a character
– Instructs compiler: a special "escape character" is coming
– Following character treated as "escape sequence char"
84
Console Output – Escape Sequences
![Page 85: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/85.jpg)
Display 1.3 Some Escape Sequences (1 of 2)
85
![Page 86: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/86.jpg)
Display 1.3 Some Escape Sequences (2 of 2)
86
![Page 87: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/87.jpg)
• C++ Standard Libraries
• #include <Library_Name> – Directive to "add" contents of library file to
your program
– Called "preprocessor directive" • Executes before compiler, and simply "copies"
library file into your program file
• C++ has many libraries – Input/output, math, strings, etc.
87
Libraries
![Page 88: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/88.jpg)
• Namespaces defined: – Collection of name definitions
• For now: interested in namespace "std" – Has all standard library definitions we need
• Examples: #include <iostream> using namespace std;
• Includes entire standard library of name definitions
• #include <iostream> using std::cin; using std::cout;
• Can specify just the objects we want
88
Namespaces
![Page 89: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/89.jpg)
• Make programs easy to read and modify
• Comments, two methods: – // Two slashes indicate entire line is to be ignored
– /*Delimiters indicates everything between is ignored*/
– Both methods commonly used
• Indentation (arranging and spacing your lines of code)
• Identifier naming – ALL_CAPS for constants
– lowerToUpper for variables
– Most important: MEANINGFUL NAMES!
89
Adding Style to your Program
Believe it or not, I reward these things!
![Page 90: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/90.jpg)
3. C++ Programming – (cont.)
90
Libraries and namespaces
functions
Variables
Input/output
Control Structure
![Page 91: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/91.jpg)
91
4. Examples
![Page 92: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/92.jpg)
4. Examples – (cont.)
• Take a number from user and display
• Write a program to take two numbers and divide them putting the integer and remainder results in separate variables.
Number Squared Predecessor Successor
Equivalent Character
3 9 2 4 ??
92
![Page 93: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/93.jpg)
4. Examples – (cont.)
93
![Page 94: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/94.jpg)
94
4. Examples – (cont.)
![Page 95: Fundamentals of Structured Programminghazemzomzom.weebly.com/uploads/1/7/5/3/17535985/sp... · Fundamentals of Structured Programming ... - Have a basic understanding of structured](https://reader031.fdocuments.in/reader031/viewer/2022040613/5f08f0f97e708231d424777b/html5/thumbnails/95.jpg)
• Textbook chapters 1, 2.
• Problem Solving with C++ chapters 1, 2.
• More on pseudo code: http://users.csc.calpoly.edu/~jdalbey/SWE/pdl_std.html
5. References
95