CONTENT WRITING TEMPLATE Author: : <Fariza Hanum Md Nasaruddin> Date: : <01/08/08/> Subject Title: : Computer Programming Subject Code : CCPS2083 Lesson Title : Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Version: 1.0 Introduction to Programming Introduction: <Attention grabber> At the ATM machine Please insert your bank card Please enter your pin number Please choose the service you want (eg. Withdrawal, fund transfer, exit, etc) Please enter the amount you want to withdraw Please remove your card Please remove money from the slot Please take receipt Thank you and have a nice day Have you ever thought about how the ATM is able to interact with you? Learning Outcome: At the end of this lesson, students will be able to : 1. 2. 3. 4. 5. 6. 7. Explain what is a program List and explain the various components of a computer system List the steps involved in designing and writing programs Use pseudocode and flowchart in their program design Conduct desk-checking of a program Explain the steps in coding, compiling, and running a C++ program Differentiate the different types of computer program errors *Every topic must cover one learning outcome only. <Topic 1> < Introduction to Programming > | Private and Confidential 1 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 List of Topic No Topic Title 1. What is a program ? 2. Computer systems 3. How to design and write programs 4. Programming Methodology 5. Coding, compiling and running a C++ program 6. *Maximum 12 topics. Terminology No 1. Execution Word 2. IDE 3. 4. 5. 6. 7. 8. 9. 10. syntax Compilation error bug Definition Integrated Development Environment Reference: 1. Ani – Animation 2. Gfx – Graphics 3. Sub Topic <Topic 1> < Introduction to Programming > | Private and Confidential 2 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Content Topic Title : Topic 1: What is a program? 1.1.0 - A program is a set of instruction for the computer to follow. - The computer follows the instructions by executing the program. *** Animation of /simulation of program running Then show the codes *** Another example Animation of /simulation of program running Then show the codes *** Diagram to show stakeholders involved with a program : user, programmer *** NOTE: IN THIS COURSE YOU WILL LEARN HOW TO CREATE PROGRAMS YOU ARE THE PROGRAMMER Activity A (Courseware Activity) <Instruction: Click and drag …etc> Activity B (Group Activity) <Instruction: Discuss, collect, visit…etc> Exercise <Topic 1> < Introduction to Programming > | Private and Confidential 3 CONTENT WRITING TEMPLATE <Topic 1> < Introduction to Programming > | Private and Confidential Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 4 CONTENT WRITING TEMPLATE Topic Title : Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Topic 2: Computer systems 1.2.0 What makes a computer? Computer needs 2 things to call it a computer: 1) Hardware 2) Software *** What is hardware? Show pictures of computers used at offices, labs, clinics, such as PC, mainframe. *** What is Software ? Show a movie about someone buying a PC. Go home switch on, no window on screen Why? The hardware is there, but there is no software *** 1.2.1 Hardware Hardware is the physical parts of the computer. There are 5 physical components that makes up a computer: 1. Input device – Mouse, keyboard 2. Output device – Monitor, printer 3. Processor 4. Main memory – RAM 5. Secondary memory – Hard disk, thumb drive. *** Animation to open up a PC – see the components *** 1.2.2 Software Software are programs to allow you to talk to the computer Without software, your computer will just be a machine, a hardware There is two main software you use with your computer <Topic 1> < Introduction to Programming > | Private and Confidential 5 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 1. Operating System 2. Application Program *** The main software in a computer is the Operating System (OS) The OS job is to allows you to interact with the computer OS also function as resource manager to your computer OS handles if you want to run programs in your computer *** You, as a person who uses computer, run application programs on your computer Application programs perform specific tasks. Examples are word processors, spreadsheets, games. These application programs have been developed by a professional programmer and sold to users. *** You will learn the idea on how these programs are designed and written. Activity A (Courseware Activity) Activity B (Group Activity) Exercise . . . . *Add more table for new topic if necessary. <Topic 1> < Introduction to Programming > | Private and Confidential 6 CONTENT WRITING TEMPLATE Topic Title : Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Topic 3: How to design and write programs? 1.3.0 To write a program you need to design it first To design, you need know what what it is that you want and what you want to achieve What is the objective of the program Designing and writing a program is a whole process and the steps of the process is known as Software Development Life Cycle (SDLC) 1.3.1 SDLC What is SDLC? For the purposes of developing a program, we can consider these steps to be important: 1. Analysis 2. Software designing (Algorithm) 3. Implementation (Writing the codes) 4. Testing 5. Maintenance In this course, we will focus only on step 1, 2, 3. 1.3.2 Problem Solving You have to understand the problem before you can attempt to solve it. You will have to analyze the problem What is it that the program must do? Determine IPO (Input – Process- Output) *** It is sometimes helpful to start by determining the output that the program must produce. In order to get an output, the program requires input. Determine what that input is. Then, you need to figure out the process that must be carried out to take the input and produce the desired output. *** Display this image. <Topic 1> < Introduction to Programming > | Private and Confidential 7 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Activity A (Courseware Activity) Activity B (Group Activity) Exercise Topic Title : Topic 4: Programming Methodology 1.4.0 During the problem solving phase, you need to be certain the task is completely specified What is the input? What information is in the output? How is the output organized? <Topic 1> < Introduction to Programming > | Private and Confidential 8 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Next, you need to develop the algorithm before implementation. *** 1.4.1 Algorithm Algorithm is a sequence of precise instructions which leads to a solution. Program is an algorithm, expressed in a language the computer can understand *** Algorithm can be written as a list of steps that needs to be done in order to come to a solution of the problem. Algorithm can also be expressed using 1) Pseudocode 2) Flow chart *** Before implementation you will have to test the algorithm for correctness This saves time in getting your program to run. To do this, you can perform a process called the Desk-checking *** Sample problem : Convert currency Ali is a businessman who runs a foreign exchange bureau, exclusively for exchange currencies from US dollars to Malaysian Ringgit. The conversion rate between these two currencies varies daily. Ali wants a program that would help him calculate the conversion and display it on the computer’s monitor for his customer to see. *** Analyze problem & design algorithm <Insert Table1. IPO> 1.4.2 Pseudocodes If you prefer, you can represent the algorithm you have designed using pseudocode. Pseudocode is an informal language Language is determined by the designer themselves. Mostly looks like English mixed with programming language codes Allows us to make our algorithm precise without worrying about the details of programming language syntax *** <Topic 1> < Introduction to Programming > | Private and Confidential 9 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Below is how the pseudocode for Example 1 may be written. Start Get value of US dollars (USD) Get conversion rate RM = USD x conversion rate Display value in RM End Program *** More examples : The following are 2 alternative pseudocodes to write a program calculating the sum of two numbers PROGRAM Add Two Numbers READ two numbers ADD the numbers WRITE the sum END PROGRAM PROGRAM Add Two Numbers READ First READ Second Sum = First + Second WRITE Sum END PROGRAM As you can see, there are no specific rules when writing pseducode. 1.4.3 Flow chart Flowchart is a diagram that shows the logical flow of a program It is useful for planning each operation a program performs, and in order in which the operations are to occur It helps progarmmer to visualize the process flow the program has to go through The final visualization can then be easily translated into a program code *** Flow chart uses standardize symbols for representation. <INSERT TABLE 2> Example of a flow chart for the problem convert currency discussed earlier <Topic 1> < Introduction to Programming > | Private and Confidential 10 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Get USD Get Conv Rate RM = USD X Rate Display RM Stop *** Another flowchart (for the program calculating the sum of two numbers) <Topic 1> < Introduction to Programming > | Private and Confidential 11 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 1.4.4 Desk-checking Desk-checking is done to make sure that the design algorithm is doing the correct things and to check whether it missed any steps. What you do when performing a desk-check ? 1) Select value for input data 2) Manually solve the problem 3) Solve the problem using your algorithm 4) Compare step 2 & 3. If it is consistent, it means your algorithm is correct. *** Performing a desk check on the convert currency problem : 1. choose a set of input values Set 1 : USD = 100 Converrion rate = 3.48 Set 2 : USD = 33 Conversion rate = 3.50 *** 2. Manually calculate the expected outcome. Therefore, the outcome for the input sets are as follows : Set 1: Output = 100 x 3.48 = RM348.00 <Topic 1> < Introduction to Programming > | Private and Confidential 12 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Set 2: Output = 33 x 3.50 = RM115.50 *** 3. Using the input sets, by going through each step of the algorithm. To assist in the desk-checking, first create a table that contains a column for each input, output, any temporary calculation (if necessary). For the program given above, you will need a table as shown . Calculate the RM value based on the formula given. <INSERT TABLE 4> 4. Compare the output with your outcome in step2. Activity A (Courseware Activity) Activity B (Group Activity) Exercise <Topic 1> < Introduction to Programming > | Private and Confidential 13 CONTENT WRITING TEMPLATE Topic Title : Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Topic 5 : More Examples 1.5.0 We have learned what to do theoretically on how to design a problem solving program. Now let’s go through the steps. Problem : Let’s just say that Ahmad will fill up his car’s tank to full and reset his car’s trip meter to zero. -After travelling 378 miles, Ahmad stopped at a petrol station to refuel. The car required 25 liters to fill up the tank. Create a program that can calculate the number of km the car can be driven per liter. 1.5.1 Algorithm <INSERT TABLE 5 IPO CHART> 1.5.2 Pseudocode Start Get value of km driven Get liter of petrol used Km per liter = km driven / liter of petrol used Display value of km per liter End Program 1.5.3 Desk-Checking There are several steps to follow when desk-checking: 1) Choose a set of inputs and calculate manually so to get the expected output. Thus: Set 1 : km driven = 324 Liter of petrol used = 35 Set 2 : km driven = 675 Liter of petrol used = 60 <Topic 1> < Introduction to Programming > | Private and Confidential 14 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 /*****************************/ 2) For each input calculate the expected outcome. Thus: Set 1: Output = 324 divided by 35 = 9.25 km per liter Set 2: Output = 675 x 60 = 11.25 km per liter /*****************************/ 3) Check your psuedocode Start Get value of km driven Get liter of petrol used Km per liter = km driven / liter of petrol used Display value of km per liter End Program /*****************************/ 4) Create a table with columns for your input and output and insert input the values. Thus: <INSERT TABLE 6 Desk Check Table – values for km driven petrol used entered > /*****************************/ 5) Using the algorithm, calculate the output column. Thus: <INSERT TABLE 7 Km per liter value calculated and entered in the check table.> /*****************************/ 6) Display aka prints the value on monitor. If value on monitor is the same as the manual calculation, algorithm is correct. /***************************/ 7) Re-do the desk-check by using different sets of input. Activity A (Courseware Activity) Activity B (Group Activity) <Topic 1> < Introduction to Programming > | Private and Confidential 15 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Exercise 1. Perform a desk check using the desk-checking table for the Add Two Numbers problem discussed earlier in this section. Topic Title : Topic 6: Coding, compiling and running a C++ program 1.6.1 Layout of a C++ program Programs are written in programming language, such as: - FOTRAN COBOL Pascal Java ASP PHP C++ The listed languages are called high-leveled language. It mostly uses English words and symbols. Each language has its own rules and grammar. <Topic 1> < Introduction to Programming > | Private and Confidential 16 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 NOTE : This course will concentrate on C++. /***************************/ -The first thing to master in learning a programming language is the grammar. In the programming language it is called the syntax. -You must know that there is a specific way or a specific layout when using a programming language to writing a program. (NO EXEPTIONS) -There are some specific “statements” which you must have in your program and it must be typed exactly as the language requires it. /***************************/ -A source codes can be typed using any “text writing program” such as: - Notepad C++ development kit (most recommended) Microsoft Word Ect. /*************************/ Layout of a C++ program : #include <iostream> using namespace std; int main() { variable_declarations statement_1 statement_2 … statement_last return 0; } *** Example of a written program using C++ language :(A simple program so there is no need for an algorithm) // This is a C++ program #include <iostream> <Topic 1> < Introduction to Programming > | Private and Confidential 17 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 using namespace std; int main(){ cout << “This is a program\n”; return 0; } /***************************/ To explain the codes, each line will be given a number to enable us to refer to it. 1. // This is a C++ program 2. #include <iostream> 3. using namespace std; 4. 5. int main(){ 6. cout << “This is a program\n”; 7. return 0; 8. } *** Explanation of the codes: (More in depth explanation is in next chapter) Line 1: // symbol is use for comments Line 2: # symbol is a pre-processor directive. #include means that it is a directive that must be included in the C++ program. #include <iostream> means that, it will ask the complier to include this in the program. iostream is used to make the program able to input/output statements. Line5: main() since main is followed by () means main is a function name. There can be only one main() function. But may have many functions. { means the opening of the body function. Line 6: cout << will print the text from “ to “ on the monitor. Thus the program will display This is a program on the computer’s monitor. ; is to end a statement. Line 7: return 0; will return the value 0 to the operating system saying that the program was… …successfully terminated. More on this in later chapters. <Topic 1> < Introduction to Programming > | Private and Confidential 18 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Line 8: } is to indicate the closing of the body function. NOTE: C++ is case sensitive. So typing include with capital “I” eg.” #Include” will result in compilation error. 1.6.2 Coding a C++ program C++ source code is written with a text editor Programmers format programs so they are easy to read Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves Indent statements Use only one statement per line Variables are declared before they are used Typically variables are declared at the beginning of the program Statements (not always lines) end with a semi-colon Include Directives #include <iostream> Tells compiler where to find information about items used in the program For example, iostream is a library containing definitions of cin and cout 1.6.3 Compiling and running C++ program There are many ways to compile your codes. <Topic 1> < Introduction to Programming > | Private and Confidential 19 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 The easiest way is using C++ development kit. The steps to compile are (using IDE DEV C++): 1. 2. 3. 4. 5. Press F9 Save the file in a folder. If there is no compilation error, program is compiled and run. *If there is errors, try debugging it and save the file again. Try compiling again.* The * mean the steps is only done if there is compilation error. *** What happens within the IDE when you compile and run your program ? C++ source code is written with a text editor The compiler on your system converts source code to object code. The linker combines all the object code into an executable program. Compiler: is a program that translates a high-level language program, such as a C++ program, into a machine-language program that the computer can directly understand and execute. Linking: The object code for your C++ program must be combined with the object code for routines (such as input and output routines) that your program uses. This process of combining object code is called linking and is done by a program called a linker. For simple programs, linking may be done for you automatically. *** Running a program involves the following : Obtain code (from source file) Compile the code (using a compiler or in IDE) Fix any errors the compiler indicates and re-compile the code Run the program *** Testing and debugging your program A bug is a mistake in a program <Topic 1> < Introduction to Programming > | Private and Confidential 20 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 Eliminating mistakes in programs is called debugging. *** Types of programming errors Syntax errors Violation of the grammar rules of the language Discovered by the compiler • Run-time errors Error messages may not always show correct location of errors Error conditions detected by the computer at run-time Logic errors (warning) Errors in the program’s algorithm Most difficult to diagnose Computer does not recognize an error Activity A (Courseware Activity) 1. Arrange the following in the order which is relevant to producing a program. debug, compiler, linker, analyze, source code, algorithm, desk checking, 2. Arrange the following in the right order reflecting the correct C++ program layout: { int main() } #include <iostream> return 0; variable_declarations <Topic 1> < Introduction to Programming > | Private and Confidential 21 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 statement_1 statement_2 … using namespace std; statement_last Activity B (Group Activity) Exercise 1. What is an object code? 2. What are the different types of error you may encounter when writing, compiling, and running a program? 3. What is a source program? 4. Where do you type in your source code? 5. What is an IDE ? 6. Name some of the more popular IDE for C++ language programming. You can find your answer by browsing the internet. Summary In the end of this lesson, you have learned: what is a program what are the various components of a computer system the steps involved in designing and writing programs that you can use pseudocode and flowchart in designing the algorithm how to conduct desk-checking of a program <Topic 1> < Introduction to Programming > | Private and Confidential 22 CONTENT WRITING TEMPLATE Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 how to coding, compiling, and running a C++ program the different types of computer program errors Self Assessment 1. What is the function of an operating system? 2. What are the processes you need to do after you have designed your algorithm in order to get your program running? 3. What is a compiler and it’s purpose? 4. Design an algorithm to find the weighted average of four test scores. The four test scores and their respective weights are given un the following format. Testscore1 weight1 Sample data may be as follows 75 0.20 95 0.35 85 0.15 65 0.30 5. The cost of an international call from Malaysia to Saudi Arabia is calculated as follows: Connection fee, RM1.99; RM2.00 for the first three minutes; and RM0.45sen for each additional minute. Design an algorithm that asks the user to enter the number of minutes the call lasted. The number of minutes is then used to calculate the cost of the call. Reference <URL, books, journal…etc> Ending *A short brief for the next lesson. <Topic 1> < Introduction to Programming > | Private and Confidential 23 CONTENT WRITING TEMPLATE <Topic 1> < Introduction to Programming > | Private and Confidential Revision No. Document No. Effective Date: 0 CWT/ISDT/001 Sept 2008 24