CSC-113 COMPUTER PROGRAMMING LECTURER FASIHA IKRAM
GENTLE REMINDER Switch Off your Mobile Phone Or Switch Mobile Phone to Silent Mode
An Overview of Programming Languages and Problem Solving Techniques
COMPUTER PROGRAM A program (also commonly called an application or software) is a set of instructions that tells the computer what to do. The physical computer machinery that executes the instructions is the hardware. Adobe Acrobat, Adobe Photoshop, FileZilla, Google Chrome, Microsoft Excel, Microsoft PowerPoint, Microsoft Word, Mozilla and more.
PROGRAMMING LANGUAGES Various programming languages enable people to tell computers what to do. A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks. Computer programs are created by using programming languages. Foundation for developing applications. Such as BASIC, C, C++, COBOL, FORTRAN, Ada, and Pascal. Modern programming languages fall into following five categories. First generation Languages Second generation Languages Third generation Languages Fourth generation Languages Fifth generation Languages Machine Language assembly language procedural non procedural NLP
PROGRAMMING LANGUAGES Machine Language (first generation of programming languages) The computer s native language Composed of binary digits (0s, 1s) The only language that computers understand 11011101 1011011 01001100 1011100 11011100 1011011 Assembly Language (second generation of programming languages) One-to-one correspondence to machine language Somewhat more user-friendly than machine language (mnemonic rather than binary digits) Assembler program that translates an assembly language program into machine language
PROGRAMMING LANGUAGES Procedural Languages (High Level Languages) (third generation languages) write programs without having to be as concerned about what kind of computer the program is being run on and are also closely resemble with mathematical notations and human readable. Examples: C, Fortran, QuickBasic Compiler - translates the entire program into machine language Interpreter - translates and executes one source program statement at a time. Nonprocedural Language (fourth generation languages) Allows the user to specify the desired result without having to specify the detailed procedures needed for achieving the result Example: data base query language - SQL Can be used by non technical users. List all records where age >18 Sum = num1+num2;
PROGRAMMING LANGUAGES Represent the equation wages = rate hours to calculate the weekly wages Machine language 100100 stands for load 100110 stands for multiplication 100010 stands for store Assembly language Can be written as follows: 100100 010001 100110 010010 100010 010011 High level language In C++, can be written as follows: wages = rate * hours; Can be written as follows: LOAD rate MULT STOR hour wages
PROGRAMMING LANGUAGES Natural Language Programming Languages (fifth generation (intelligent) languages) Translates natural languages into a structured, machine-readable form Are extremely complex and experimental
ELEMENTS OF PROGRAMMING: Each programming statement is made up of following elements. 1. 2. 3. Symbols Reserved words Identifiers Symbols include mathematical operates and punctuations. + Plus used for addition - Hyphen for subtraction * Asterisk for multiplication / Forward slash use for division // backward slash use for comments ; Semi-colon used for statement termination inverted commas! Not operator Identifiers Special names created by programmer for. The name of the program Constants used in the program Variables used in the program
ELEMENTS OF PROGRAMMING: Reserve words Reserve words are words that have special meaning in the programming language and cannot used as identifier names. the reserved words, must not be used for any other purposes. The reserved words already used are int and void. All reserved words are in lower-case letters. Cout do goto signed unsigned break double if sizeof void Case else int static volatile Char enum long struct while cin extern register switch default float return typedef for short union const
ELEMENTS OF PROGRAMMING: Activity: Add two numbers. Find out the identifiers, symbols and reserve words. #include <iostream> using namespace std; int main() { } string Myname='M. ALi Azeem'; cout <<"My name is " << Myname << endl; // your code goes here return 0; Symbols Reserve Words identifiers # Include Main << Using Myname <> Namespace ; Std Return }{ Int () String = Cout // End1
ELEMENTS OF PROGRAMMING: Activity2: #include <iostream> Symbols Reserve Words identifier s using namespace std; int main() { int num1, num2, sum; num1=5; num2=7; cout << "Enter two numbers to add\n"; sum = num1 + num2; cout <<"Sum of entered numbers = " << sum <<endl; // your code goes here return 0; }
STEP IN PROGRAM DEVELOPMENT Programming can be defined as the development of a solution to an identified problem, and the setting up of a related series of instructions that will produce the desired results. The program development process is a series of activities that are necessary for the successful development of a computer program, however simple or complex. The program development process can be divided into six stages. 1. Define the problem 2. Designing the program 3. Coding the program 4. Testing the program 5. Installing and maintain the program 6. Documentation the program
STEP IN PROGRAM DEVELOPMENT Define the Problem To help with initial analysis, the problem should be divided into three separate components: The inputs required by the program. The outputs or end result required. The processing steps to produce the required outputs. IPO Chart: IPO charts use to organize and summarize the results of a problem definition. The output is the first data added to the IPO chart, you can then ask what input the program need to produce the required output. Processing is simply the series of actions you must perform on the input to get the required output.
WRITE A PROGRAM CALCULATING THE SUM OF TWO NUMBERS : ADDITION Input Processing Output 5, 10 15 1) Declare variables num1 num2 sum 2) Assign values num1 = 5 num2 = 10 3) Process sum = num1 + num2 Names for our cells The computer (and so C) provides basic arithmetic operations. If the operation you want to use is not provided, you have to compose it.
STEP IN PROGRAM DEVELOPMENT Activity #3: Write a program calculating the sum of two numbers: Addition. Suppose you were asked to add two numbers mentally. The first thing you would do is ask for the two numbers. When you are given the two number. You would add them. You would then communicate the result to the person who asked you for it. IPO Chart Input Process Output Num1 Num2 Input Num1 Input Num2 Sum=num1+num2 print Sum; Sum
STEP IN PROGRAM DEVELOPMENT Activity #4: Write a program calculating the average of two numbers: Suppose you were asked to add two numbers mentally. The first thing you would do is ask for the two numbers. When you are given the two number. You would add them, then divide the result by 2 to he get the average.. You would then communicate the result to the person who asked you for it. Input Process Output Ipo Chart Num1 Num2 Input Num1 Input Num2 Sum=num1+num2 Average=sum/2 print average; Average
STEP IN PROGRAM DEVELOPMENT Activity #5:Write a program that and prints the tax paid by a worker and net pay received, given the hourly rate and the number of hours worked for the week. Tax is equal to total pay minus tax. For process 1. Compute the total pay. (hourlyrate*hoursworked) 2. Compute the tax paid.(taxrate*totalpay) 3. Compute the net pay.(totalpay-tax)
STEP IN PROGRAM DEVELOPMENT (DESIGNING ) Designing the problem Once we have clearly define the problem, several programming tools are now available to assist the programmer is designing a solution for the problem. One should note that for a given problem, several solutions may exist. The programmer must examine the alternative solutions and choose the best one. Pseudo codes Algorithms Flowcharts Decision tables Structure HIPO chart
PSEUDOCODE WRITE A PROGRAM CALCULATING THE SUM OF TWO NUMBERS Pseudocode are English-like statements that follow a loosely defined syntax and are used to convey the design of an algorithm. A pseudocode is an informal programming language that s is very similar to the high level programming language but without the strictness of the syntax of a high level language. Version 1: Version 2: 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
WHAT IS AN ALGORITHM? A step-by-step problem solving procedure, especially an establish, recursive computational procedure for solving a problem in a finite number of steps. An algorithm has to be clear have a finite length stop in finite time There are many models supporting the development of the code: Pseudocode Structure Diagram and finally in C++ Flowcharts All the models try to achieve something. What?
MODULAR DESIGN WRITE A PROGRAM CALCULATING THE SUM OF TWO NUMBERS Pseudocode are more closely to the programming language and for the advanced programmer. Modular Design Structure Diagrams are helpful to break the algorithm into more manageable pieces. Flowcharts show the workflow of the algorithm and stress on structured programming. We don t break according to the sequence in which the parts are following, but rather in its frequency of use and similarity to other parts. Every model has its advantages and disadvantages. But all try to help you to structure your code in a top-down style and this is the way you should implement your algorithm.
STEP IN PROGRAM DEVELOPMENT(DESIGNING ) Flowchart Flowchart is a graphic from of a pseudocode or algorithm. Programming instructions are represented by symbols instead of alphanumeric phrases. Flowchart Symbol Name Description PROCESS An operation or action Terminator Decision A start or end point in a program A question or branch in a program Input/output Indicated data input or output to or form a program Flow line Indicates direction of program flow connector A jump from one point to another
FLOWCHARTS SUM OF TWO NUMBERS START Addition of two numbers. READ First READ Second step 1. Start step 2. Read num1, num2 Sum = First + Second WRITE Sum End step 3. Sum=num1+num2 step 4. write sum; step 5. end
FLOWCHARTS SUM OF TWO NUMBERS START Addition of two numbers. READ First READ Second Sum = First + Second WRITE Sum step 1. Start step 2. Read num1, num2 step 3. Sum=num1+num2 step 4. write sum; step 5. end End
STEP IN PROGRAM DEVELOPMENT 3. Coding the program Code the Algorithm into a Specific Programming Language Only after all design considerations have been met 4. Testing the program Test the Algorithm for Correctness Most importance in the development of a program To identify major logic errors early, so that they may be easily corrected.
STEP IN PROGRAM DEVELOPMENT 5. Installing and maintain the program The first version of program is ready and now allow user to use and test it. 6. Documentation the program Document and Maintain the program It is an ongoing task from the initial definition of the problem to the final test result. Documentation involves both external and internal documentation that may have been coded in the program. Maintenance requires using and modifying the program if the problem domain changes.
PROGRAMMING WITH THE PROBLEM ANALYSIS CODING EXECUTION CYCLE Problem solving process First step Define the problem. Outline the solution. Design an algorithm. Test the algorithm for correctness. Second step Implement the algorithm in programming language, such as C++. Run the program on the computer Last step Document and maintain the program.
PROCEDURAL VS. OBJECT-ORIENTED PROGRAMMING Procedural programming is based on a structured, top-down approach to writing effective programs. concentrates on what a program has to do and involves identifying and organizing the processes in the program solution Decomposed into separate tasks or functions and includes top-down development broken down problem into more detailed steps modular design grouping task together because they all perform the same function; connected to top-down development Object-Oriented Programming OOP is also based on decomposing the problem. The primary focus is on the things that make up the program.
TOP-DOWN DEVELOPMENT If we look at a problem as a whole, it may seem impossible to solve because it is so complex. Examples: writing a tax computation program Writing a library system Writing a Bank System Complex problems can be solved using top-down design, also known as stepwise refinement, where We break the problem into parts Then break the parts into smaller parts Soon, each of the parts will be easy to solve
ADVANTAGES OF TOP-DOWN DEVELOPMENT Breaking the problem into parts helps us to clarify what needs to be done. At each step of refinement, the new parts become less complicated and, therefore, easier to figure out. Parts of the solution may turn out to be reusable. Breaking the problem into parts allows more than one person to work on the solution. Problem: You own a home improvement company providing painting, roofing, and basement waterproofing service. A section of the town has recently flooded (zip code 43100). You want to send out pamphlets to our customers in that area.
THE TOP LEVEL Get the customer list from a file. Sort the list according to zip code. Make a new file of only the customers with the zip code 43100 from the sorted customer list. Print an envelope for each of these customers. Main Read Sort Select Print
ANOTHER LEVEL? Should any of these steps be broken down further? Possibly! How do I know? Ask yourself whether or not you could easily write the algorithm for the step. If not, break it down again. When you are comfortable with the breakdown, write the pseudocode for each of the steps (modules) in the hierarchy. Typically, each module will be coded as a separate function. Another Example: A Top-down analysis of a simple cooking task Cook Breakfast Bring out cook ware Take out Uncooked food Cook food Serve food Get Coffee Pot Get Frying pan Get fork Get Serving dishes
WRITE A PROGRAM CALCULATING THE SUM OF TWO NUMBERS : ADDITION Input Processing Output 5, 10 15 1) Declare variables input_1 input_2 sum 2) Assign values input_1 = 5 input_2 = 10 3) Process sum = input_1 + input_2 Names for our cells The computer (and so C) provides basic arithmetic operations. If the operation you want to use is not provided, you have to compose it.
STRUCTURE DIAGRAM WRITE A PROGRAM CALCULATING THE SUM OF TWO NUMBERS Version 1: Version 2: Add Two Numbers Add Two Numbers READ Two Numbers ADD Two Numbers Write The Sum READ Two Numbers ADD Two Numbers Write The Sum Read Num1 Sum = Input_1 +Input_2 Read Num2 Take note: We develop software iteratively (meaning version by version), but the code itself is broken in top-down manner!!
C++ CODING WRITE A PROGRAM CALCULATING THE SUM OF TWO NUMBERS /* Addition of 2 numbers */ #include <iostream> using namespace std; int main() { int first, second, sum; cin>>first; cin>>second; sum = first + second; cout<<sum<<endl; Where do you find now in the code the steps we have developed? 1. Declaration of the structure 2. Reading the input 3. Processing the data 4. Writing the output } return 0;
PROGRAMMING GUIDELINE The following rules are intended to avoid some mistakes students normally do. Even, if you have not programmed much until now and some of the given rules may still be a little bit cryptic, you can capture the idea and apply the rules right from the start. Programming can be fun, but also a torture.
PROGRAMMING GUIDELINE Rule 1: Think about your strategy twice! Choose a strategy possibly coming out with less program lines. Rule 2: You cannot program, what you cannot do with paper and pencil. It is worth to rethink your strategy. - Do I really have chosen the best? - Can I reformulate my problem to make it more simple? Even after having an algorithm ready in mind. Play with your data. Play to be the computer. You are only allowed to perform actions a computer can. Can you solve your problem this way? No? Then do not touch a computer and think about your problem again.
PROGRAMMING GUIDELINE Rule 3: The compiler must be easy and in short time accessible. You can still use old DOSshells, if you want. But open at least two windows, one for editing, one for compiling the code. Prepare your programming environment, save your files, that it is fun to program. Rule 4: (Data) Structure before functionality! First declare all the variables you will need. (Later you will use more advanced structures like objects, trees, graphs,..) But in all cases: The data structure you need has to be ready first.
PROGRAMMING GUIDELINE Rule 5: Name your variables appropriate and format your code in a way, that the structure is easily detectable. while (counter < 100) { if (no > 1) {... } } while (counter < 100){ if (no > 1) {... } } Rule 6: Write code using a divide and conquer approach. (Top-down) Express READ Two Numbers Test PROGRAM Add Two Numbers ADD Two Numbers Cycle WRITE The Sum
PROGRAMMING GUIDELINE Rule 7: Compile every three lines. Write code in such a way, that it Compiles and executes at every moment of the code development! Rule 8: Do your best in writing the documentation, learn from your code. Make sure you do not do the same mistakes again. Express Test Cycle Whatever we do, our code always compiles and executes!! Compile very often! Detect a mistake as soon as possible! /* Addition of 2 numbers */ #include <iostream> using namespace std; int main() { /* The input numbers */ int first, second;... /* The output number */ int sum; Commen clearly
Summary A program (also commonly called an application or software) is a set of instructions that tells the computer what to do. A vocabulary and set of grammatical rules for instructing a computer to perform specific tasks. Computer programs are created by using programming languages. Foundation for developing applications. Elements of programming are :Symbols,Reserved words and Identifiers. IPO charts use to organize and summarize the results of a problem definition. Pseudocode are English-like statements that follow a loosely defined syntax and are used to convey the design of an algorithm. Flowcharts show the workflow of the algorithm and stress on structured programming.