Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSE 324 - Computer Programming “Using C-Language” Lectures 1.

Similar presentations


Presentation on theme: "1 CSE 324 - Computer Programming “Using C-Language” Lectures 1."— Presentation transcript:

1 1 CSE 324 - Computer Programming “Using C-Language” Lectures 1

2 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!

3 Mohamed Shalan CSE 324 - Computer Programming 3 Logistics Email: shalan.mohamed@gmail.comshalan.mohamed@gmail.com  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

4 Mohamed Shalan CSE 324 - Computer Programming 4 Logistics

5 Mohamed Shalan CSE 324 - Computer Programming 5 Question Why do we write programs?

6 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

7 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.

8 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.

9 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

10 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

11 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

12 Mohamed Shalan CSE 324 - Computer Programming 12 The Problem-solving Process Problem specification Algorithm Program Executable (solution) Analysis Design Implementation Compilation

13 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

14 Mohamed Shalan CSE 324 - Computer Programming 14 Algorithm –History Muhammad ibn Musa Al-Khwarizmi http://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

15 Mohamed Shalan CSE 324 - Computer Programming 15 A sequence of instructions describing how to do a task Algorithm – Working Definition

16 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

17 Mohamed Shalan CSE 324 - Computer Programming 17 From Algorithms to Programs Problem C Program Algorithm Algorithm: A sequence of instructions describing how to do a task (or process)

18 Mohamed Shalan CSE 324 - Computer Programming 18 Components of an Aalgorithm Variables and values Instructions  Sequences  Procedures  Selections  Repetitions Also required: Documentation

19 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

20 Mohamed Shalan CSE 324 - 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:

21 Mohamed Shalan CSE 324 - Computer Programming 21 Restrictions on Variables Variables may be restricted to contain a specific type of value

22 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)

23 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

24 Mohamed Shalan CSE 324 - Computer Programming 24 Instructions -- Application Some instructions can only be applied to a specific type of values or variables Examples:

25 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 Meal TM

26 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

27 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

28 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 }

29 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... }

30 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

31 Mohamed Shalan CSE 324 - Computer Programming 31 Selection Example -- Car Repair if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil }

32 Mohamed Shalan CSE 324 - 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.

33 Mohamed Shalan CSE 324 - Computer Programming 33 Selection Example – Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Sequence if the condition is true.

34 Mohamed Shalan CSE 324 - Computer Programming 34 Selection Example – Car Repair (cont) if (motor turns) then { CheckFuel CheckSparkPlugs CheckCarburettor } else { CheckDistributor CheckIgnitionCoil } Sequence if the condition is false.

35 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

36 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 zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } }

37 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 zero while (reply is "No" and begging count < 100) { Say("Oh please!") add 1 to begging count ListenToReply ( ) } } Condition is tested before sequence

38 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 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

39 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 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

40 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 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

41 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 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?

42 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

43 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

44 Mohamed Shalan CSE 324 - 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 10100110 01110110 00100110 00000000 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc...

45 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.


Download ppt "1 CSE 324 - Computer Programming “Using C-Language” Lectures 1."

Similar presentations


Ads by Google