Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Library (computing) wikipedia , lookup
Falcon (programming language) wikipedia , lookup
Go (programming language) wikipedia , lookup
One-pass compiler wikipedia , lookup
Program optimization wikipedia , lookup
Assembly language wikipedia , lookup
History of compiler construction wikipedia , lookup
C Sharp (programming language) wikipedia , lookup
Chapter Outline • A Brief History of Programming Language • Introduction to Programming – What is a computer program and importance of computer programming – Importance of good programs. – Relationship between compilers, interpreters, assemblers and programs – C++ program structure • Program Development Life Cycle – Problem solving phases; problem definition, algorithm design and implementation – Analysis, design, coding, maintenance Chapter Outcome At the end of this class, student should be able to: • Understand the concept and importance of program and programming • Differentiate between compiler, program, interpreter and assembler • Apply the steps in program development life cycle. A Brief History Of Programming Language • Programming Language is an artificial and formal language that has a limited vocabulary consisting of a set of keywords for making up instructions and a set of precise grammar rules. • Example: C, C++, Java, COBOL A Brief History Of Programming Language The Language of Computer • Digital signals are sequences of 0s and 1s • Machine language: language of a computer Binary digit (bit) The digit 0 or 1 Binary code A sequence of 0s and 1s Byte A sequence of eight bits A Brief History Of Programming Language Types of Programming Language Low-level Language • Machine Language • Early computers were programmed in machine language • Assembly Language • Assembly language instruction are mnemonic (use code, easier to remember) • Assembler: translates a program written in assembly language into machine language High-level Language • Languages that are machine independent. • Instructions are quite English-like • Single instruction can be written to correspond to many operations at the machine level. • Include Basic, FORTRAN, COBOL, Pascal, C++, C, and Java • Compiler: translates a program written in a high-level language machine language • The equation BMI = weight (kg) / (height (m) x height (m)) • can be written in C++ as: BMI = weight/pow(height,2); A Brief History Of Programming Language Example of Programming Language machine language: 100100 010001 // Load BMI = weight/pow(height,2); 100110 010010 // Multiply 100010 010011 // Store assembler ASSEMBLY LANGUAGE •LOAD •STOR •MULT •ADD •SUB MACHINE LANGUAGE •100100 •100010 •100110 •100101 •100011 compiler High Level Language •BMI = weight/pow(height,2); Introduction To Programming What is Computer? • A device capable of performing computations and making logical decisions at speed faster than human being. • An electronic machine, operating under the control of instructions stored in its own memory Basic Operation of a Computer Accepting data from a user INPUT Manipulating data PROCESS Producing results OUTPUT STORAGE Storing results ** These operations are under the control of instructions stored in the computer’s memory Component of a Computer COMPUTER HARDWARE The electric, electronic and mechanical equipment that makes up a computer SOFTWARE The series of instructions that tells the computer hardware how to perform tasks What is a program? • • • • A program is a set of instructions that tells the computer how to solve a problem or perform a task. A program is like a recipe. It contains a list of ingredients (variables) and a list of directions (statements) that tell the computer what to do with the variables. A computer program is a set of detailed directions telling the computer exactly what to do, one step at a time. A program can be as short as one line of code, or as long as several millions lines of code. What is a program? • Computer process data under the control of sets of instructions called computer program. • Computer programs guide the computer through orderly sets of actions specified by computer programmers. What is a programming? • A programming is designing and writing a computer program. • The programmer must decide what the program needs to do, develop the logic of how to do it, and write instructions for the computer in a programming language that the computer can translate into its own language and execute. The Importance Of Good Program • Some people consider Computer Programming as an Art which must follow certain standards in order to promote program clarity, readability, and comprehension so as to produce programs that are easier to read, test, debug, modify, and maintain. • Standards that have been emphasize by most of the programmers are: – Names for variables, types and functions. – Indent style and spacing. The Importance Of Good Program • Names for variables, types and functions. – Choosing an appropriate variable names is seen as the basis for good style programming – The following are some rules that can be used when naming variables, types and functions: • Function names will start with a lower case letter. • Example: double averageMark (double, double); – Variable names start with a lower case letter and the length must not be more than 40 characters. – Constant names can be all capital letters. – Example: const int MAX_SIZE = 10. The Importance Of Good Program • Indent style and spacing. – In order to improve its readability in programming, indentation is used to format program source code. – Adding spaces in between sentence makes your program much more readable. – A new level of indentation should be used at every level of statement nesting in your program. – The minimum number of spaces at each indentation should be at least 3, and many programmers use a tab mark (typically 8 spaces). The Importance Of Good Program • Indent style and spacing. if (marks > 50) { return PASS; } else { return FAIL; } Style 2 Style 1 – Comparison if(marks>50{return PASS;} else {return FAIL;} Style 1 is much easier to read because they are indented well, and logical blocks of code are grouped and displayed together more clearly. Relationship Between Compilers, Interpreters, Assemblers and Program • All programs have to be translated into machine code before the computer can actually run them. • Programs written in high-level languages are translated into machine language by a compiler or interpreter. Relationship Between Compilers, Interpreters, Assemblers and Program • Interpreters – a computer program that translates and executes a program written in a high-level language. – translates the source program, instruction by instruction, each time that program is run. – An interpreter translates program statements one at a time. – Interpreters are helpful during the debugging stage, but are slower during execution of the finished program. Relationship Between Compilers, Interpreters, Assemblers and Program • Compilers – a computer program that translates programs written in a high-level language into machine code. – translates each high-level instruction into several machinecode instructions all at once time. • Compiler runs much faster than an interpreted program. Relationship Between Compilers, Interpreters, Assemblers and Program • Assemblers – Consists of little more than a table look up routine, where each word of the source language (assembly language) is looked up in a table for its numerical equivalent, which is then output as part of the target language program. – Assembly language generally gives the programmer precise and direct access to every capability of the computer hardware. C++ Program Structure • How to write a complete program that consist certain element: – Begin the program with comments for documentation – Include header files, if any are used in the program – Declare variables and constants, if any – Write the definition of the function main Elements Of Computer Program General form of a C++ Program // Introductory comments // file name, programmer, when written or modified // what program does #include <iostream.h> void main() { constant declarations ; variable declarations; executable statements ; } Elements Of Computer Program Example of C++ program: #include <iostream.h> compiler directive/header file braces void main( ) main function { //display greeting message comment cout<<”Welcome to CSC 128 class”; statement } OUTPUT Welcome to CSC 128 class Elements Of Computer Program Comments Preprocessor directives Function Begin block /* File Name Programmer Matrix No Topic Program purpose Date */ : : : : : : bmi.cpp Alif 2006174538 Exercise 1 This program to calculate bmi for a user. 20 October 2013 #include <iostream.h> #include <math.h> void main() { double weight, height,bmi; cout << "Enter weight in kg: "; cin >> weight; Function Body cout << "Enter height in meter: "; cin >> height; bmi = weight /pow(height,2); cout << "The BMI is : "<<bmi<<endl; End block } Example of C++ program: Elements Of Computer Program Comments • Important part of a program's documentation. • It is used to explain the purpose of a program. • Explain the parts of program and keep note regarding changes to the source code. • Describe about the reason of the program and understand the program more clearly. • Increased the user understanding of the program. • Store the names of programmers for future reference. • Used to make program documentation. Comments • Can be used in 2 ways: 1. Using /* */ symbol • Used for one or more than one line of comments in group • Example: /* This is the example of comment in C++ program.This is the second line */ Comments 2. Using // symbol • Used only for one line • Example: // // This is example of comments This is the second line Preprocessor Directive • A line of code that begins with the # symbol. • Are processed by preprocessor (which is usually bundled into the compiler) before compilation. • Are not executable code lines but instructions to the compiler. • Each header file stores functions that are related to a particular application. • The directive #include <iostream.h> tells the preprocessor to include the contents of the file iostream.h, which includes input-output operations (such as printing to the screen). Function Main Function • Every C++ program contains one or more functions, one of which must be named main. • Every C++ program has a main function. • A function is a block of code that carries out a specific task. • The word main is followed in the code by a pair of parentheses ( ). Function Braces • Right after these parentheses we can find the body of the main function enclosed in braces { }. • What is contained within these braces is what the function does when it is executed. • Braces used to mark the beginning and end of blocks of code. • The open braces ({ ) are place in the beginning of code after the main functions and close braces ( }) are used to show closing of code. • The code after the ( }) will not be read/evaluate by compiler Function Statement • A function consists of a sequence of statements that perform the work of the function. • It consists of instructions or command which make the program work. • Each statement in C++ ends with a semicolon (;). • There are 3 type of statement : – Input statement- cin>>statement; – Output statement-cout<<“statement”; – Operation statement-mathematical operation Problem Solving Phases To be a good programmer, you must be a good problem solver. To be a good problem solver, you must understand a problem in a methodical way, from initial inspection and definition to final solution. Problem Solving Phases Program Development Life Cycle • Creating new program is called program development • The process associated with creating successful applications programs is called the Program Development Life Cycle (PDLC). • There are 5 steps to Program Development Life Cycle: Analysis Design Implementation/ Coding Testing/ Debugging Program Maintenance Problem Solving Phases Phase 1: Analysis • Done by reviewing the program specifications. – Eliminate ambiguities in the problem statement. – Other criteria must be identified especially the data that will be used as input, process involve and output that will be produced. • Indicating what the new system should do. • In this step, the objectives, outputs, inputs, and processing requirements are determined. • The program objectives are the problems that you are trying to solve. Problem Solving Phases Phase 1: Analysis Example of Problem Solving Exercise • Write a program to calculate the bmi of a user Objective : - To calculate the bmi of a user Output : - bmi Input : - weight , height Process : - Declare double bmi, weight, height - Calculate bmi= weight/pow(height,2) Problem Solving Phases Phase 2: Design • Develop a series of steps with a logical order which, when applied would produce the output of the problem. • Good program design is very important in order to make the development process run smoothly, as well as to make future revisions easier. • In program design step, a solution is created using structured programming techniques such as top-down approach and algorithm which consist of pseudocode and flowchart. Problem Solving Phases Phase 2: Design Algorithm Problem Solving Phases Phase 2: Design What is Algorithm? • A step-by-step problem solving process in which a solution is arrived at a finite amount of time. • A sequence of a finite number of steps arranged in a specific logical order which when executed will produce the solution for that problem. • An algorithm must satisfy some requirements which are: – Unambiguousness (clear) – Generality (unspecific) – Correctness – Finiteness (limitation) Problem Solving Phases Phase 2: Design Algorithm Pseudo code Flowchart Problem Solving Phases Phase 2: Design Algorithm – Pseudocode • Is a semiformal, English like language with a limited vocabulary that can be used to design and describe algorithms. • Each pseudo code statement includes keywords that describe operations and some appropriate English like description of operands. • Each pseudo code statement should be written on a separate line. Problem Solving Phases Phase 2: Design Algorithm – Pseudocode • The statements that make up a sequence control structure should begin with unambiguous words such as compute, set and initialize. • For the selection control structure use an if-else statement. • For repetition control structure use the for /while /do..while statement and indent the loop body. • All words in a pseudo code statement must be unambiguous and as easy as possible for nonprogrammers to understand. Problem Solving Phases Phase 2: Design Algorithm – Pseudocode • Example: (Sequential Control Structure) – calculate the bmi of a user BEGIN DECLARE double bmi, weight, height READ weight, height CALCULATE bmi=weight/pow(height,2) DISPLAY bmi Problem Solving Phases Phase 2: Design Algorithm – Pseudocode • Example: (Selection Control Structure) – Identify whether a number is a positive or negative number BEGIN DECLARE int num GET num IF (num>0) DISPLAY positive number ELSE DISPLAY negative number END Problem Solving Phases Phase 2: Design Algorithm – Pseudocode • Example: (Repetition Control Structure) • Get three numbers and find the total of the three numbers BEGIN DECLARE int num, n, total INITIALIZE total=0, n=1 WHILE (n<=3) GET num; total=total+num; n++; DISPLAY total END Problem Solving Phases Phase 2: Design Algorithm – Flowchart • A graphic presentation of the detailed logical sequence of steps needed to solve programming problems. • Uses geometric symbols and familiar relational operators to provide a graphic display of the sequence of steps involved in a program. • The steps in a flowchart follow each other in the same logical sequence as their corresponding program statements will follow in a program. • Different symbols are used to represent different actions, such as start/stop, decision, input/output, processing, and looping symbols. Problem Solving Phases Phase 2: Design Algorithm – Flowchart Symbol Name Description Flowline To connect symbols and indicate the flow of logic Terminal Used to represent the beginning (Start) or the end (End) of a program Input/Output Processing Decision Used for input and output operations, such as reading and printing. Used for arithmetic and data manipulation operations. Used for any logic or comparisons operations. This symbol has one entry and two exit paths. The path chosen depend on whether the answer to question is “yes” or “no” Connector Used to join different flowlines Offpage Connector Used to indicate that the flowchart continues to next page Problem Solving Phases Phase 2: Design Algorithm – Flow Chart Example: Sequential Control Structure Calculate the bmi of a user Problem Solving Phases Phase 2: Design Algorithm – Flow Chart Example: Selection Control Structure • Identify whether a number is a positive or negative number Problem Solving Phases Phase 2: Design Algorithm – Flow Chart Example: Repetition Control Structure • Get three numbers and find the total of the three numbers Problem Solving Phases Phase 2: Design Algorithm – Flow Chart Example: Function Calculate the area of a room by using 2 functions: getInput()and calculate() Problem Solving Phases Phase 2: Design Algorithm – Flow Chart Example: using off page connector Problem Solving Phases Phase 3: Implementation/Coding • The pseudo code or flow diagram will be converted to a program by using a certain programming language (eg.: BASIC, Java, C++). • Coding is the actual process of creating the program in a programming language. • The coded program is referred to as source code. • To be executed, the program is converted by the computer to object code using a special program (compiler or interpreter) Problem Solving Phases Phase 4: Testing/Debugging • Check and verify for the correctness of the result. • Debugging is the process of making sure a program is free of errors, or “bugs.” • Preliminary debugging begins after the program has been entered into the computer system. • The process whereby we will correct the error and run the program successfully Problem Solving Phases Phase 5: Maintenance • Virtually every program, if it is to last a long time, requires ongoing maintenance. • Process of updating software so that it continues to be useful. • A costly process, but can be used to extend the life of a program. Question 1 Write a program to calculate pressure using the formula given. Analysis OUTPUT INPUT PROCESS Pseudocode Begin Declare double ALL variable GET/READ Patm , h , p , g CALCULATE p= Patm + hpg DISPLAY p END Flowchart Question 1- Answer Write a program to calculate pressure using the formula given. Analysis OUTPUT INPUT PROCESS P Patm, h,p,g P = Patm + hpg Pseudocode BEGIN DECLARE double P, Patm , Hpg GET Patm, Hpg CALCULATE P = Patm + Hpg DISPLAY P END Flowchart START DECLARE double P, Patm , Hpg GET Patm, Hpg CALCULATE P=Patm + Hpg DISPLAY P END Question 2 Write the flowchart for the pseudocode given below: Answer : Flowchart START DECLARE int side1, side2, area GET side1, side2 CALCULATE area = side1 * side2 DISPLAY area END Question 3 Write pseudocode for the flowchart given below: Pseudocode : ANSWER BEGIN DECLARE char Name[50]; double Hours, Rate, Pay INPUT Name, Hours, Rate CALCULATE Pay = Hours * Rate DISPLAY Name, Pay END Question 4 Draw a flowchart based on the pseudocode given below: BEGIN DECLARE double b,h ,A GET b,h COMPUTE A=b*h PRINT A,b,h END (Hint: A-Area of parallelogram, b-base,h-height) Formula: A=b*h Flowchart : Answer BEGIN DECLARE double b, h, A GET b,h COMPUTE A=b*h PRINT A,b,h END Question 5 A retail store grants its customers a maximum amount of credit. Each customer’s available credit is his or her maximum amount of credit minus the amount of credit used. Write a pseudo code and flowchart algorithm for a program that asks for a customer’s maximum amount of credit and amount of credit used. The program should then display the customer’s available credit. Answer: Program Analysis / Specification OUTPUT availableCredit INPUT creditLimit , creditUsed PROCESS Available = limit - used Question 5 : Answer A retail store grants its customers a maximum amount of credit. Each customer’s available credit is his or her maximum amount of credit minus the amount of credit used. Write a pseudo code and flowchart algorithm for a program that asks for a customer’s maximum amount of credit and amount of credit used. The program should then display the customer’s available credit. Program Analysis / Specification OUTPUT INPUT PROCESS balance maxAmount, amountUsed balance = maxAmount - amountUsed Pseudocode BEGIN DECLARE double balance, maxAmount, amountUsed READ maxAmount,amountUsed CALCULATE balance = maxAmount – amountUsed DISPLAY balance END Flowchart BEGIN DECLARE double balance, maxAmount, amountUsed READ maxAmount, amountUsed CALCULATE balance = maxAmount - amountUsed DISPLAY balance END