CPS120 Introduction to Computer Science
Exam Review
Introduction To Computers
5
A mental model that removes complex details
This is a key concept.
Abstraction
System Components
• Hardware – I/O devices (input/output)
• keyboard, mouse, monitor, etc.
– CPU– Primary Storage– Secondary Storage
• Software
Stored-Program Concept
Figure 5.1 The von Neumann architecture
The Fetch-Execute Cycle
• Fetch the next instruction
• Decode the instruction
• Get data if needed
• Execute the instruction
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
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
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
BUS
• Electronic pathway that carries information between devices in a computer– Bus Width (think lanes of a highway)– Bus Speed (think speed limit)
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
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
Memory
• Memory is a collection of cells, each with a unique physical address
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”
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
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
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)
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
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
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
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
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
Databases
• Organized in: – Tables
• Collection of information about a specific topic
– Field• Category of information
– Record• Single instance of information
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
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
Interpreter vs. Compiler
• Interpreter– Translates instructions to machine code line-by-
line.
• Compiler– Translates the entire program to machine code
before running it.
Computer Mathematics
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
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
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
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
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
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
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
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
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
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
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
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
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
Data Representation
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)
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
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.
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.
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.
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.
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.
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.
Keyword Encoding
• Frequently used words are replaced with a single character. For example:
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.
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
File Systems and Directories
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
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
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
An Analogy
Sequential-access data files compare to audio cassette tapes ASrandom-access data files compare to audio compact discs (CD's)
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
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.
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
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
Databases
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
Structured Query Language
• The Structured Query Language (SQL) is a comprehensive database language for managing relational databases
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
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)
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
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.
SQL Select Distinct
• The DISTINCT keyword is used to return only distinct (different) values.– SELECT DISTINCT column-name(s)
FROM table-name
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
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.
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
Gates and Boolean Logic
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)
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.
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
Representations
• There are three different, but equally powerful, notational methods for describing the behavior of gates and circuits– Boolean expressions– logic diagrams– truth tables
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
NOT Gate
• A NOT gate accepts one input value and produces one output value
Various representations of a NOT gate
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
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
XOR Gate
Various representations of an XOR gate
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
Algorithms & Problem Solving
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
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
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
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++
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!
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
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
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
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
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.
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
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
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
Lower Level Languages
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
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
The Assembly Process
Compilation Process
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
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
Introduction to C++
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
Characteristics of a C++ Program
• Comments
• Compiler Directives
• Functions
• Braces
• Statements
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;}
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 */
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
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
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
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
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
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
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
Debugging
Compiling and Debugging
• Executable code will not be created until you correct all of the syntax errors in your source code
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
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
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.
Top Related