+ All Categories
Home > Documents > CPS120 Introduction to Computer Science Exam Review.

CPS120 Introduction to Computer Science Exam Review.

Date post: 13-Jan-2016
Category:
Upload: donald-parks
View: 219 times
Download: 1 times
Share this document with a friend
121
CPS120 Introduction to Computer Science Exam Review
Transcript
Page 1: CPS120 Introduction to Computer Science Exam Review.

CPS120 Introduction to Computer Science

Exam Review

Page 2: CPS120 Introduction to Computer Science Exam Review.

Introduction To Computers

Page 3: CPS120 Introduction to Computer Science Exam Review.

5

A mental model that removes complex details

This is a key concept.

Abstraction

Page 4: CPS120 Introduction to Computer Science Exam Review.

System Components

• Hardware – I/O devices (input/output)

• keyboard, mouse, monitor, etc.

– CPU– Primary Storage– Secondary Storage

• Software

Page 5: CPS120 Introduction to Computer Science Exam Review.

Stored-Program Concept

Figure 5.1 The von Neumann architecture

Page 6: CPS120 Introduction to Computer Science Exam Review.

The Fetch-Execute Cycle

• Fetch the next instruction

• Decode the instruction

• Get data if needed

• Execute the instruction

Page 7: CPS120 Introduction to Computer Science Exam Review.

CPU (Central Processing Unit)

• Processes instructions, performs calculations, and manages the flow of information

• Performance:– CPU speed is a major factor in determining

how fast a computer operates – Each generation of CPU is more powerful

than the one before. It's clock cycles are faster at a given speed

Page 8: CPS120 Introduction to Computer Science Exam Review.

Control Unit

• Control unit is the organizing force in the computer

• There are two registers in the control unit– The instruction register (IR) contains the

instruction that is being executed– The program counter (PC) contains the

address of the next instruction to be executed

Page 9: CPS120 Introduction to Computer Science Exam Review.

Arithmetic/Logic Unit

• Performing basic arithmetic operations such as adding

• Performing logical operations such as AND, OR, and NOT

• Most modern ALUs have a small amount of special storage units called registers

Page 10: CPS120 Introduction to Computer Science Exam Review.

BUS

• Electronic pathway that carries information between devices in a computer– Bus Width (think lanes of a highway)– Bus Speed (think speed limit)

Page 11: CPS120 Introduction to Computer Science Exam Review.

Flow of Information

• The parts are connected to one another by a collection of wires called a bus

Data flow through a von Neumann architecture

Page 12: CPS120 Introduction to Computer Science Exam Review.

Processing

• Random Access Memory – Temporarily stores data inside a computer– Constantly overwritten

– Dynamic RAM is type of memory chip that makes up memory in many computer systems.

• If you have limited memory or you have many programs open, your computer may need to use part of the hard drive to simulate more memory

Page 13: CPS120 Introduction to Computer Science Exam Review.

Memory

• Memory is a collection of cells, each with a unique physical address

Page 14: CPS120 Introduction to Computer Science Exam Review.

Storage Devices

• Primary storage – RAM (Random Access Memory, or just “memory” for

short) • active data that is being stored, lost if power is cut off so save

often

– ROM (Read-Only Memory) • commands that are permanently stored on chips • PC’s use BIOS, Macs use the Toolbox

• Secondary storage – Hard disk, floppy disks – Slower method of storage & retrieval but “safer”

Page 15: CPS120 Introduction to Computer Science Exam Review.

Main Memory / Primary Storage

