Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages

Similar presentations


Presentation on theme: "Programming Languages"— Presentation transcript:

1 Programming Languages
Chapter 1: An Overview of Computers and Programming Languages Ed. 8 Credits go to © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

2 Objectives In this chapter you will:
Learn about different types of computers Explore the hardware and software components of a computer system Learn about the language of a computer Learn about the evolution of programming languages Examine high-level programming languages Discover what a compiler is and what it does Examine a C++ program Explore how a C++ program is processed Learn what an algorithm is and explore problem-solving techniques Become aware of structured design and object-oriented design programming methodologies Become aware of Standard C++ and ANSI/ISO Standard C++, C++11, and C++14

3 Without software, a computer is useless
Introduction Without software, a computer is useless Software is developed with programming languages C++ is a programming language C++ is suited for a wide variety of programming tasks

4 A Brief Overview of the History of Computers
Early calculation devices Abacus – invented in Asia, used Babylon, China & Europe Pascaline Leibniz device Jacquard’s weaving looms Babbage machines: difference and analytic engines Hollerith machine Early computer-like machines Mark I – IBM & Harvad University, ‘44, weighted 50 tons, 18,000 tubes Electronic Numerical Integrator and Calculator (ENIAC) The computer we know today are based on Von Neumann architecture in late ‘40s Universal Automatic Computer (UNIVAC) – ‘51 Transistors and microprocessors

5 Categories of Computers
Mainframe computers Midsize computers Microcomputers personal computers (PC) – 1st Apple computer ‘77 Stephen Wozniak & Steven Jobs, PC - IBM ‘81 PDAs, pocket computers Cell phones, mobile phones ipods combined with PDAs and phones

6

7 CPU (Central Processing Unit)
CU (Control Unit): Fetches and decodes instructions Controls flow of information in and out of MM (Main Memory) Controls operation of internal CPU components PC (program counter): points to next instruction to be executed

8 CPU (Central Processing Unit) (continued)
IR (instruction register): holds instruction currently being executed ALU (arithmetic logic unit): carries out all arithmetic and logical operations

9 Main Memory or Random Access Memory - RAM
Directly connected to the CPU All programs must be loaded into main memory before they can be executed All data must be brought into main memory before it can be manipulated When computer power is turned off, everything in main memory is lost

10

11 Secondary storage: Device that stores information permanently
Examples of secondary storage: Hard disks USB flash drive CD-ROMs SD - (Secure Digital Card) is an ultra small flash memory carddesigned to provide high-capacity memory in a small size. SD cardsare used in many small portable devices such as digital video camcorders, digital cameras, handheld computers, audio players and mobile phones. Floppy disks Zip disks Tapes

12 Input devices feed data and programs into computers - Include:
Input/Output Devices Input devices feed data and programs into computers - Include: Keyboard Mouse Scanner Camera Secondary storage Output devices display results. They include: Monitor Printer

13 Software Software: Programs that do specific tasks
System programs take control of the computer, such as an operating system Operating system monitors the overall activity of the computer and provides services such as: Memory management Input/output activities Storage management Application programs perform a specific task Word processors Spreadsheets Games

14 The Language of a Computer
Analog signals: varying continuous wave forms Digital signals are sequences of 0s and 1s Machine language: language of a computer A sequence of 0s and 1s Binary digit (bit): The digit 0 or 1 Binary code: Byte: A sequence of eight bits

15 The Language of a Computer
TABLE 1-1 Binary Units Unit Symbol Bits/Bytes Byte 8 bits Kilobyte KB 210 bytes = 1024 bytes Megabyte MB 1024 KB = 210 KB = 220 bytes = 1,048,576 bytes Gigabyte GB 1024 MB = 210 MB = 230 bytes = 1,073,741,824 bytes Terabyte TB 1024 GB = 210 GB = 240 bytes = 1,099,511,627,776 bytes Petabyte PB 1024 TB = 210 TB = 250 bytes = 1,125,899,906,842,624 bytes Exabyte EB 1024 PB = 210 PB = 260 bytes = 1,152,921,504,606,846,976 bytes Zettabyte ZB 1024 EB5 210 EB = 270 bytes = 1,180,591,620,717,411,303,424 bytes © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

16 Coding Schemes ASCII (American Standard Code for Information Interchange) – Appendix C 128 characters A is encoded as (66th character) 3 is encoded as Number systems The decimal system (base 10) is used in our daily life The computer uses the binary (or base 2) number system

17 Coding Schemes (continued)
EBCDIC Used by IBM 256 characters Unicode 65536 characters Two bytes (16 bits) are needed to store a character.

