Programming Fundamental All Chapter
Programming Fundamental All Chapter
Main Reference Hanly and Koffman, Problem Solving and Program Design in C, Fifth Edition, Addison Wesley Reference Joyce Farrell Thomson, Programming Logic and Design, Fifth Edition
1
REV00
REV00
REV00
REV00
REV00
REV00
REV00
What is wrong with this logic for making a cake? Stir Add two eggs Bake at 350 degrees for 45 minutes Add three cups of flour
REV00
Logic errors, or semantic errors, are more difficult to locate than syntax errors Logic for multiplying a number by 2 (includes input, processing, and output statements)
Get input number. Compute calculated answer as inputNumber times 2. Print calculatedAnswer.
DDC1023 PROGRAMMING METHODOLOGY
8
REV00
REV00
REV00
11
REV00
12
REV00
13
REV00
14
REV00
15
REV00
REV00
17
REV00
18
REV00
2 Analysis 5 Testing Identify data objects Verify the program meets Goal to model properties requirements Determine Input / Output data System and Unit test Constraints on the problem 6 Maintaining 3 Design Maintain the system Decompose into smaller problems Top-down design (divide and conquer) Develop algorithm (Desk check)
DDC1023 PROGRAMMING METHODOLOGY
19
REV00
20
REV00
REV00
REV00
23
REV00
Database: collection of related files, called tables, that serve the information needs of the organization
24
REV00
REV00
REV00
stop
27
REV00
Processing symbol
Output symbol
28
REV00
29
REV00
REV00
31
REV00
REV00
33
REV00
REV00
35
REV00
REV00
37
REV00
REV00
39
REV00
40
REV00
41
REV00
REV00
Chapter 1: Summary
4 major computer operations: Input Processing Output Storage 6 programming phases: Understand the problem Plan the logic Code the program Translate the program to machine language Test the program Deploy the program
43
REV00
Chapter 1: Summary
Data hierarchy
Character Field Record File Database
REV00
Chapter 1: Summary
Flowchart: pictorial representation of program logic Variables: named memory locations that contain values Testing a value involves making a decision Assignment statements: store a value into a variable Assignment operator: the equal (=) sign in most languages 2 major data types: text numeric Procedural programming: focuses on actions performed on data Object-oriented programming: focuses on representing and manipulating objects
DDC1023 PROGRAMMING METHODOLOGY
45
REV00
46
REV00
47
REV00
48
REV00
49
REV00
Selection (decision)
Ask a question, take one of 2 actions Dual-alternative or single-alternative
Loop
Repeat actions based on answer to a question
50
REV00
Sequence structure
DDC1023 PROGRAMMING METHODOLOGY
51
REV00
Selection structure
DDC1023 PROGRAMMING METHODOLOGY
52
REV00
do theOtherProcess
53
REV00
REV00
55
REV00
Loop structure
DDC1023 PROGRAMMING METHODOLOGY
56
REV00
57
REV00
58
REV00
REV00
60
REV00
REV00
REV00
Flowchart and pseudocode for loop within selection within sequence within selection
DDC1023 PROGRAMMING METHODOLOGY
63
REV00
The 3 structures
DDC1023 PROGRAMMING METHODOLOGY
64
REV00
65
REV00
66
REV00
67
REV00
Example 1
DDC1023 PROGRAMMING METHODOLOGY
68
REV00
REV00
REV00
REV00
REV00
REV00
REV00
REV00
CASE Structure: Decisions with more than 2 alternatives Tests a variable against a series of values and takes action based on a match Nested if-then-else statements will do what a case structure does
76
REV00
REV00
REV00
REV00
REV00
REV00
REV00
REV00
Unstructured loop
DDC1023 PROGRAMMING METHODOLOGY
84
REV00
Sequence and structured loop that accomplish the same tasks as Figure 2-37
85
REV00
Chapter 2: Summary
Spaghetti code: snarled program logic 3 basic structures: sequence, selection, loop Combined by stacking and nesting Structured techniques promote clarity, professionalism, efficiency, and modularity
86
REV00
Chapter 2: Summary
case structure: questions with multiple alternatives while loop: a pretest loop asks the question first
REV00
88
REV00
REV00
90
REV00
91
REV00
92
REV00
93
REV00
REV00
REV00
REV00
REV00
98
REV00
99
REV00
100
REV00
REV00
102
REV00
REV00
REV00
105
REV00
106
REV00
Sample logic
REV00
REV00
109
REV00
REV00
Program that prints a bill using main program that calls nameAndAddress() method
DDC1023 PROGRAMMING METHODOLOGY
111
REV00
REV00
113
REV00
REV00
REV00
REV00
117
REV00
REV00
REV00
120
REV00
121
REV00
Chapter 3: Summary
Documentation: all supporting material for a program Output documentation: includes report designs File description: details data types and lengths of each field of data in the file User documentation: manuals and instructional materials, and operating instructions Modules: smaller, reasonable units of code that provide reusability Subroutines, procedures, functions, methods Modularization: Provides abstraction Allows multiple programmers to work on a problem Makes it easy to reuse work and identify structures
DDC1023 PROGRAMMING METHODOLOGY
122
REV00
Chapter 3: Summary
Modules can call other modules Flowchart symbol is a rectangle with a bar across the top Variable declarations define the name and type of the data to be stored Hierarchy chart illustrates modules relationships Common structure for procedural programs: Housekeeping tasks Main loop End-of-job tasks Hierarchy chart illustrates modules relationships
123
REV00
REV00
REV00
REV00
REV00
REV00
129
REV00
130
REV00
131
REV00
REV00
REV00
Relational comparisons
134
REV00
Requires that both of 2 tests evaluate to True Requires a nested decision (nested if) Using nested if statements Second selection structure is contained entirely within 1 side of 1st structure else clause paired with last if
DDC1023 PROGRAMMING METHODOLOGY
135
REV00
REV00
137
REV00
When you want to take action when 1 or the other of 2 conditions is true
Example: Salespeople get bonus when they have achieved one of two goals: Sell at least five items Sell at least $2,000 in merchandise itemsSold >= ITEMS_MIN? If true, assign $300 bonus
DDC1023 PROGRAMMING METHODOLOGY
138
REV00
REV00
140
REV00
141
REV00
142
REV00
REV00
144
REV00
REV00
146
REV00
Flowchart and pseudocode that determines base price for house based on model number using the case structure
DDC1023 PROGRAMMING METHODOLOGY
147
REV00
148
REV00
149
REV00
150
REV00
REV00
152
REV00
REV00
154
REV00
155
REV00
156
REV00
157
REV00
REV00
REV00
Definite loop: number of iterations predetermined Also called counted loop Counter: numeric variable used to count number of times an event occurs Loop control variable may be altered by user input Indefinite loop: loop iterates until some condition is true Number of iterations may vary
160
REV00
Indefinite loop: loop performed a different number of times each time the program executes 3 crucial steps: Starting value to control the loop must be provided Comparison must be made using the value that controls the loop Within the loop, value that controls the loop must be altered Loop control variable: any variable that determines whether the loop will continue
DDC1023 PROGRAMMING METHODOLOGY
161
REV00
REV00
REV00
REV00
165
REV00
REV00
167
REV00
REV00
REV00
170
REV00
171
REV00
REV00
REV00
REV00
REV00
REV00
177
REV00
REV00
REV00
Chapter 5: Summary
When using a loop, write one set of instructions that operates on multiple, separate data Three steps must occur in every loop: Initialize loop control variable Compare variable to some value Alter the variable that controls the loop Nested loops: loops within loops Nested loops maintain two individual loop control variables Alter each at the appropriate time
DDC1023 PROGRAMMING METHODOLOGY
180
REV00
Chapter 5: Summary
Use posttest loop when loop body must execute at least one time Control variable evaluated after loop body executes Characteristics of all structured loops: Loop-controlling question provides entry or exit from repeating structure Loop-controlling question provides the only entry or exit from repeating structure Accumulator: variable that gathers values Loops used to ensure user data is valid by reprompting the user
181
REV00
182
REV00
Main function
183
REV00
184
REV00
185
REV00
186
REV00
REV00
188
REV00
189
REV00
190
REV00
191
REV00
scanf("%lf", &miles);
192
REV00
193
REV00
Preprocessor directives: #include, #define Any used variable should be declared before the usage.
194
REV00
234
234 234 234
%4d
%5d %6d %1d
#234
##234 ###234 234
-234
-234 -234 -234
%4d
%5d %6d %2d
-234
#-234 ##-234 -234
195
REV00
-.006
-.006
%8.3f
%.3f
##-0.006 -.006
-0.006
%8.5f
-0.00600
-3.1416
-3.14159 %.4f
196
REV00
197
REV00
198
REV00
199
REV00
Syntax error occurs when the code violates grammar rules of C and is detected by the compiler.
200
REV00
201
REV00
202
REV00
Chapter 6: Summary
Every C program has preprocessor directives and a main function. The main function contains variable declarations and executable statements. Variable names must begin with a letter or an underscore and consist of letters, digits, and underscore symbols. A reserved word cannot be used as an identifier.
203
REV00
204
REV00
char A a z
* ?
205
REV00
206
REV00
207
REV00
208
REV00
209
REV00
z - (a + b / 2) + w * -y
210
REV00
211
REV00
For I/O library functions (printf, scanf) To use them, have to use #include <stdio.h> For Mathematical Functions (sqrt, pow, sin, cos) To use them, have to use #include <math.h>
DDC1023 PROGRAMMING METHODOLOGY
212
REV00
Example: Display the square root of 2 numbers provided as the input data (first and second) and the square root of their sum.
213
REV00
Chapter 7: Summary
Cs data types enable the compiler to determine how to store a particular value in memory and what operations can be performed on that value. Three standard data types are int, double, and char. The data type of each variable must be declared. Assignment statements are used to perform computations and store results in memory. Function calls are used to get data (scanf) and to display values stored in memory (printf).
214
REV00
215
REV00
The execution of the program is sequential Selection A control structure which chooses alternative to execute Repetition A control structure which repeats a group of statements.
We will focus on the selection control structure this week.
DDC1023 PROGRAMMING METHODOLOGY
216
REV00
Chapter 8: Conditions
A program may choose among alternative statements by testing the value of key variables. if (your_grade > 60) printf(you are passed!)
Condition is an expression that is either false (represented by 0) or true (represented by 1). your_grade > 60
Conditions may contain relational or equality operators.
217
REV00
Chapter 8: Conditions
Relational and Equality Operators
218
REV00
Chapter 8: Conditions
Sample Conditions
219
REV00
Chapter 8: Conditions
Logical Operators To form more complicated conditions by the three logical operators && (and) || (or) ! (not) Logical Expressions is an expression which uses one or more logical operators, e.g., salary < MIN_SALARY || dependents > 5 temperature > 90.0 && humidity > 0.9 !(0 <= n && n<= 100)
220
REV00
Chapter 8: Conditions
221
REV00
Chapter 8: Conditions
Operator Precedence An operators precedence determines its order of evaluation.
222
REV00
Chapter 8: Conditions
Evaluation Tree & Step-by-Step Evaluation
223
REV00
Chapter 8: Conditions
Short Circuit Evaluation Stopping evaluation of a logical expression as soon as its value can be determined. e.g. a && b must be false if a is 0.
224
REV00
Chapter 8: Conditions
Writing English Conditions in C
225
REV00
Chapter 8: Conditions
Comparing Characters
226
REV00
Chapter 8: Conditions
Complement a logical expression with the symbol ! just change its operator
a>b
227
REV00
Ex-2.
if (x != 0.0) product = product * x;
228
REV00
229
REV00
230
REV00
Dangling Else
if (x > 0) if (y > 0) a = a + 1; else b = b + 1;
231
REV00
232
REV00
233
REV00
234
REV00
235
REV00
When the selection is based on the value of a single variable or of a simple expression (called the controlling expression).
236
REV00
237
REV00
Chapter 8: Summary
Use if statement or switch statement to code decision steps in C. Using the relational operators (<, <=, >, >=) and equality operators (==, !=) to compare variables and constants Using the logical operators (&& (and), || (or), !(not)) to form more complex conditions Nested if statements are common in C and are used to represent decisions with multiple alternatives. Programmers use indentation and the multiple-alternative decision form when applicable to enhance readability of nested if statements. The switch statement implements decisions with more than 2 alternatives, where the alternative selected depends on the value of a variable or expression (the controlling expression). The controlling expression can be type int or char, but not type double.
DDC1023 PROGRAMMING METHODOLOGY
238
REV00
239
REV00
240
REV00
241
REV00
242
REV00
REV00
244
REV00
245
REV00
246
REV00
247
REV00
248
REV00
249
REV00
The increment (i.e., ++) or decrement (i.e., --) operators are the frequently used operators which take only one operand. for(int i=0; i<100; i++) {} for(int i=100; i>0; i--) {}
Side effect: the value of its operand is incremented/decremented by one.
250
REV00
251
REV00
252
REV00
253
REV00
254
REV00
255
REV00
256
REV00
REV00
258
REV00
259
REV00
260
REV00
261
REV00
262
REV00
263
REV00
264
REV00
Chapter 9: Summary
Repetition and Loop Statements Counter-Controller Loop Sentinel-Controller Loop Endfile-Controller Loop Input Validation Loop General Conditional Loop while, for, and do-while Compound Assignment Operators
265