Computer Programming I

Dec 30, 2015

Welcome message from author

This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-3

Computer Components

HardwareCPU IO deviceMain memory

Software How does computer work?

Memory

Random Access Memory (RAM)Temporary memoryMain memory

Read Only Memory (ROM)For start-up directionspermanent memory.

Programs

Computer Hardware Software

Programs that run on a computerOperating systemApplication software

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-9

Programming Languages

Three types of programming languages

1. Machine languages Strings of numbers giving machine

specific instructions Example:

+1300042774

+1400593419

+1200274027

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-10

Programming Languages

Three types of programming languages

2. Assembly languages English-like abbreviations representing

elementary computer operations (translated via assemblers)

Example:LOAD BASEPAY

ADD OVERPAY

STORE GROSSPAY

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-11

Programming Languages

Three types of programming languages

3. High-level languages Codes similar to everyday English Use mathematical notations (translated via

compilers) Example:

grossPay = basePay + overTimePay

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-12

Programming Languages (3)

Machine Languages

Assembly Languages

High-Level Languages

+1300042774+1400593419+1200274027

LOAD AADD B

STORE C

C=A+B

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-13

C++

C++ is a third generation language Why C++ not C

C++ is an object oriented language

Decimal System

Positional base 10 numeral systems 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Use same symbol for different orders of

magnitude For example, “1262” in base 10

1*103+2*102+6*101+2*100

Binary

A computer is a “bistable” device A bistable device:

Easy to design and build Has 2 states: 0 and 1

One Binary digit (bit) represents 2 possible states (0, 1)

Decimal to Binary representation 0: 0 1: 1 2: 10

3: 11 4: 100

5: 101 6: 110 7: 111 8: 1000

9: 1001

10: 1010 11: 1011 12: 1100

13: 1101 14: 1110 15: 1111 16: 10000 17: 10001

Convert Binary to Decimal

18

Interpret binary numbers (transform to base 10) 1101

= 1*23+1*22+0*21+1*20=8+4+0+1=13 Translate the following binary number to

decimal number 101011

Convert Decimal to Binary

Procedure:

1. Divide the decimal number by 2

2. Make the remainder the next digit to the left of the answer

3. Replace the decimal number with the quotient

4. If quotient is not zero, Repeat 1-4; otherwise, done

Algorithm

A finite set of well-defined instructions for accomplishing some task which, given an initial state, will terminate in a corresponding recognizable end-state.

Examples: Select the largest number from a set of number (n)

Suppose n numbers are a1, a2, …an Set LG=a1; For i=2 to n, do

if LG<ai, then set LG=ai; Else do nothing;

The largest number is LG

Convert Decimal number 100 to Binary Number

100 % 2 = 0=> last digit100 / 2 = 5050 % 2 = 0 => 2nd last digit50/2 = 2525 % 2 = 1 => 3rd last digit25 / 2 = 1212 % 2 = 0 => 4th last digit

12 / 2 = 66 % 2 = 0 => 5th last digit6 / 2 = 3 3 % 2 = 1 => 6th last digit3 / 2 =1 1 % 2 = 1 => 7th last digit1 / 2 = 0

The result is 1100100

Bytes and Words A group of 8 bits is a byte A byte can represent 28 = 256 possible

states Several bytes grouped together to form a

word Word length of a computer, e.g., 32 bits

computer, 64 bits computer

Representing Text Text is a series of characters

letters, punctuation marks, digits 0, 1, …9, spaces, return (change a line),…

How many bits do we need to represent a character? 1 bit can be used to represent 2 different things 2 bit … 2*2 = 22 different

things n bit 2n different things

In order to represent 128 different character Solve 2n = 128 for n n=7

ASCII The American Standard Code for Information

Interchange 128 characters 7 bits could be used to represent ASCII

characters However, in 1960s, an 8-bit byte was

becoming hardware standard, therefore, it was decided to use 1 byte (8 bits) to store the ASCII code (first 7 bits), with the eighth bit being used as a parity bit to detect transmission errors

Related Documents