18 Programming Language Evolution
Early computers were programmed in machine language To calculate wages = rates * hours in machine language: // Load rates // Multiply by hours // Store wages

19 Assembly language instruction are mnemonic
Assembler: translates a program written in assembly language into machine language wages = rates * hours in machine language: // Load-LOAD rates // Multiply-MULT by hours // Store-STOR wages

20 Program Example Using the assembly language instructions, the equation wages = rates * hours can be written as follows: LOAD rate MULT hour STOR wages

21 The equation wages = rate * hours can be written in C++ as:
High-Level Languages High-level languages include Basic, FORTRAN, COBOL, C, C++, C, C#, Java, Pascal and Python Compiler: translates a program written in a high- level language machine language The equation wages = rate * hours can be written in C++ as: wages = rate * hours;

22 Processing a C++ Program (1 of 4)
#include <iostream> using namespace std; int main() { cout << "My first C++ program." << endl; return 0; } Sample Run: My first C++ program.

23 Processing a C++ Program (2 of 4)
Steps needed to process a C++ program Use a text editor to create the source code (source program) in C++ Include preprocessor directives Begin with the symbol # and are processed by the preprocessor Use the compiler to: Check that the program obeys the language rules Translate the program into machine language (object program) Use an integrated development environment (IDE) to develop programs in a high-level language Programs such as mathematical functions are available The library contains prewritten code you can use A linker combines object program with other programs in the library to create executable code The loader loads executable program into main memory The last step is to execute the program

24 Processing a C++ Program (3 of 4)
IDEs are quite user friendly Compiler identifies the syntax errors and also suggests how to correct them Build or Rebuild is a simple command that links the object code with the resources used from the IDE

25 Processing a C++ Program (4 of 4)
Steps needed to process a C++ program Use a text editor to create the source code (source program) in C++ Include preprocessor directives Begin with the symbol # and are processed by the preprocessor Use the compiler to: Check that the program obeys the language rules Translate the program into machine language (object program) Use an integrated development environment (IDE) to develop programs in a high-level language Programs such as mathematical functions are available The library contains prewritten code you can use A linker combines object program with other programs in the library to create executable code The loader loads executable program into main memory The last step is to execute the program FIGURE 1-2 Processing a C++ program © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use.

26 To execute a program written in a high-level language such as C++
Processing a Program To execute a program written in a high-level language such as C++ 1. Use an editor to create a source program in C++ 2. Use the compiler to Check that the program obeys the rules Translate into machine language (object program)

27 Processing a Program (continued)
3. Software Development Kit (SDK) may be used to create a program Linker: Combines object program with other programs provided by the SDK to create executable code 4. Loader: Loads executable program into main memory 5. The last step is to execute the program

28 Three C++ Program Stages
myprog.cpp myprog.obj myprog.exe SOURCE OBJECT EXECUTABLE written in machine language C++ via compiler via linker other code from libraries, etc.

29 Programming is a process of problem solving Problem solving techniques
Analyze the problem Outline the problem requirements Design steps (algorithm) to solve the problem Algorithm: Step-by-step problem-solving process - series of actions in a specific order Solution achieved in finite amount of time Example: "Rise and Shine" algorithm Get out of bed, take off pajamas, take a shower, get dressed, eat breakfast, carpool to work

30 Problem Solving Process
Step I - Analyze the problem Outline the problem and its requirements Design steps (algorithm) to solve the problem Step II - Implement the algorithm Implement the algorithm in code Verify that the algorithm works Step III - Maintenance Use and modify the program if the problem domain changes

31 Step 1. Analyze the Problem
Thoroughly understand the problem Understand problem Step 1 requirements Does program require user interaction? Does program manipulate input data? What is the output looks like? If the problem is complex, divide it into subproblems Analyze each subproblem as above Step 1

32 Step 2. Algorithms Design
Algorithm is a step by step procedure for solving a problem in terms of Series of actions in specific order The actions executed The order in which actions execute Example: "Rise and Shine" algorithm Get out of bed, take off pajamas, take a shower, get dressed, eat breakfast, carpool to work Experience has shown that the most difficult part of solving a problem on a computer is developing the algorithm for the solution. Program control (flow of control) Specifying the order in which actions (statements or instructions) execute Control structures help specify this order

