CS 1 Introduction CS 1 Part 11. Hardware 1.Central Processing Unit (CPU) 2.Main Memory 3.Secondary Memory / Storage 4.Input Devices 5.Output Devices CS.

Post on 02-Apr-2015

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

Transcript

CS 1 Part 1 1

CS 1

Introduction

CS 1 Part 1 2

Hardware

1. Central Processing Unit (CPU)2. Main Memory3. Secondary Memory / Storage4. Input Devices5. Output Devices

CS 1 Part 1 3

Main Memory

• It is volatile. Main memory is erased when program terminates or computer is turned off

• Also called Random Access Memory (RAM)• Organized as follows:– bit: smallest piece of memory. Has values 0 (off,

false) or 1 (on, true)– byte: 8 consecutive bits. Bytes have addresses.

• Addresses are sequential numbers from 0 to the maximum amount of memory in your computer.

CS 1 Part 1 4

Memory Organization

• In the chart above, the number 149 is stored in the byte with the address 16, and the number 72 is stored at address 23.

• What these numbers might mean depends upon the program.

• The concept of the “stored program” computer is this: that the numbers can be machine instructions.

CS 1 Part 1 5

Secondary Storage

• Non-volatile: data retained when program is not running or computer is turned off

• Comes in a variety of media:– magnetic: floppy disk, hard drive– optical: CD-ROM, DVD– Flash drives, connected to the USB port– Solid-state drives instead of rotating disks

CS 1 Part 1 6

Machine Instructions

• The C++ compiler translates your program, which is relatively easy for you to read, into binary numbers which are the instructions and data the computer understands.

CS 1 Part 1 7

Binary (base 2)

• Inside a computer, the contents of memory cells can be either on or off, or zero or 1.

• Place value: in base 10 you have units, 10s, 100s, 1000s, etc.

• In binary: units, 2s, 4s, 8s, 16s, 32s, etc. Powers of 2 vs. powers of 10.

• Count in binary on your fingers.• Thus there are 10 kinds of people: those who

understand binary and those who don’t.

CS 1 Part 1 8

Binary Addition

Add:0 0 1 1 0 1 0 10 0 1 0 1 1 0 1---------------0 1 1 0 0 0 1 0

Four rules:0+0=01+0=11+1=0 carry 11+1+1 = 1 carry 1

CS 1 Part 1 9

Base 8 and Base 16

• It’s easy to convert between base 2 and either base 8 (octal) or base 16 (hexadecimal) by just grouping digits.

• Convert 11000111 to hex:• Groups of 4 because 2^4th =16• 1100=1210 = C16; 0111=710

CS 1 Part 1 10

Binary to Base 10

Place values:128 64 32 16 8 4 2 1 0 0 1 1 0 1 0 1So in the example we have:

0x128=0 0x8=01x32=32 1x4=40x64=0 0x2=01x16=16 1x1=1Add 32+16+4+1=53

CS 1 Part 1 11

Elements of a Program

• Common elements in programming languages:– Key Words– Programmer-Defined Identifiers– Operators– Punctuation– Syntax

CS 1 Part 1 12

Keywords

• Also known as reserved words• Have a special meaning in C++• Can not be used for any other purpose

CS 1 Part 1 13

Identifiers

• Names made up by the programmer• Not part of the C++ language• Used to represent various things: variables

(memory locations), functions, etc.

CS 1 Part 1 14

Operators and Precedence

• Used to perform operations on data• Many types of operators:– Arithmetic – * (multiply), / (divide), + (add), -

(subtract)– Assignment – ex: = the equal sign– Comparison: ==

CS 1 Part 1 15

Punctuation

• Use semicolon to end statements (not every line is a statement)

• Use comma to separate items in a list.

CS 1 Part 1 16

Grouping

• Parentheses alter the order of operations in an expression, and enclose function arguments.

• Braces {} group statements together.• Brackets [] are used for array references.

CS 1 Part 1 17

Language Syntax

• Syntax refers to the way the elements of a language are put together. For example, in English the verb form doesn’t usually tell you the subject, while in Spanish it does.

• Programming language syntax is the way program statements are constructed.

• Semantics refers to what the statements “mean.”

CS 1 Part 1 18

Variables

• A variable is a named storage location in the computer’s memory for holding a piece of data.

• The contents can be changed by a program.

CS 1 Part 1 19

Variable Declaration

• To create a variable in a program you must write a variable definition (also called a variable declaration)

• You supply a name and the data type. • Examples:– int hours;– double pay;– char answer;

CS 1 Part 1 20

What Programs Do

• Reduced to simplest terms, a program:• Takes some form of input• Does some processing to it• Creates some kind of output

CS 1 Part 1 21

Three Basic Constructs

• Execution in sequence• Making a decision• Looping

CS 1 Part 1 22

Creating a Program

1. Define clearly and precisely what the program is to do.2. Use various tools to create a model of the program.3. Check your model for errors.4. Write code that reflects the model.5. Compile the code, which checks for syntax errors.6. Correct any errors you find and go back to step 5 if there are any.7. Run the program with test data and determine whether the

results are correct.8. If the results are not what you wanted, make changes and go

back to step 7.9. Validate the results.

CS 1 Part 1 23

Two Programming Paradigms

• Procedural programming focuses on the process. The program generally executes from start to finish in a linear way.

• Object-oriented programs focus on objects, which have both data and methods that act on the data. While an object-oriented program can be procedural, most respond to messages from external events.

top related