Elementary Programming with C - Session 1: Basics of C

Differentiate between Command, Program and Software Explain the beginning of C Explain when and why is C used Discuss the C program structure Discuss algorithms Draw flowcharts List the symbols used in flowcharts

ppt26 trang | Chia sẻ: candy98 | Lượt xem: 442 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Elementary Programming with C - Session 1: Basics of C, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Basics of CSession 1Elementary Programming with C/Session 1/ 2 of 26ObjectivesDifferentiate between Command, Program and SoftwareExplain the beginning of CExplain when and why is C usedDiscuss the C program structureDiscuss algorithmsDraw flowchartsList the symbols used in flowcharts Elementary Programming with C/Session 1/ 3 of 26Software, Program and CommandSoftwareProgram 2Program 1 Commands Commands CommandsElementary Programming with C/Session 1/ 4 of 26The Beginning of CC – Dennis RitchieB – Ken ThompsonBPCL – Martin RichardsElementary Programming with C/Session 1/ 5 of 26Application Areas Of C C was initially used for systems programming A system program forms a portion of the operating system of the computer or its support utilities Operating Systems, Interpreters, Editors, Assembly programs are usually called system programs The UNIX operating system was developed using C There are C compilers available for almost all types of PC’sElementary Programming with C/Session 1/ 6 of 26Middle Level LanguageHigh Level LanguageAssembly LanguageCElementary Programming with C/Session 1/ 7 of 26Structured Language C allows compartmentalization of code and data It refers to the ability to section off and hide all information and instructions, necessary to perform a specific task, from the rest of the program Code can be compartmentalized in C by using functions or code blocks.Elementary Programming with C/Session 1/ 8 of 26About C C has 32 keywords These keywords combined with a formal syntax form a C programming language Rules to be followed for all programs written in C:All keywords are lowercasedC is case sensitive, do while is different from DO WHILEKeywords cannot be used as a variable or function name main(){/* This is a sample Program*/ int i,j; i=100; j=200; : }Elementary Programming with C/Session 1/ 9 of 26The C Program Structure-1main()Elementary Programming with C/Session 1/ 10 of 26Delimiters { ... }The C Program Structure-2Elementary Programming with C/Session 1/ 11 of 26Statement Terminator .... ;The C Program Structure-3Elementary Programming with C/Session 1/ 12 of 26/* Comment Lines */The C Program Structure-4Elementary Programming with C/Session 1/ 13 of 26The C Library All C compilers come with a standard library of functions A function written by a programmer can be placed in the library and used when required Some compilers allow functions to be added in the standard library Some compilers require a separate library to be createdElementary Programming with C/Session 1/ 14 of 26Compiling & Running A ProgramElementary Programming with C/Session 1/ 15 of 26The Programming Approach to Solving ProblemsClassroomLeaving the classroomHead towards the staircaseGo to the basementHead for the cafeteriaCafeteriaAlgorithm is a set of steps that are performed to solve a problem. The example below describes an algorithmThese are the steps followed when a student wants to go to the cafeteria from the classroomElementary Programming with C/Session 1/ 16 of 26Solving a ProblemIn order to solve a problemUnderstand the problem clearlyGather the relevant informationProcess the informationArrive at the solutionElementary Programming with C/Session 1/ 17 of 26PseudocodeIt is not actual code. A method of algorithm - writing which uses a standard set of words which makes it resemble codeEach pseudocode starts with a BEGINTo show some value , the word DISPLAY is usedThe pseudocode finishes with an END BEGINDISPLAY ‘Hello World !’ENDElementary Programming with C/Session 1/ 18 of 26FlowchartsIt is a graphical representation of an algorithmSTARTDISPLAY ‘Hello World !’STOPElementary Programming with C/Session 1/ 19 of 26The Flowchart SymbolElementary Programming with C/Session 1/ 20 of 26Flowchart to add two numbers Elementary Programming with C/Session 1/ 21 of 26 The IF ConstructBEGININPUT numr = num MOD 2IF r=0Display “Number is even”END IFENDYesNoElementary Programming with C/Session 1/ 22 of 26 The IF-ELSE ConstructBEGININPUT numr=num MOD 2IF r=0 DISPLAY “Even Number”ELSE DISPLAY “Odd Number”END IFEND YesNoElementary Programming with C/Session 1/ 23 of 26Multiple criteria using AND/OR BEGININPUT yearsWithUsINPUT bizDoneIF yearsWithUs >= 10 AND bizDone >=5000000 DISPLAY “Classified as an MVS”ELSE DISPLAY “A little more effort required!”END IFEND Elementary Programming with C/Session 1/ 24 of 26 Nested IFs-1BEGININPUT yearsWithUsINPUT bizDoneIF yearsWithUs >= 10 IF bizDone >=5000000 DISPLAY “Classified as an MVS” ELSE DISPLAY “A little more effort required!”END IFELSE DISPLAY “A little more effort required!”END IFEND Elementary Programming with C/Session 1/ 25 of 26 Nested IFs-2INPUT YearsWithUsSTARTINPUT bizDoneYearsWithUs >= 10 bizDone > 5000000DISPLAY “A Little more effort required”STOPNOYESNOYESDISPLAY “A Little more effort required”DISPLAY “Classified as an MVS”Elementary Programming with C/Session 1/ 26 of 26 LoopsBEGINcnt=0WHILE (cnt < 1000)DO DISPLAY “Scooby” cnt=cnt+1END DOEND YesNo