33 Program flowchart (programming logic)
Graphical representation of an algorithm. The detailed flowchart provides a detailed picture of a process by mapping all of the steps and activities that occur in the process. Provides an essential tool to understanding programming logic Designing a flowchart helps the coding easier. Drawn using symbols connected by arrows called flowlines Rectangle symbol (action symbol) Indicates any type of action Diamond symbol (decision symbol) Indicates that a decision to be made Oval symbol: Indicates beginning or end of a program, or a section of code (circles) Indicates a portion of an algorithm add grade to total add 1 to counter total = total + grade; counter = counter + 1;

34 Flowchart Flowchart of sequence structure: Two actions performed in order When drawing a portion, small circles is used add grade to total add 1 to counter total = total + grade; counter = counter + 1; The program flowchart can be likened to the blueprint of a building. As we know a designer draws a blueprint before starting construction on a building. Similarly, a programmer prefers to draw a flowchart prior to writing a computer program. As in the case of the drawing of a blueprint, the flowchart is drawn according to defined rules and using standard flowchart symbols prescribed by the American National Standard Institute, Inc. See detail Flow Chart Definition. Fig. 5.1 Flowcharting C#’s sequence structure. When flowcharting a complete algorithm Oval containing "Begin" is first symbol Oval containing "End" is last symbol When drawing a portion, small circles used Useful for algorithms.

35 Pseudocode Pseudocode (pronounced SOO-doh-kohd) is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled natural language rather than in a programming language.  Similar to everyday English Not actually executed on computers pseudocode is sometimes used as a detailed step in the process of developing a program a notation that combines some of the structure of a programming language, such as IF-ELSE and DO WHILE constructs, with a natural language, such as plain English. is an informal high-level  informal language of a computer program. artificial, informal language helps develop algorithms pseudocode is often used to “think out” a program during the program-design process Easy to convert into corresponding program language Consists only of executable statements Declarations are not executable statements Actions: input, output, calculation

36 Step 2. Design an Algorithm
If problem was broken into subproblems Design algorithms for each subproblem Check the correctness of algorithm Can test using sample data Some mathematical analysis might be required Step 2

37 Once the algorithm is designed and correctness verified
Step 3. Write the Code Once the algorithm is designed and correctness verified Write the equivalent code in high-level language use text editor Step 3

38 Step 4. Compiling and Linking
Run code through compiler If compiler generates errors Look at code and remove errors Run code again through compiler If there are no syntax errors Compiler generates equivalent machine code Linker links machine code with system resources (libraries) Step 4

39 Step 5. The Loader and Executing
Once compiled and linked, loader can place program into main memory for execution The final step is to execute the program Compiler guarantees that the program follows the rules of the language Does not guarantee that the program will run correctly Step 5

40 Various Types of Errors
Design errors occur when specifications are wrong Compile errors occur when syntax is wrong Run-time errors result from incorrect assumptions, e.g. x / 0 incomplete understanding of the programming language, or unanticipated user errors. count == count + 1 if (count = 20)

41 Design an algorithm to find the perimeter and area of a rectangle
Example Rectangle Design an algorithm to find the perimeter and area of a rectangle The perimeter and area of the rectangle are given by the following formulas: perimeter = 2 * (length + width) area = length * width

42 Algorithm (in pseudocode):
Example 1-1 Algorithm (in pseudocode): Get length of the rectangle Get width of the rectangle Find the perimeter using the following equation: perimeter = 2 * (length + width) 4. Find the area using the following equation: area = length * width

43 Example 1- 2 Algorithm (in pseudocode): Every salesperson has a base salary Salesperson receives $10 bonus at the end of the month for each year worked if he or she has been with the store for five or less years The bonus is $20 for each year that he or she has worked there if over 5 years

44 Example 1-3 (continued) Additional bonuses are as follows: If total sales for the month are $5000- $10000, he or she receives a 3% commission on the sale If total sales for the month are at least $10000, he or she receives a 6% commission on the sale

45 Algorithm (in pseudocode): Get baseSalary Get noOfServiceYears
Example 1-3 (continued) Algorithm (in pseudocode): Get baseSalary Get noOfServiceYears Calculate bonus using the following formula: if (noOfServiceYears is less than or equal to five) bonus = 10 * noOfServiceYears otherwise bonus = 20 * noOfServiceYears 4. Get totalSale

46 5. Calculate additionalBonus as follows:
Example 1-3 (continued) 5. Calculate additionalBonus as follows: if (totalSale is less than 5000) additionalBonus = 0 otherwise if(totalSale is >= to 5000 and totalSale < 10000) additionalBonus = totalSale · (0.03) additionalBonus = totalSale · (0.06) 6. Calculate payCheck using the equation payCheck = baseSalary + bonus + additionalBonus