• Speeds up computer by storing data the computer has recently used– Internal Cache: On CPU chip (L1 or primary cache– External Cache: On the system board and consists of

Static RAM (SRAM chips)• Sometimes built into chip

• RAM – slower main memory– Each time the computer requests data from RAM, the

computer places a copy of the data in memory cache• Constantly contains the most recently used data

Page 16: CPS120 Introduction to Computer Science Exam Review.

RAM and ROM

• RAM stands for Random Access Memory– Inherent in the idea of being able to access each

location is the ability to change the contents of each location

• ROM stands for Read Only Memory– The contents in locations in ROM cannot be changed

• RAM is volatile, ROM is not– This means that RAM does not retain its bit

configuration when the power is turned off, but ROM does

Page 17: CPS120 Introduction to Computer Science Exam Review.

Using Memory Cache

1. Look through documents on your desk (internal cache)

2. Look through documents in your desk drawer (external cache)

3. Looking through documents in your filing cabinet (RAM)

Page 18: CPS120 Introduction to Computer Science Exam Review.

Ports

• Ports are connectors at the back of a computer system that you use to plug in an external device. This allow instructions and data to flow between the computer and the device

Page 19: CPS120 Introduction to Computer Science Exam Review.

Secondary Storage Devices

• Because most of main memory is volatile and limited, it is essential that there be other types of storage devices where programs and data can be stored when they are no longer being processed

Page 20: CPS120 Introduction to Computer Science Exam Review.

Hard Drives

• Magnetically stores data on rotating disks called platters

• Stores your operating system and programs• Document will not be lost when you turn off

the computer• Capacity is measured in bytes• Speed is measured in revolutions per minute

Page 21: CPS120 Introduction to Computer Science Exam Review.

CD-R & CD-RW Drives

• CD-R (Compact Disc – Recordable)– Permanent store; not erasable

• CD-RW – Can be erased and rewritten

– Read / Write and Re-write speeds

– Usually 74 minutes

• Special software needed to record onto a disk– Referred to as burning a CD

Page 22: CPS120 Introduction to Computer Science Exam Review.

DVD-ROM Drive

• DVD – Digital Versatile Disk– Generally cannot change the information stored on a

disk

– Similar to CD but stores more information

– Speed determines transfer rate (6 X)

– Capacity• 1 side / 1 layer – 4.7 GB

• 1 side / 2 layer – 8.5 GB

• 2 sides / 1 layer – 9.4 GB

• 2 sides / 2 layers – 17 GB

Page 23: CPS120 Introduction to Computer Science Exam Review.

Databases

• Organized in: – Tables

• Collection of information about a specific topic

– Field• Category of information

– Record• Single instance of information

Page 24: CPS120 Introduction to Computer Science Exam Review.

Operating Systems

• Software that controls the overall activity of the computer– Ensure that all of the parts of the computer

work together smoothly– Functions:

• Control hardware

• Run software

• Manage information

Page 25: CPS120 Introduction to Computer Science Exam Review.

Compilers

• An engine that works on your behalf to process instructions and allow you to deal with various basic rules of the language– The compiler’s job is to make sure you follow

the rules, to require that you provide enough information that the compiler can translate you instructions into languages the components can understand

Page 26: CPS120 Introduction to Computer Science Exam Review.

Interpreter vs. Compiler

• Interpreter– Translates instructions to machine code line-by-

line.

• Compiler– Translates the entire program to machine code

before running it.

Page 27: CPS120 Introduction to Computer Science Exam Review.

Computer Mathematics

Page 28: CPS120 Introduction to Computer Science Exam Review.

Representing Data

• The computer knows the type of data stored in a particular location from the context in which the data are being used; – i.e. individual bytes, a word, a longword, etc– 01100011 01100101 01000100 01000000

• Bytes: 99(10, 101 (10, 68 (10, 64(10

• Two byte words: 24,445 (10 and 17,472 (10

• Longword: 1,667,580,992 (10

Page 29: CPS120 Introduction to Computer Science Exam Review.

Alphanumeric Codes

• American Standard Code for Information Interchange (ASCII)– 7-bit code– Since the unit of storage is a bit, all ASCII

codes are represented by 8 bits, with a zero in the most significant digit

H e l l o W o r l d48 65 6C 6C 6F 20 57 6F 72 6C 64

• ASCII is a subset of the Unicode character set

Page 30: CPS120 Introduction to Computer Science Exam Review.

Decimal Equivalents

• Assuming the bits are unsigned, the decimal value represented by the bits of a byte can be calculated as follows:

1. Number the bits beginning on the right using superscripts beginning with 0 and increasing as you move left• Note: 20, by definition is 1

2. Use each superscript as an exponent of a power of 23. Multiply the value of each bit by its corresponding

power of 24. Add the products obtained

Page 31: CPS120 Introduction to Computer Science Exam Review.

Binary to Hex

• Step 1: Form four-bit groups beginning from the rightmost bit of the binary number– If the last group (at the leftmost position) has less than

four bits, add extra zeros to the left of the group to make it a four-bit group

• 0110011110101010100111 becomes• 0001 1001 1110 1010 1010 0111

• Step 2: Replace each four-bit group by its hexadecimal equivalent– 19EAA7(16

Page 32: CPS120 Introduction to Computer Science Exam Review.

Converting Decimal to Other Bases

• Step 1: Divide the number by the base you are converting to (r)

• Step 2: Successively divide the quotients by (r) until a zero quotient is obtained

• Step 3: The decimal equivalent is obtained by writing the remainders of the successive division in the opposite order in which they were obtained– Know as modulus arithmetic

• Step 4: Verify the result by multiplying it out

Page 33: CPS120 Introduction to Computer Science Exam Review.

Representing Signed Numbers• Remember, all numeric data is represented inside

the computer as 1s and 0s– Arithmetic operations, particularly subtraction raise the

possibility that the result might be negative

• Any numerical convention needs to differentiate two basic elements of any given number, its sign and its magnitude– Conventions

• Sign-magnitude

• Two’s complement

• One’s complement

Page 34: CPS120 Introduction to Computer Science Exam Review.

Representing Negatives

• It is necessary to choose one of the bits of the “basic unit” as a sign bit– Usually the leftmost bit– By convention, 0 is positive and 1 is negative

• Positive values have the same representation in all conventions

• However, in order to interpret the content of any memory location correctly, it necessary to know the convention being used used for negative numbers

Page 35: CPS120 Introduction to Computer Science Exam Review.

Sign-Magnitude

• For a basic unit of N bits, the leftmost bit is used exclusively to represent the sign

• The remaining (N-1) bits are used for the magnitude

Page 36: CPS120 Introduction to Computer Science Exam Review.

Sign-magnitude Operations

• Addition of two numbers in sign-magnitude is carried out using the usual conventions of binary arithmetic– If both numbers are the same sign, we add their

magnitude and copy the same sign– If different signs, determine which number has

the larger magnitude and subtract the other from it. The sign of the result is the sign of the operand with the larger magnitude

Page 37: CPS120 Introduction to Computer Science Exam Review.

Two’s Complement Convention

• A positive number is represented using a procedure similar to sign-magnitude

• To express a negative number1. Express the absolute value of the number in binary2. Change all the zeros to ones and all the ones to zeros (called

“complementing the bits”)3. Add one to the number obtained in Step 2

– The range of negative numbers is one larger than the range of positive numbers

– Given a negative number, to find its positive counterpart, use steps 2 & 3 above

Page 38: CPS120 Introduction to Computer Science Exam Review.

Two’s Complement Operations

• Addition:– Treat the numbers as unsigned integers

• The sign bit is treated as any other number

– Ignore any carry on the leftmost position

• Subtraction– Treat the numbers as unsigned integers– If a "borrow" is necessary in the leftmost place,

borrow as if there were another “invisible” one-bit to the left of the minuend

Page 39: CPS120 Introduction to Computer Science Exam Review.

One’s Complement

• Devised to make the addition of two numbers with different signs the same as two numbers with the same sign

• Positive numbers are represented in the usual way

• For negatives– STEP 1: Start with the binary representation of the

absolute value– STEP 2: Complement all of its bits

Page 40: CPS120 Introduction to Computer Science Exam Review.

One's Complement Operations

– Treat the sign bit as any other bit– For addition, carry out of the leftmost bit is

added to the rightmost bit – end-around carry

Page 41: CPS120 Introduction to Computer Science Exam Review.

Data Representation

Page 42: CPS120 Introduction to Computer Science Exam Review.

Representing Data: Bits

• Computer only uses “on” & “off” within its circuits

• Binary number system – “On”, 1, high state of electricity – “Off”, 0, low state of electricity – Bits (0’s and 1’s)

Page 43: CPS120 Introduction to Computer Science Exam Review.

Data and Computers

• Computers are multimedia devices, dealing with a vast array of information categories. Computers store, present, and help us modify:– Numbers

– Text

– Audio

– Images and graphics

– Video

Page 44: CPS120 Introduction to Computer Science Exam Review.

Data and Computers

• Data compression–reducing the amount of space needed to store a piece of data.

• Compression ratio–is the size of the compressed data divided by the size of the original data.

• A data compression technique can be lossless, which means the data can be retrieved without losing any of the original information. Or it can be lossy, in which case some information is lost in the process of compaction.

Page 45: CPS120 Introduction to Computer Science Exam Review.

Analog and Digital Information

• Information can be represented in one of two ways: analog or digital. – Analog data is a continuous representation,

analogous to the actual information it represents.

– Digital data is a discrete representation, breaking the information up into separate elements.

Page 46: CPS120 Introduction to Computer Science Exam Review.

Representing Audio Information

• To digitize the signal we periodically measure the voltage of the signal and record the appropriate numeric value. The process is called sampling.

• In general, a sampling rate of around 40,000 times per second is enough to create a reasonable sound reproduction.

Page 47: CPS120 Introduction to Computer Science Exam Review.

Representing Images and Graphics

• Color is often expressed in a computer as an RGB (red-green-blue) value, which is actually three numbers that indicate the relative contribution of each of these three primary colors.

Page 48: CPS120 Introduction to Computer Science Exam Review.

Digitized Images and Graphics

• Digitizing a picture is the act of representing it as a collection of individual dots called pixels.

• The number of pixels used to represent a picture is called the resolution.

• The storage of image information on a pixel-by-pixel basis is called a raster-graphics format. – Several popular raster file formats including bitmap

(BMP), GIF, and JPEG.

Page 49: CPS120 Introduction to Computer Science Exam Review.

Vector Graphics

• Instead of assigning colors to pixels as we do in raster graphics, a vector-graphics format describe an image in terms of lines and geometric shapes.

• A vector graphic is a series of commands that describe a line’s direction, thickness, and color.

• The file size for these formats tend to be small because every pixel does not have to be accounted for.

Page 50: CPS120 Introduction to Computer Science Exam Review.

Keyword Encoding

• Frequently used words are replaced with a single character. For example:

Page 51: CPS120 Introduction to Computer Science Exam Review.

Run-Length Encoding• A single character may be repeated over and

over again in a long sequence. This type of repetition doesn’t generally take place in English text, but often occurs in large data streams.

• In run-length encoding, a sequence of repeated characters is replaced by a flag character, followed by the repeated character, followed by a single digit that indicates how many times the character is repeated.

Page 52: CPS120 Introduction to Computer Science Exam Review.

Huffman Encoding

• If we use only a few bits to represent characters that appear often and reserve longer bit strings for characters that don’t appear often, the overall size of the document being represented is small

Page 53: CPS120 Introduction to Computer Science Exam Review.

File Systems and Directories

Page 54: CPS120 Introduction to Computer Science Exam Review.

File Systems

• A file is a named collection of related data

• A file system is the logical view that an operating system provides so that users can manage information as a collection of files

• A file system is often organized by grouping files into directories

Page 55: CPS120 Introduction to Computer Science Exam Review.

Text and Binary Files

• In a text file the bytes of data are organized as characters from the ASCII or Unicode character sets

• A binary file requires a specific interpretation of the bits based on the information in the file

Page 56: CPS120 Introduction to Computer Science Exam Review.

Text and Binary Files

• The terms text file and binary file are somewhat misleading– They seem to imply that the information in a

text file is not stored as binary data– Ultimately, all information on a computer is

stored as binary digits

Page 57: CPS120 Introduction to Computer Science Exam Review.

An Analogy

Sequential-access data files compare to audio cassette tapes ASrandom-access data files compare to audio compact discs (CD's)

Page 58: CPS120 Introduction to Computer Science Exam Review.

Sequential Files

• Stores data as one long continuous piece of data• Each different element is stored next to the prior

piece of element• There are no "empty bytes" of disk space in

between consecutive pieces of data• To access a specific record (piece of data), though,

a program must read through all of the former pieces of data

Page 59: CPS120 Introduction to Computer Science Exam Review.

Random-Access Files

• A random-access external data file stores data into blocks of memory of equal size

• This may waste some disk space, it does speed up access times.

Page 60: CPS120 Introduction to Computer Science Exam Review.

File Protection

• In multiuser systems, file protection is of primary importance

• We don’t want one user to be able to access another user’s files unless the access is specifically allowed

• A file protection mechanism determines who can use a file and for what general purpose– Users are grouped as Owner, Group and World– Rights generally allow users to READ, WRITE and/or

EXECUTE

Page 61: CPS120 Introduction to Computer Science Exam Review.

Directory Trees

• A file system is often viewed as a directory tree• The directory at the highest level is called the root

directory• A directory of files can be contained within other

directorie– The directory containing another is usually called the

parent directory, and the one inside is called a subdirectory or child directory

– Directories at the same level of the tree are called siblings

Page 62: CPS120 Introduction to Computer Science Exam Review.

Databases

Page 63: CPS120 Introduction to Computer Science Exam Review.

The Relational Model• In a relational DBMS, the data items

and the relationships among them are organized into tables

– A table is a collection of records

– A record is a collection of related fields

– Each field of a database table contains a single data value

– Each record in a table contains the same fields

Page 64: CPS120 Introduction to Computer Science Exam Review.

Structured Query Language

• The Structured Query Language (SQL) is a comprehensive database language for managing relational databases

Page 65: CPS120 Introduction to Computer Science Exam Review.

SQL Data Manipulation

• SQL includes a syntax to update records with query and update commands

• These form the Data Manipulation Language (DML) part of SQL: – SELECT - extracts data from a database – UPDATE - updates data in a database – DELETE - deletes data from a database – INSERT - inserts new data into a database

Page 66: CPS120 Introduction to Computer Science Exam Review.

The Select Statement

• The SELECT statement selects columns of data from a database

• The tabular result is stored in a result table (called the result set)

Page 67: CPS120 Introduction to Computer Science Exam Review.

The WHERE Clause

• To conditionally select data from a table, a WHERE clause can be added to the SELECT statement with the following syntax:

– SELECT column FROM table WHERE column condition value

Page 68: CPS120 Introduction to Computer Science Exam Review.

The LIKE Condition

• The LIKE condition is used to specify a search for a pattern in a column.

• The syntax is like this: – SELECT column FROM table WHERE column

LIKE pattern• A "%" sign can be used to define wildcards (missing

letters in the pattern) both before and after the pattern.

Page 69: CPS120 Introduction to Computer Science Exam Review.

SQL Select Distinct

• The DISTINCT keyword is used to return only distinct (different) values.– SELECT DISTINCT column-name(s)

FROM table-name

Page 70: CPS120 Introduction to Computer Science Exam Review.

SQL Order By

• The ORDER BY clause is used to sort the rows.

• Example: To display the companies in alphabetical order:– SELECT Company, OrderNumber

FROM OrdersORDER BY Company

Page 71: CPS120 Introduction to Computer Science Exam Review.

SQL Data Definition

• The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted, links between tables defined and, and constraintsmposed between database tables.

Page 72: CPS120 Introduction to Computer Science Exam Review.

DDL Commands

• The most important DDL statements in SQL are: – CREATE TABLE - creates a new database

table – ALTER TABLE - alters (changes) a database

table – DROP TABLE - deletes a database table – CREATE INDEX - creates an index (search

key) – DROP INDEX - deletes an index

Page 73: CPS120 Introduction to Computer Science Exam Review.

Gates and Boolean Logic

Page 74: CPS120 Introduction to Computer Science Exam Review.

Decision Making In Computers

• A circuit quite simply allows one out of two choices to be made depending on its inputs

• When decisions are made in a computer program, they are simply the result of a computation in which the final result is either TRUE or FALSE

• The value zero (0) is considered to be FALSE. Any positive or negative value is considered to be TRUE (usually represented by 1)

Page 75: CPS120 Introduction to Computer Science Exam Review.

Boolean Logic

• If you have ever taken a True or False test, you have used Boolean logic. – In the Boolean system an object can exist in

only one of two states, there is no third choice

• This is a central concept in programming.

Page 76: CPS120 Introduction to Computer Science Exam Review.

Gates and Circuits

• A gate is a device that performs a basic operation on electrical signals

• Gates are combined into circuits to perform more complicated tasks

Page 77: CPS120 Introduction to Computer Science Exam Review.

Representations

• There are three different, but equally powerful, notational methods for describing the behavior of gates and circuits– Boolean expressions– logic diagrams– truth tables

Page 78: CPS120 Introduction to Computer Science Exam Review.

Truth Tables

• Use this truth table to determine the results of the logical operators. In this table, 1 represents TRUE and 0 represents FALSE.

• Note that the ! symbol (the logical NOT operator) changes a TRUE to a FALSE.

AND OR NOT A B A && B A B A || B A !A 0 0 0 0 0 0 0 1 0 1 0 0 1 1 1 0 1 0 0 1 0 1

1 1 1 1 1 1

Page 79: CPS120 Introduction to Computer Science Exam Review.

NOT Gate

• A NOT gate accepts one input value and produces one output value

Various representations of a NOT gate

Page 80: CPS120 Introduction to Computer Science Exam Review.

AND Gate

• An AND gate accepts two input signals• If the two input values for an AND gate are both

1, the output is 1; otherwise, the output is 0

Various representations of an AND gate

Page 81: CPS120 Introduction to Computer Science Exam Review.

OR Gate

• If the two input values are both 0, the output value is 0; otherwise, the output is 1

Various representations of a OR gate

Page 82: CPS120 Introduction to Computer Science Exam Review.

XOR Gate

Various representations of an XOR gate

Page 83: CPS120 Introduction to Computer Science Exam Review.

NAND and NOR Gates

• The NAND and NOR gates are essentially the opposite of the AND and OR gates, respectively

Various representations of a NAND gate

Various representations of a NOR gate

Page 84: CPS120 Introduction to Computer Science Exam Review.

Algorithms & Problem Solving

Page 85: CPS120 Introduction to Computer Science Exam Review.

The Program Development Cycle

Get the program into machine-readable form

Test and debug the program

Translate the program

Understand the problem

Code the program

Plan the program's logic

Document the program

Page 86: CPS120 Introduction to Computer Science Exam Review.

What Can a Program Do?

• A program can only instruct a computer to:– Read Input– Sequence– Calculate– Store data– Compare and branch– Iterate or Loop– Write Output

Page 87: CPS120 Introduction to Computer Science Exam Review.

Fundamental Programming Concepts

• Assignment of values to a variable

• Iteration (Looping)– Over a set of set of statements– With respect to a logical expressions

(conditions)

• Delegation of sub-tasks to functions / procedures

Page 88: CPS120 Introduction to Computer Science Exam Review.

What is an Algorithm?

• An algorithm is merely the sequence of steps taken to solve a problem– Two parts

• Actions to be executed • Order in which those actions are to be done

– Computational steps that transform the input data into useful output data.

• Algorithms are not programs– They need to be coded in a programming language like

C++

Page 89: CPS120 Introduction to Computer Science Exam Review.

Pseudocode & Flowcharts are Important

• Pseudocode – – Make a detailed description of your algorithm’s logic

before worrying about C++ syntax and data layout.

– An algorithm you develop using pseudocode should be capable of implementation in any procedural programming language

• Pseudocode is generally independent of the implementation language

• Flowcharts – – A graphical layout of the algorithm is often very useful

in spotting “illogical” logic!

Page 90: CPS120 Introduction to Computer Science Exam Review.

Reasons Programmers Draw Flowcharts

• Drawing a flowchart gives the programmer a good visual reference of what the program will do

• Flowcharts serve as program documentation• Flowcharts allow a programmer to test alternative

solution to a problem before coding • Flowcharts provide a method for easy desk

checking

Page 91: CPS120 Introduction to Computer Science Exam Review.

Terminator. Shows the starting and ending points of the program. A terminator has flow lines in only one direction, either in (a stop node) or out (a start node).

Data Input or Output. Allows the user to input data and results to be displayed.

Processing. Indicates an operation performed by the computer, such as a variable assignment or mathematical operation. With a heading – an internal subroutine

Decision. The diamond indicates a decision structure. A diamond always has two flow lines out. One flow lineout is labeled the “yes” branch and the other is labeled the “no” branch.

Predefined Process. One statement denotes a group of previously defined statements. Such as a function or a subroutine created externally

Connector. Connectors avoid crossing flow lines, making the flowchart easier to read. Connectors indicate where flow lines are connected. Connectors come in pairs, one witha flow line in and the other with a flow line out.

Off-page connector. Even fairly small programs can have flowcharts that extend severalpages. The off-page connector indicates the continuation of the flowchart on another page. Just like connectors, off-page connectors come in pairs.

Flow line. Flow lines connect the flowchart symbols and show the sequence of operations during the program execution.

Common Flowchart Symbols

Common Flowchart Symbols

Page 92: CPS120 Introduction to Computer Science Exam Review.

Rules for Drawing Flowcharts

• Top to bottom and left to right– Draw the flowchart the way you like to read– Use arrowheads on flow lines whenever the

flow is not top to bottom, left to right

• Be neat ! Use graphics software

• Avoid intersecting lines

Page 93: CPS120 Introduction to Computer Science Exam Review.

Disadvantages to Flowcharts

• Time consuming

• A program flowchart shows how the input becomes output, but it does not show why a particular step is done

• Flowcharts are subjective

Page 94: CPS120 Introduction to Computer Science Exam Review.

Pseudocode

• This device is not visual but is considered a “first draft” of the actual program.

• Pseudocode is written in the programmer’s native language and concentrates on the logic in a program—not the syntax of a programming language.

Page 95: CPS120 Introduction to Computer Science Exam Review.

General Rules for Pseudocode

• There is no standard pseudocode

• The rules of Pseudocode are generally straightforward– Should be easily read and understood by non-

programmers – All statements showing "dependency" are to be

indented. • These include while, do, for, if, switch

Page 96: CPS120 Introduction to Computer Science Exam Review.

Pseudocode Statement Rules– Statements are written in a simple English-like

language– Each instruction is started on a separate line– Logic-showing keywords are written in UPPER CASE

or typed in BOLD UPPERCASE• (e.g. IF, THEN, FOR, DO etc.)• These are the only uppercase words in this form of

pseudocode.

– Indentation is used to show structure– Instructions are written from top to bottom, with only

one entry point and one exit point– Logically related groups of instructions can be formed

into modules and given a name

Page 97: CPS120 Introduction to Computer Science Exam Review.

Rules for Pseudocode1. Make the pseudocode language-independent

2. Indent lines for readability

3. Make key words stick out by showing them capitalized, in a different color or a different font

4. Punctuation is optional

5. End every IF with ENDIF

6. Begin loop with LOOP and end with ENDLOOP

7. Show MAINLINE first; all others follow

8. TERMINAE all routines with an END instruction

Page 98: CPS120 Introduction to Computer Science Exam Review.

Lower Level Languages

Page 99: CPS120 Introduction to Computer Science Exam Review.

Machine Language

• Machine language: the instructions built into the hardware of a particular computer – Every processor type has its own set

of specific machine instructions– The relationship between the processor and the

instructions it can carry out is completely integrated

– Each machine-language instruction does only one very low-level task

Page 100: CPS120 Introduction to Computer Science Exam Review.

Assembly Language

• Assembly languages: assign mnemonic letter codes to each machine-language instruction– The programmer uses these letter codes in place

of binary digits

– A program called an assembler reads each of the instructions in mnemonic form and translates it into the machine-language equivalent

Page 101: CPS120 Introduction to Computer Science Exam Review.

The Assembly Process

Page 102: CPS120 Introduction to Computer Science Exam Review.

Compilation Process

Page 103: CPS120 Introduction to Computer Science Exam Review.

Compilation Process

1. Get the set of instructions from you2. Review the instructions to see if they violate the

rules (syntax) of the language3. If all the rules are obeyed, create a working file

in the language of the computer (machine language)

4. Attach to the working file full instructions for any shortcuts you may have used (linkage)

5. Assemble a final file in machine language

Page 104: CPS120 Introduction to Computer Science Exam Review.

Compiling and Debugging

• Executable code will not be created until you correct all of the syntax errors in your source code

• Then the fun (with logic errors) begins

Page 105: CPS120 Introduction to Computer Science Exam Review.

Introduction to C++

Page 106: CPS120 Introduction to Computer Science Exam Review.

C++ Usages & Conventions• C++ is absolutely case sensitive

–For Instance: A is 97 in ASCII and a is 65–Remember: in ASCII {, [, and ( are not equivalent

• No keywords in ANSI standard are even partially uppercase

–‘While’ is not a keyword, ‘while’ is–Be careful if you define new keywords

Page 107: CPS120 Introduction to Computer Science Exam Review.

Characteristics of a C++ Program

• Comments

• Compiler Directives

• Functions

• Braces

• Statements

Page 108: CPS120 Introduction to Computer Science Exam Review.

A Simple C++ Program

Comments //Simple C++ Program // // Purpose: To demonstrate the // parts of a simple C++

programCompiler Directive #include <iostream.h>Main Function main ( )

Braces {Statements cout << "This is a simple program ";

return 0;}

Page 109: CPS120 Introduction to Computer Science Exam Review.

Comments

• Document what is happening, why it is happening and other issues

• Commentary is ignored by the compiler• C++ has inline, block and documentary comments

–Inline comments are within line of code• Use the // symbols

–Block comments are long comments delimited with /* and */

Page 110: CPS120 Introduction to Computer Science Exam Review.

Compiler Directives

• Instructions to the compiler rather than part of the C++ language– Most common directive is #include

• For Example: #include <iostream.h>– A .h file is a header file. It serves as a link between

program code and standard C++ code needed to make programs run

Page 111: CPS120 Introduction to Computer Science Exam Review.

Functions

• A function is a block of code that carries out a specific task

• Every C++ program has a main function that executes when a program initiates– Includes open parenthesis to designate a

function– Ends with a return 0; statement

Page 112: CPS120 Introduction to Computer Science Exam Review.

Scope Delimiters

• A symbol or pair of symbols used to define a region or area which is considered a locale

• In programming, many structures need to have their scope defined because they should not affect the entire program– In C++, the symbols ‘{‘ and ‘}’ are used

Page 113: CPS120 Introduction to Computer Science Exam Review.

Semicolons

• There must be a semicolon after every statement– To tell the compiler that the statement is

complete– Function definitions and compiler directives are

exempt

Page 114: CPS120 Introduction to Computer Science Exam Review.

Columns and White Space• Modern programming languages are free form with

delimiters instead of columns to determine the end of instructions

–The ; (semi-colon) is the delimiter used in C++

• Use tabs, indents, and blank lines in any manner that makes code easier to understand

• Many programming instructions become subordinate to other instructions due to scope and other restrictions. Formatting code to reflect this makes it easier to read

Page 115: CPS120 Introduction to Computer Science Exam Review.

Uppercase or Lowercase

• Be careful to use the same combination of uppercase or lowercase lettering when you enter source code

• Commands and other reserved words are all lower case

Page 116: CPS120 Introduction to Computer Science Exam Review.

C++ Control Structures

1. "Sequence statements" are imperatives2. "Selection" is the "if then else" statement

– AND, OR, NOT and parentheses ( ) can be used for compound conditions

3. "Iteration" is satisfied by a number of statements– "while" – " do " – "for"

4. The case-type statement is satisfied by the "switch" statement.

– CASE statements are used for most non-trivial selection decisions

Page 117: CPS120 Introduction to Computer Science Exam Review.

Debugging

Page 118: CPS120 Introduction to Computer Science Exam Review.

Compiling and Debugging

• Executable code will not be created until you correct all of the syntax errors in your source code

Page 119: CPS120 Introduction to Computer Science Exam Review.

Syntax & Logic Errors

• A syntax error is simply the violation of the rules of a language; misuse of structure and form in programming or a violation of the compiler’s rules. These errors are detected by the compiler– Also know as 'fatal compilation errors'

• A logic error is a mistake that complies with the rules of the compiler that causes the program to generate incorrect output

Page 120: CPS120 Introduction to Computer Science Exam Review.

Linker Errors

• Not all syntax errors are detectable by the compiler– These errors do not become apparent until files

are put together to create an executable– These errors are not linked to a specific line of

code• Look for the name of the variable and see what lines

of code it occurs on using EDIT and FIND– LNIK2001: unresolved external – LNK1120: unresolved externals

Page 121: CPS120 Introduction to Computer Science Exam Review.

Debugging

• Debugging is the process of locating and fixing or bypassing bugs (errors) in computer program code or the engineering of a hardware device.

• To debug a program or hardware device is to start with a problem, isolate the source of the problem, and then fix it.


Recommended