1 cse 324 - computer programming “using c-language” lectures 1
TRANSCRIPT
1
CSE 324 - Computer Programming“Using C-Language”
Lectures 1
Mohamed Shalan CSE 324 - Computer
Programming 2
Logistics
Grading Policy Final : 75% Mid-Term : 10% Homework : 5% Project : 10%
General Rules Attend 75% of tutorial sessions Late submission -> No Credit!
Mohamed Shalan CSE 324 - Computer
Programming 3
Logistics
Email: [email protected] Don’t send anything unrelated to the class Don’t insert me into your address book Make the subject line “CSE 324” Don’t use any language other than English
language Make sure that your name is included in the email
Mohamed Shalan CSE 324 - Computer
Programming 4
Logistics
Mohamed Shalan CSE 324 - Computer
Programming 5
Question
Why do we write programs?
Mohamed Shalan CSE 324 - Computer
Programming 6
Hardware vs. Software
A computer is made up of hardware and software
Hardware Software• CPU
-e.g. Intel Core 2 Duo• Input/Output devices
- keyboard- monitor- mouse
• main memory- e.g. 2GB RAM
• secondary memory- e.g. 120 GB hard drive
• operating systems- Windows XP- Mac OS X- Unix / Linux
• applications- games- Microsoft Word- Internet Explorer
Mohamed Shalan CSE 324 - Computer
Programming 7
Programs
A program is a set of instructions for a computer to follow
Following the instructions is called running or executing the program.
Mohamed Shalan CSE 324 - Computer
Programming 8
Running a Program
Sometimes the computer and the program are considered to be one unit. Programmers typically find this view to be more
convenient.
Mohamed Shalan CSE 324 - Computer
Programming 9
Low-level language(Machine language
/assembly language)
Programming Languages
High-level Programming
Languages (e.g. Java)human readable
computer readable
Compiler
translated by
Mohamed Shalan CSE 324 - Computer
Programming 10
Low-level language(Machine language
/assembly language)
Programming Languages
High-level Programming
Languages (e.g. Java)human readable
computer readable
Compiler
translated by
Source code
Object code
Mohamed Shalan CSE 324 - Computer
Programming 11
sterilize(saw,alcohol);raise_hammer();lower hammer(fast);start(saw);/* etc. etc. */
Patient has elevated pressure in anterior parietal lobe
The Problem-solving Process
"Doctor, my head hurts"
Problem
specification
Algorithm
Program
Executable (solution)
Design
Implementation
Compilation
Analysis
Mohamed Shalan CSE 324 - Computer
Programming 12
The Problem-solving Process
Problem specification
Algorithm
Program
Executable (solution)
Analysis
Design
Implementation
Compilation
Mohamed Shalan CSE 324 - Computer
Programming 13
A sequence of instructions specifying the steps required to accomplish some task
Named after:Muhammad ibn Musa al-Khwarizmi of Khowarezm (now Khiva in Uzbekistan)
Algorithm
Mohamed Shalan CSE 324 - Computer
Programming 14
Algorithm –History
Muhammad ibn Musa Al-Khwarizmihttp://www-groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al-Khwarizmi.html
Book on arithmetic: Hindu numeration, decimal numbers, use of zero,
method for finding square root Latin translation (c.1120 CE): “Algoritmi de numero
Indorum” Book on algebra
Hisab al-jabr w’al-muqabala
Mohamed Shalan CSE 324 - Computer
Programming 15
A sequence of instructions describing how to do a task
Algorithm – Working Definition
Mohamed Shalan CSE 324 - Computer
Programming 16
Algorithm -- Examples
A cooking recipe Assembly instructions for a model The rules of how to play a game VCR instructions A car repair manual
Mohamed Shalan CSE 324 - Computer
Programming 17
From Algorithms to Programs
Problem
C ProgramC Program
AlgorithmAlgorithm: A sequence of instructions describing how to do a task (or process)
Mohamed Shalan CSE 324 - Computer
Programming 18
Components of an Aalgorithm Variables and values Instructions
Sequences Procedures Selections Repetitions
Also required: Documentation
Mohamed Shalan CSE 324 - Computer
Programming 19
Values
Represent quantities, amounts or measurements
May be numerical or alphabetical (or other things)
Often have a unit related to their purpose Example:
Recipe ingredients
Mohamed Shalan CSE 324 - Computer
Programming 20
Variables
This jarcan contain
10 cookies
50 grams of sugar
3 slices of cake
etc.
Values
Variable
• Are containers for values – places to store values
• Example:
Mohamed Shalan CSE 324 - Computer
Programming 21
Restrictions on Variables
Variables may be restricted to contain a specific type of value
Mohamed Shalan CSE 324 - Computer
Programming 22
Components of an Algorithm
Values and Variables Instruction (a.k.a. primitive)
Sequence (of instructions) Procedure (involving instructions) Selection (between instructions) Repetition (of instructions)
Documentation (beside instructions)
Mohamed Shalan CSE 324 - Computer
Programming 23
Instructions (Primitives)
Some action that is simple unambiguous that the system knows about... ...and should be able to actually do
Mohamed Shalan CSE 324 - Computer
Programming 24
Instructions -- Application
Some instructions can only be applied to a specific type of values or variables
Examples:
Mohamed Shalan CSE 324 - Computer
Programming 25
Sequence
A series of instructions ...to be carried out one after the other... ...without hesitation or question Example:
How to cook a Gourmet MealTM
Mohamed Shalan CSE 324 - Computer
Programming 26
Sequence -- Example
1. Open freezer door 2. Take out Gourmet Meal™ 3. Close freezer door 4. Open microwave door 5. Put Gourmet Meal™ on carousel 6. Shut microwave door 7. Set microwave on high for 5 minutes 8. Start microwave 9. Wait 5 minutes 10. Open microwave door 11. Remove Gourmet Meal™ 12. Close microwave door
Mohamed Shalan CSE 324 - Computer
Programming 27
Procedure
A named sequence of instructions So that you can
Refer to it collectively (by name) ...instead of individually (by each instruction in the
sequence) Example:
Drive_To_Uni
Mohamed Shalan CSE 324 - Computer
Programming 28
Procedure -- Example procedure Drive_To_Uni { 1. find car keys 2. disable car alarm 3. open car door 4. get in car 5. shut car door 6. put keys in ignition 7. start car 8. back car out of
driveway 9. drive to end of street 10. turn right 11. drive to end of street 12. turn left ...etc...etc...etc
...etc...etc...etc...
52. find parking space
53. pull into parking space
54. turn off engine
55. remove keys from ignition
56. open car door
57. get out
58. shut car door
59. lock car door
60. enable alarm
}
Mohamed Shalan CSE 324 - Computer
Programming 29
Procedure – Example (cont)
procedure Do_Wednesday{ Wake_up Have_Shower Eat_Breakfast Drive_To_Uni Sit_1301_Lecture ...etc...etc...etc... Drive_From_Uni ...etc...etc...etc...}
procedure Do_Week
{
Do_Monday
Do_Tuesday
Do_Wednesday
Do_Thursday
...etc...etc...etc...
}
Mohamed Shalan CSE 324 - Computer
Programming 30
Selection
An instruction that decides which of two possible sequences is executed
The decision is based on a single true/false condition
Examples: Car repair
Mohamed Shalan CSE 324 - Computer
Programming 31
Selection Example -- Car Repair
if (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
Mohamed Shalan CSE 324 - Computer
Programming 32
Selection Example – Car Repair (cont)if (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
Should be a true or false condition.
Mohamed Shalan CSE 324 - Computer
Programming 33
Selection Example – Car Repair (cont)if (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
Sequence if the condition is true.
Mohamed Shalan CSE 324 - Computer
Programming 34
Selection Example – Car Repair (cont)
if (motor turns) then {
CheckFuelCheckSparkPlugsCheckCarburettor
}else {
CheckDistributorCheckIgnitionCoil
}
Sequence if the condition is false.
Mohamed Shalan CSE 324 - Computer
Programming 35
Repetition
Repeat an instruction... ...while (or maybe until) some true or false
condition occurs Test the condition each time before
repeating the instruction Also known as iteration or loop Example:
Algorithm used by a kid to convince his mom to buy a toy
Mohamed Shalan CSE 324 - Computer
Programming 36
Repetition -- Example
procedure Ask ( toy )
{
Say("Hey mom; I want to buy a“, toy)
ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!")
add 1 to begging count
ListenToReply ( )}
}
Mohamed Shalan CSE 324 - Computer
Programming 37
Repetition – Example (cont)
procedure Ask ( toy ){
Say("Hey mom; I want to buy a“, toy)ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
}
Condition is tested before sequence
Mohamed Shalan CSE 324 - Computer
Programming 38
Repetition – Example (cont)
procedure Ask ( toy ){
Say("Hey mom; I want to buy a“, toy)ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
}
Sequence may not get executed at all
Mohamed Shalan CSE 324 - Computer
Programming 39
Repetition – Example (cont)
procedure Ask ( toy ){
Say("Hey mom; I want to buy a“, toy)ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
}
Ensure initial values of variables used in the conditions are set correctly
Mohamed Shalan CSE 324 - Computer
Programming 40
Repetition – Example (cont)
procedure Ask ( toy ){
Say("Hey mom; I want to buy a“, toy)ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
}
Ensure the variables used in the conditions are updated in each iteration
Mohamed Shalan CSE 324 - Computer
Programming 41
Repetition – Example (cont)
procedure Ask ( toy ){
Say("Hey mom; I want to buy a“, toy)ListenToReply ( )start begging count at zerowhile (reply is "No" and begging count < 100){ Say("Oh please!") add 1 to begging count ListenToReply ( )}
} Infinite loop
• What if we don’t increment the begging count?
Mohamed Shalan CSE 324 - Computer
Programming 42
Documentation
Records what the algorithm does Describes how it does it Explains the purpose of each component of
the algorithm Notes restrictions or expectations
Mohamed Shalan CSE 324 - Computer
Programming 43
From Algorithms to Programs
Both are sets of instructions on how to do a task Algorithm:
talking to humans, easy to understand in plain (English) language
Program: talking to computer (compiler) can be regarded as a “formal expression” of an algorithm
Mohamed Shalan CSE 324 - Computer
Programming 44
High-Level Language
Compilers and linkers translate a high level program into executable machine code.
#include <stdio.h>
int main(){ printf(“Hello World”);
return 0;}
Source code Executable code
10100110 0111011000100110 0000000011111010 1111101001001110 1010011011100110 1001011011001110 0010111010100110 0100111011111010 0110011001001110 10000110
etc...
Mohamed Shalan CSE 324 - Computer
Programming 45
Developing a C Program
Use Text Editor to edit the “source file” (.c).
Editor + Compiler + Linker + Debugger = IDE
Debugging: Finding logical error (bugs) and eliminate them.