47 Calculate each student’s grade
Example 1-5 (1 of 4) Calculate each student’s grade There are 10 students in a class Each student has taken five tests Each test is worth 100 points Design algorithms to: Calculate the grade for each student and class average Find the average test score Determine the grade Use the provided data: students’ names and test scores

48 Algorithm to determine the average test score
Example 1-5 (2 of 4) Algorithm to determine the average test score Get the five test scores Add the five test scores The sum of the test scores is represented by sum Suppose average stands for the average test score: average = sum / 5;

49 Algorithm to determine the grade:
Example 1-5 (3 of 4) Algorithm to determine the grade: if average is greater than or equal to 90 grade = A otherwise if average is greater than or equal to 80 and less than 90 grade = B if average is greater than or equal to 70 and less than 80 grade = C if average is greater than or equal to 60 and less than 70 grade = D grade = F

50 Main algorithm (in pseudocode) is presented below:
Example 1-5 (4 of 4) Main algorithm (in pseudocode) is presented below: totalAverage = 0; Repeat the following for each student: Get student’s name Use the algorithm to find the average test score Use the algorithm to find the grade Update totalAverage by adding current student’s average test score Determine the class average as follows: classAverage = totalAverage / 10

51 Structured Programming
Structured design: Dividing a problem into smaller subproblems Structured programming Implementing a structured design The structured design approach is also called Top-down design Stepwise refinement Modular programming

52 Structured Programming
Stacked building blocks Overlapping building blocks (illegal in structured programs) Nested building blocks Combination of control structures: Stacking Placing one after another Nesting Inserting of one structure into another Fig. Stacked, nested and overlapped building blocks.

53 Object-Oriented Programming
Identify components called objects Each object consists of data and operations on that data Specify relevant data and possible operations to be performed on that data An object combines data and operations on the data into a single unit To create operations: Write algorithms and implement them in a programming language

54 OBJECT Operations Data What is an object? set of functions
internal state Operations Data

55 Object-Oriented Programming (continued)
A programming language that implements OOD (Design) is called an object-oriented programming (OOP) language Before: learn how to represent data in computer memory, how to manipulate data, and how to implement operations then write algorithms and implement them in a programming language

56 Object-Oriented Design
A technique for developing a program in which the solution is expressed in terms of objects - self- contained entities composed of data and operations on that data. cin cout >> << get Private data setf Private data . . ignore setw 56

57 Object-Oriented Programming (continued)
Learn how to combine data and operations on the data into a single unit called an object C++ was designed to implement OOD OOD is used in conjunction with structured design

58 Two Programming Methodologies
Functional Object-Oriented Decomposition Design OBJECT Operations Data FUNCTION FUNCTION OBJECT Operations Data OBJECT Operations Data FUNCTION

59 An object contains data and operations
checkingAccount OpenAccount Private data: accoutNumber balance WriteCheck MakeDeposit IsOverdrawn GetBalance

60 C++ designed by Bjarne Stroustrup at Bell Laboratories in early 1980s
ANSI/ISO STANDARD C++ C++ evolved from C C++ designed by Bjarne Stroustrup at Bell Laboratories in early 1980s C++ programs were not always portable from one compiler to another, so In mid-1998, ANSI/ISO C++ language standards were approved the book focuses on the latest syntax of ANSI/ISO C++ Second standard (the C++ code cleaner and more effective) called C++11, was approved in 2011 New data type long long to deal with large integers Auto declaration of variables using initialization statements Enhancement the functionality of for loops effectively work with arrays and containers C++14 is an update over C++ was approved in 2014.

61 Computer system has hardware and software
Summary Computer: an electronic device that can perform arithmetic and logical operations Computer system has hardware and software Central processing unit (CPU): brain Primary storage (RAM) is volatile; secondary storage (e.g., disk) is permanent Operating system monitors the overall activity of the computer and provides services

62 Program flowchart (programming logic)
Summary Various kinds of languages, such as machine language, assembly, high-level Algorithm: step-by-step problem-solving process - series of actions in a specific order; solution in finite amount of time. Program flowchart (programming logic) Graphical representation of an algorithm. The problem-solving process has three steps: Analyze problem and design an algorithm Implement the algorithm in code Maintain the program

63 Object: data and operations on those data
Summary Structured design: Problem is divided into small subproblems Each subproblem is solved Combine solutions to all subproblems Object-oriented design (OOD): a program is a collection of interacting objects Object: data and operations on those data


Download ppt "Programming Languages"

Similar presentations


Ads by Google