1 CSE Computer Programming “Using C-Language” Lectures 1
Mohamed Shalan CSE 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 Computer Programming 3 Logistics 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
Mohamed Shalan CSE Computer Programming 4 Logistics
Mohamed Shalan CSE Computer Programming 5 Question Why do we write programs?
Mohamed Shalan CSE 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 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 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 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 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 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 Computer Programming 12 The Problem-solving Process Problem specification Algorithm Program Executable (solution) Analysis Design Implementation Compilation
Mohamed Shalan CSE 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 Computer Programming 14 Algorithm –History Muhammad ibn Musa Al-Khwarizmi 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 Computer Programming 15 A sequence of instructions describing how to do a task Algorithm – Working Definition
Mohamed Shalan CSE 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 Computer Programming 17 From Algorithms to Programs Problem C Program Algorithm Algorithm: A sequence of instructions describing how to do a task (or process)
Mohamed Shalan CSE Computer Programming 18 Components of an Aalgorithm Variables and values Instructions Sequences Procedures Selections Repetitions Also required: Documentation
Mohamed Shalan CSE 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 Computer Programming 20 Variables This jar can contain 10 cookies 50 grams of sugar 3 slices of cake etc. ValuesVariable Are containers for values – places to store values Example:
Mohamed Shalan CSE Computer Programming 21 Restrictions on Variables Variables may be restricted to contain a specific type of value
Mohamed Shalan CSE 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 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 Computer Programming 24 Instructions -- Application Some instructions can only be applied to a specific type of values or variables Examples:
Mohamed Shalan CSE 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 Meal TM
Mohamed Shalan CSE 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 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 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 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 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 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 Computer Programming 31 Selection Example -- Car Repair if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil }
Mohamed Shalan CSE Computer Programming 32 Selection Example – Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Should be a true or false condition.
Mohamed Shalan CSE Computer Programming 33 Selection Example – Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Sequence if the condition is true.
Mohamed Shalan CSE Computer Programming 34 Selection Example – Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Sequence if the condition is false.
Mohamed Shalan CSE 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 Computer Programming 36 Repetition -- Example procedure Ask ( toy ) { Say("Hey mom; I want to buy a“, toy) ListenToReply ( ) start begging count at zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } }
Mohamed Shalan CSE Computer Programming 37 Repetition – Example (cont) procedure Ask ( toy ) { Say("Hey mom; I want to buy a“, toy) ListenToReply ( ) start begging count at zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } } Condition is tested before sequence
Mohamed Shalan CSE Computer Programming 38 Repetition – Example (cont) procedure Ask ( toy ) { Say("Hey mom; I want to buy a“, toy) ListenToReply ( ) start begging count at zero while (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 Computer Programming 39 Repetition – Example (cont) procedure Ask ( toy ) { Say("Hey mom; I want to buy a“, toy) ListenToReply ( ) start begging count at zero while (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 Computer Programming 40 Repetition – Example (cont) procedure Ask ( toy ) { Say("Hey mom; I want to buy a“, toy) ListenToReply ( ) start begging count at zero while (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 Computer Programming 41 Repetition – Example (cont) procedure Ask ( toy ) { Say("Hey mom; I want to buy a“, toy) ListenToReply ( ) start begging count at zero while (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 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 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 Computer Programming 44 High-Level Language Compilers and linkers translate a high level program into executable machine code. #include int main() { printf(“Hello World”); return 0; } Source code Executable code etc...
Mohamed Shalan CSE 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.