Programming Languages

Slides:



Advertisements
Similar presentations
Chapter 1 An Overview of Computers and Programming Languages.
Advertisements

ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Lecture 1: Overview of Computers & Programming
Overview of Programming and Problem Solving ROBERT REAVES.
Chapter 1: An Overview of Computers and Programming Languages J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: An Overview of Computers and Programming Languages
Chapter 1: An Overview of Computers and Programming Languages
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 1: An Overview of Computers and Programming Languages.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 1 Introduction.
CHAPTER 1: AN OVERVIEW OF PROGRAMMING INSTRUCTOR: MOHAMMAD MOJADDAM How to Program in C++
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages Updated by: Dr\Ali-Alnajjar.
CHAPTER 1 AN OVERVIEW OF COMPUTERS AND PROGRAMMING LANGUAGES.
CS102 Introduction to Computer Programming
Chapter 1: An Overview of Computers and Programming Languages.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 1 Introduction to Computers and Programming.
Why Program? Computer – programmable machine designed to follow instructions Program – instructions in computer memory to make it do something Programmer.
Chapter Introduction to Computers and Programming 1.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
About the Presentations The presentations cover the objectives found in the opening of each chapter All chapter objectives are listed in the beginning.
Chapter 1 An Overview of Computers and Programming Languages.
EGR 2261 Engineering Problem Solving Using C and C++ Professor Nick Reeder.
Topics Introduction Hardware and Software How Computers Store Data
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Chapter 1: An Overview of Computers and Programming Languages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 1: An Overview of Computers and Programming Languages.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 1: An Overview of Computers and Programming Languages.
Overview of Programming and Problem Solving. Objectives In this chapter you will: Learn about different types of computers Explore the hardware and software.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 1: An Overview of Computers and Programming Languages.
Chapter 1: An Overview of Computers and Programming Languages
Chapter 1: An Overview of Computers and Programming Languages
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 1: An Overview of Computers and Programming Languages.
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 1 st semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
CHAPTER 1 INTRODUCTION 2 nd Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 1: An Overview of Computers and Programming Languages.
Chapter 1 : Overview of Computer and Programming By Suraya Alias
CS2301:Computer Programming 2
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages.
Chapter 1 An Overview of Computers and Programming Languages.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 1: An Overview of Computers and Programming Languages.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming
Chapter 1 Introduction 2nd Semester H
Chapter 1: An Overview of Computers and Programming Languages
Chapter 1: An Overview of Computers and Programming Languages
Topics Introduction Hardware and Software How Computers Store Data
Chapter 1: An Overview of Computers and Programming Languages
Chapter 1: An Overview of Computers and Programming Languages
Chapter 1: An Overview of Computers and Programming Languages
ICS103 Programming in C Lecture 1: Overview of Computers & Programming
Chapter 1: An Overview of Computers and Programming Languages
About the Presentations
Chapter 1: An Overview of Computers and Programming Languages
C++ Programming: From Problem Analysis to Program Design
Chapter 1: Introduction to Computers and Programming
Chapter 1: An Overview of Computers and Programming Languages
Chapter 1: An Overview of Computers and Programming Languages
Topics Introduction Hardware and Software How Computers Store Data
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
Chapter 1 Introduction(1.1)
Chapter 1: An Overview of Computers and Programming Languages
King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1 Done By: Asmal Alosaimi Edited By: Fatimah Alakeel.
Introduction to Computer Programming
Reminders Please turn off cell phones.
ICS103 Programming in C 1: Overview of Computers And Programming
Chapter 1: Introduction to Computers and Programming
Presentation transcript:

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.

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

Programming Language Evolution Early computers were programmed in machine language To calculate wages = rates * hours in machine language: 100100 010001 // Load rates 100110 010010 // Multiply by hours 100010 010011 // Store wages

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

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

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;

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.

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

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

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.

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)

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

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.

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

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

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

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

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;

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.

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

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

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

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

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

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)

Design an algorithm to find the perimeter and area of a rectangle Example 1-1 - 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

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

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

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

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

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

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

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;

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

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

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

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.

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

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

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

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

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

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

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

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.

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

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

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