Download presentation
Presentation is loading. Please wait.
1
introduction to computer programs
Chapter 1 introduction to computer programs
2
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
3
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.
4
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
5
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
6
Types of Programming Language
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);
7
Example of Programming Language
A Brief History Of Programming Language Example of Programming Language machine language: BMI = weight/pow(height,2); // Load // Multiply // Store assembler compiler ASSEMBLY LANGUAGE LOAD STOR MULT ADD SUB MACHINE LANGUAGE 100100 100010 100110 100101 100011 High Level Language BMI = weight/pow(height,2);
8
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
9
Accepting data from a user
Basic Operation of a Computer Accepting data from a user Manipulating data Producing results INPUT PROCESS OUTPUT STORAGE Storing results ** These operations are under the control of instructions stored in the computer’s memory
10
Component of a Computer
HARDWARE SOFTWARE COMPUTER The series of instructions that tells the computer hardware how to perform tasks The electric, electronic and mechanical equipment that makes up a computer
11
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.
12
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.
13
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.
14
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.
15
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.
16
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).
17
The Importance Of Good Program
Indent style and spacing. Comparison Style 1 if (marks > 50) { return PASS; } else return FAIL; Style 2 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.
18
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.
19
Relationship Between Compilers, Interpreters, Assemblers and Program
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.
20
Relationship Between Compilers, Interpreters, Assemblers and Program
a computer program that translates programs written in a high-level language into machine code. translates each high-level instruction into several machine-code instructions all at once time. Compiler runs much faster than an interpreted program.
21
Relationship Between Compilers, Interpreters, Assemblers and Program
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.
22
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
23
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 ; }
24
Elements Of Computer Program
Example of C++ program: #include <iostream.h> compiler directive/header file void main( ) main function { //display greeting message comment cout<<”Welcome to CSC 128 class”; statement } OUTPUT Welcome to CSC 128 class braces
25
Example of C++ program:
Elements Of Computer Program Comments Preprocessor directives Function Begin block /* File Name : bmi.cpp Programmer : Alif Matrix No : Topic : Exercise 1 Program purpose : This program to calculate bmi for a user. Date : 20 October 2013 */ #include <iostream.h> #include <math.h> void main() { double weight, height,bmi; cout << "Enter weight in kg: "; cin >> weight; cout << "Enter height in meter: "; cin >> height; bmi = weight /pow(height,2); cout << "The BMI is : "<<bmi<<endl; } Function Body End block Example of C++ program:
26
Elements Of Computer Program
27
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.
28
Comments Can be used in 2 ways: 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 */
29
Comments 2. Using // symbol Used only for one line Example:
// This is example of comments // This is the second line
30
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).
31
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 ( ).
32
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
33
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
35
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.
36
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
37
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.
38
Example of Problem Solving Exercise
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)
39
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.
40
Problem Solving Phases Phase 2: Design
Algorithm
41
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)
42
Problem Solving Phases Phase 2: Design Algorithm
Pseudo code Flowchart
43
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.
44
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.
45
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
46
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
47
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
48
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.
49
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 Used for input and output operations, such as reading and printing. Processing Used for arithmetic and data manipulation operations. Decision 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
50
Problem Solving Phases Phase 2: Design
Algorithm – Flow Chart Example: Sequential Control Structure Calculate the bmi of a user
51
Problem Solving Phases Phase 2: Design
Algorithm – Flow Chart Example: Selection Control Structure Identify whether a number is a positive or negative number
52
Problem Solving Phases Phase 2: Design
Algorithm – Flow Chart Example: Repetition Control Structure Get three numbers and find the total of the three numbers
53
Problem Solving Phases Phase 2: Design
Algorithm – Flow Chart Example: Function Calculate the area of a room by using 2 functions: getInput()and calculate()
54
Problem Solving Phases Phase 2: Design
Algorithm – Flow Chart Example: using off page connector
55
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)
56
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
57
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.
58
Question 1 Write a program to calculate pressure using the formula given. Analysis OUTPUT INPUT PROCESS
59
Declare double ALL variable GET/READ Patm , h , p , g
Pseudocode Begin Declare double ALL variable GET/READ Patm , h , p , g CALCULATE p= Patm + hpg DISPLAY p END
60
Flowchart
61
Question 1- Answer P Patm, h,p,g P = Patm + hpg
Write a program to calculate pressure using the formula given. Analysis OUTPUT INPUT PROCESS P Patm, h,p,g P = Patm + hpg
62
DECLARE double P, Patm , Hpg GET Patm, Hpg CALCULATE P = Patm + Hpg
Pseudocode BEGIN DECLARE double P, Patm , Hpg GET Patm, Hpg CALCULATE P = Patm + Hpg DISPLAY P END
63
Flowchart START DECLARE double P, Patm , Hpg CALCULATE P=Patm + Hpg
DISPLAY P END CALCULATE P=Patm + Hpg GET Patm, Hpg
64
Question 2 Write the flowchart for the pseudocode given below:
65
CALCULATE area = side1 * side2
Answer : Flowchart START DECLARE int side1, side2, area DISPLAY area END CALCULATE area = side1 * side2 GET side1, side2
66
Question 3 Write pseudocode for the flowchart given below:
67
CALCULATE Pay = Hours * Rate DISPLAY Name, Pay END
Pseudocode : ANSWER BEGIN DECLARE char Name[50]; double Hours, Rate, Pay INPUT Name, Hours, Rate CALCULATE Pay = Hours * Rate DISPLAY Name, Pay END
68
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
69
Flowchart : Answer BEGIN END DECLARE double b, h, A GET b,h COMPUTE
A = b * h PRINT A,b,h DECLARE double b, h, A BEGIN END
70
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 INPUT PROCESS availableCredit creditLimit , creditUsed Available = limit - used
71
balance = maxAmount - amountUsed
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
72
Pseudocode BEGIN DECLARE double balance, maxAmount, amountUsed READ maxAmount,amountUsed CALCULATE balance = maxAmount – amountUsed DISPLAY balance END
73
Flowchart BEGIN END DECLARE double balance, maxAmount, amountUsed
READ maxAmount, amountUsed CALCULATE balance = maxAmount - amountUsed DISPLAY balance DECLARE double balance, maxAmount, amountUsed BEGIN END
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.