80
Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Embed Size (px)

Citation preview

Page 1: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Computer Programming

CS 1 Introduction to Computers and Computer Technology

Rick Graziani

Fall 2015

Page 2: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 2

Computer Architecture

• Central Processing Unit (CPU) or processor– Arithmetic/Logic unit (ALU)

• Performs operations on data such as addition and subtraction– Control unit

• Coordinating the CPU’s activities• Holds input and results (output) for the ALU

– Registers• Temporary storage for the CPU

– General registers– Special purpose registers

Page 3: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 3

A CPU can be:

A CPU can be:

1. A series of integrated circuits (chips) on one or more circuit boards– Older mainframe and minicomputers

2. On a single integrated circuit known as a microprocessor

microprocessor = a CPU on a single chip

microcomputer = older term for a computer with a microprocessor(s) (PC, Macintosh)

Page 4: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 4

Computer Architecture

• Bus– Used to transfer bits between the CPU and RAM (main memory)

Page 5: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 5

User interface

2 + 3 =

5

User Types (Input)

Computer Outputs

Page 6: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 6

Computer Architecture

• Task: Add two values stored in main memory (RAM)– Data (two values) must be transferred from main memory to registers

within the CPU– ALU: Values are added– Result stored in main memory (RAM)

Value = 2

Value = 3

2 + 3 = 5

Value = 5

Page 7: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 7

Computer Architecture

• Task: Add two values stored in main memory (RAM)– Data (two values) must be transferred from main memory to registers

within the CPU– ALU: Values are added– Result stored in main memory (RAM)

2

3

2 + 3 = 5

2

3

2 + 3 5= 5

Page 8: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 8

Our CPU and the Pentium CPU

Page 9: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 9

Stored Program Concept

• Stored program concept: A program can be encoded as bit patterns and stored in main memory.

– CPU can then:

• extract the instructions as needed (copy them into its registers)

• execute them

Program

instruction

Page 10: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 10

Terminology

• Machine instruction: An instruction (or command) encoded as a bit pattern recognizable by the CPU

• Machine language: The set of all instructions recognized by a machine (CPU)

Op-code Operand Description 1 RXY LOAD reg. R from cell XY. 2 RXY LOAD reg. R with XY. 3 RXY STORE reg. R at XY. 4 0RS MOVE R to S. 5 RST ADD S and T into R. (2’s comp.) 6 RST ADD S and T into R. (floating pt.) 7 RST OR S and T into R. 8 RST AND S and T into R. 9 RST XOR S and T into R. A R0X ROTATE reg. R X times. B RXY JUMP to XY if R = reg. 0. C 000 HALT.

Page 11: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 12

Machine Instruction Types

• Machine Instruction Types– Data Transfer: Copy data from one location to another– Arithmetic/Logic: Use existing bit patterns to compute a new bit

patterns– Control: Direct the execution of the program

• More in a moment

Op-code Operand Description 1 RXY LOAD reg. R from cell XY. 2 RXY LOAD reg. R with XY. 3 RXY STORE reg. R at XY. 4 0RS MOVE R to S. 5 RST ADD S and T into R. (2’s comp.) 6 RST ADD S and T into R. (floating pt.) 7 RST OR S and T into R. 8 RST AND S and T into R. 9 RST XOR S and T into R. A R0X ROTATE reg. R X times. B RXY JUMP to XY if R = reg. 0. C 000 HALT.

Page 12: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 13

Adding values stored in memory

2

3

5

2+3=5

Page 13: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 14

Using machine language

Page 14: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 15

Computer Architecture

2

3

2

3

2 + 3 5= 5

6C

6D

6E

5

6

50

Page 15: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 16

Adding values stored in memory

• Data Transfer

– Instructions that request the movement (copying) of data from one location to another

Step 1

Step 2

Step 3

Step 4

Step 5

2

3

2

3

2 + 3 5= 5

6C

6D

6E

5

6

50

Page 16: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 17

Adding values stored in memory

• ALU

– Instructions that tells the control unit to request an activity within the ALU

– Capable of performing operations other than basic arithmetic operations, including: AND, OR, XOR.

Step 1

Step 2

Step 3

Step 4

Step 5

2

3

2

3

2 + 3 5= 5

6C

6D

6E

5

6

50

Page 17: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 18

Remember our half-adder? Adding two bits

• Adding two, eight bit values involves a similar process, just more gates (2 + 3 = 5):

• 00000010 + 00000011 = 00001001

Inputs: A, B

S = Sum

C = Carry

AND

XOR

A + B = 2’s 1’s

0 1 =

01

10

SC

1

0

0

+ 1

----1

Page 18: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 19

Adding values stored in memory

• Control

– Instructions that direct the execution of the program Jump or Branch instructions: Directs the CPU to execute an instruction other than the next one in the list.

• Unconditional Jump: Skip to Step 6

• Condition Jump: If the value is 0 then Skip to Step 6

Step 1

Step 2

Step 3

Step 4

Step 5

Page 19: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 20

The architecture of our machine

• Main Memory– 256 cells: 00 through FF (Hex)

• 0000 0000 through 1111 1111– Storage: 8 bits per cell

• CPU– 16 registers: 0 through F (Hex); 8 bits per cell– Program counter: (Address of) Keeps track of the next instruction– Instruction register: Contains the current instruction to be executed by

the ALU.

Address

00000001

00000010

00000011

11111111

00000000

Page 20: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 21

Converting Decimal, Hex, and Binary

Dec. Hex. Binary Dec. Hex. Binary

0 0 0000 8 8 1000

1 1 0001 9 9 1001

2 2 0010 10 A 1010

3 3 0011 11 B 1011

4 4 0100 12 C 1100

5 5 0101 13 D 1101

6 6 0110 14 E 1110

7 7 0111 15 F 1111

1 Hex digit = 4 bits

Page 21: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 22

Loading instructions into the computer

Page 22: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 23

Parts of a Machine Instruction

Machine Language• Each instruction involves two parts:

– Op-code: Specifies which operation to execute• LOAD, ADD, STORE, etc.

– Operand: Gives more detailed information about the operation• Interpretation of operand varies depending on op-code

Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s

comp.) 6 ADD S and T into R. (floating

pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

0011 0101 1010 0111 Binary (16 bits)

1 Hex digit = 4 bits

Page 23: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 24

Parts of a Machine Instruction

Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s

comp.) 6 ADD S and T into R. (floating

pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Store the bits found in register 5 in main memory cell A7

88A 7

1010 01115

Page 24: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 25

The machine cycle

Page 25: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 26

Program Execution

Page 26: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 27

The program is stored in main memory ready for execution

• The program is copied put into main memory.– Typically from permanent storage.– Requires two memory cells per instruction:

• Instructions are 16 bits; memory cells are 8 bits• Program counter contains first instruction: A0

00

FF

1 Hex digit = 4 bits Hard Disk Drive

Page 27: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 28

Performing the fetch step of the machine cycle

Page 28: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 29

• At the beginning of the first fetch:– Program Counter = A0

0

5

6

Instruction Register

Program Counter

Registers

A0

6E

6C

6D

02

03

Page 29: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 30

• At the beginning of the first fetch:– Instruction at A0 (and A1) loaded into Instruction register– Program Counter = A2

0

Instruction Register

Program Counter

Registers

6E

6C

6D

02

03

156C

A0A2

5

6

Page 30: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 31

• CPU analyzes the instruction

• Loads Register 5 with the contents of memory cell address 6C.

156C

0

5

6

Instruction Register

Program Counter

Registers

A2

…6C

6D

02

03Load register 5 in main memory cell 6C

6E

02

Page 31: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 32

• At the beginning of the next fetch:– Program Counter = A2

02

0

5

6

Instruction Register

Program Counter

Registers

A2

…6C

6D

02

03

6E

Page 32: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 33

• Instruction at A2 (and A3) loaded into Instruction register

• Program Counter incremented: A4

• CPU analyzes the instruction

• Loads Register 6 with the contents of memory cell address 6D.

02

0

5

6

Instruction Register

Program Counter

Registers

…6C

6D

02

03Load register 6 in main memory cell 6D

6E

A2

166D03

A4

Page 33: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 34

• At the beginning of the next fetch:– Program Counter = A4

02

03

0

2

3

Instruction Register

Program Counter

Registers

A4

…6C

6D

02

03

6E

Page 34: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 35

• Instruction at A4 (and A5) loaded into Instruction register• Program Counter incremented: A6• CPU analyzes the instruction• Adds contents of register 5 and register 6, storing the result into

register 0.

02

03

0

5

6

Instruction Register

Program Counter

Registers

…6C

6D

02

03Add: result into register 0, adding contents of register 5 and register 6

6E

A4A6

5056

05

Adds contents of register 5 and 6, placing result into register 0.

Page 35: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 36

• At the beginning of the next fetch:– Program Counter = A6

05

02

03

0

2

3

Instruction Register

Program Counter

Registers

A6

…6C

6D

02

03

6E

Page 36: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 37

• Instruction at A6 (and A7) loaded into Instruction register

• Program Counter incremented: A8

• CPU analyzes the instruction

• Stores contents of register 0 in the memory cell at address 6E.

05

02

03

0

5

6

Instruction Register

Program Counter

Registers

…6C

6D

02

03Store contents of register 0, in memory cell address 6E

6E 05

A6A8

306E

Page 37: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 38

• At the beginning of the next fetch:– Program Counter = A8

05

02

03

0

5

6

Instruction Register

Program Counter

Registers

A8

…6C

6D

02

03

6E 05

Page 38: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 39

• Instruction at A8 (and A9) loaded into Instruction register

• Program Counter incremented: AA

• CPU analyzes the instruction

• Halts the program. (Program ends.)

05

02

03

0

5

6

Instruction Register

Program Counter

Registers

A8

…6C

6D

02

03Halt the program

6E 05

AA

C000

Page 39: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 40

Algorithm

• An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.

• Algorithms are part of many activities, even mundane ones.

• Note: Researchers believe that the human mind including imagination, creativity, and decision making, is actually the result of algorithm execution.– This is used in artificial intelligence

Page 40: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 41

Example

• Obtain a basket of unshelled peas and an empty bowl.

• As long as there are unshelled peas in the basket continue to execute the following steps:

a. Take a pea from the basket

b. Break open the pea pod

c. Dump the peas from the pod into the bowl

d. Discard the pod

Page 41: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 42

Defining the Algorithm

• An algorithm is an ordered set of unambiguous, executable steps that defines a terminating process.– Steps do not have to be executed in sequence.

• Non-Terminating Sequence: – Make a list of positive integers.– The above requirement could not be performed in an algorithm,

because it does not terminate (it is infinite). • Unambiguous

– The instructions must be clear, specific and direct– No room for creativity or interpretation

1 2 3 4 5 6 7 8 9 10 11 … (this could go on for ever!)

Non-terminating sequence

Ambiguous

Organize the CDs (By title? By artist? By genre?)

Page 42: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 43

Abstract Nature of Algorithms

• An algorithm can represented in several ways.

• Example: Algorithm to convert temperatures from Celsius to Fahrenheit: – As an algebraic formula:

• F = (9/5)C + 32– As a written instruction:

• Multiply the temperature reading in Celsius by 9/5 and then add 32

Page 43: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 44

Algorithm Representation

• Algorithm requires some form of a language.

• Algorithm is a form of communication– Don’t want

misunderstandings– Proper level of detail– Proper level of difficulty

• Problems arise when:– Steps not precisely defined– Not enough detail

Page 44: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 45

• Primitive – A well defined set of building blocks (terms) used in computer science.– Arithmetic and logic operations built into the language– Removes any ambiguity– Includes its own syntax

• Programming language – A collection of primitives (terms) and the rules that state how the primitives can be combined to represent more complex ideas.

Algorithm Representation

LOAD reg. R from cell XY

Op-code Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Page 45: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 46

• Machine language uses primitives

• High-level programming languages (C++, Java) use higher-level primitives, constructed from the lower-level machine language primitives.

• This results in an easier set of instructions to write.

• More later.

Store the bits found in register 5 in main memory cell A7

Op-code Operand Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s

comp.) 6 ADD S and T into R. (floating

pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Page 46: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 47

Pseudocode

• Pseudocode – A notational system in which ideas can be expressed informally during the algorithm development process. (written sentence)

• Used independently of the programming language.– Each programming language has its own primitives and rules.

Pseudocode

1. Enter two numbers.

2. Add the numbers together.

3. Display the result.

Page 47: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 48

Pseudocode

Conditional selection• The selection of one of two possible activities depending upon the truth or

falseness of some condition

if condition then action

or

if condition then (activity)

else (activity)

• If this condition is true, perform this activity.If (sunny)

then (put on sunscreen)

• If this condition is true, perform this activity, otherwise perform a different activity.If (sunny)

then (go swimming)else (go bowling)

Page 48: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 49

Repeating structure

• Another common semantic structure is the repeated execution of a statement or sequence of statements as long as some condition remains true.

while condition do activity

• Also known as a while loop• Examples:

while (tickets remain to be sold) do (sell a ticket)

Page 49: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 50

Repeating structure

Task: Write Hello 500 times.

PseudocodeCounter = 1While counter is less than or equal to 500, write the word “Hello” and add 1 to Counter.

Programming CodeCounter = 1While (counter <= 500)

do(print “Hello”; Counter Counter + 1)

Counter

1

Hello

2Hello

3 Hello4Hello

500501

Hello

…<End of loop>

Page 50: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 51

For loop

• A for loop can be used to accomplish the same thing as a while loop.• Note: There are some differences between while and for loops.

Counter = 1For (Counter <= 500)

do(print the message “Hello”; Counter Counter + 1)

Counter

1

Hello

2Hello

3 Hello4Hello

500501

Hello

…<End of loop>

Page 51: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 52

For loop

• A for loop can be used to accomplish the same thing as a while loop.• Note: There are some differences between while and for loops.

For (Counter = 1; Counter <= 500; Counter Counter + 1) do(print the message “Hello”)

Counter

1

Hello

2Hello

3 Hello4Hello

500501

Hello

…<End of loop>

Page 52: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 53

Programming Concepts

• Program consists of:– Declarative statements

• Variables and data types– Imperative statements

• Procedures, instructions, and algorithms– Comments

• Enhance readability• Used throughout the program

Page 53: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 54

Variables and Data Types

• Variable – A location in RAM (main memory), given a descriptive name, which stores information.

• Data type – Variables are a type of day which can be:– Number

• Integer: 0, 1, 2, 3, 4, 5, 6, etc.• Real (floating point): 9.75, 300.5412

– Character: a, b, c, A, B, C, etc.– String of text: “123 Main Street”

• Working area or scratch pad for the program.

125

9.75

d

Integer

Float

Character

Variable

Page 54: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 55

Data structure

• Variables are often associated with a data structure.

• Data structure – A conceptual shape of arrangement of data

• Homogeneous Array – Block of values of the same type– Such as a one dimensional list or a two dimensional array (row,

column)

• Example: String or array of characters– char Last_Name[25]

1 2 3 4 5 … … 24 25

B o o l o o t i a n

char Last_Name[25]

Page 55: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 56

Data structure

• Two dimensional array– Row and column– Integer pins [bowler, frame]

Dan

Sue

Gary

Mary

3

12

0

7

6

9

1

7

Integer pins [bowler, frame]

Frame1 2 3 4 5 6 7 8 9 10

pins [Mary, 2] = 7

Page 56: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 57

Assigning Value

• Assignment statement – Statement which assigns a value to a variable.– Value assigned from user input– Value assigned from another variable– Value assigned from a specific value– Value assigned from a calculation that can include both

variables and assigned values.

125

9.75

d

Integer

Float

Character

Fahrenheit = (9/5)Celcius + 32

Page 57: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 58

1 #include <stdio.h>This is a preprocessor command that includes standard input output header file (stdio.h)

from the C library before compiling a C program

2 int main()This is the main function from where execution of any C program begins.

3 {This indicates the beginning of the main function.

4 /*_some_comments_*/whatever is given inside the command “/* */” in any C program, won’t be considered for

compilation and execution.

5 printf(“_____________”);printf command prints the output onto the screen.

6 getch();This command waits for any character input from keyboard.

7 return 0;This command terminates C program (main function) and returns 0.

8 }This indicates the end of the main function

Page 58: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

#include <stdio.h>

int main()

{

printf("Hello world");

return 0;

}

Note: Actual syntax has been simplified

• Display “Hello world”

• End the program

Page 59: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

#include <stdio.h>

int main()

{

int a;

printf("Enter an integer");

scanf(a);

printf("Integer that you have entered is”, a);

return 0;

}

Note: Actual syntax has been simplified

• Display “Enter an integer”

• User enters a number

• Display “Integer you have entered is”

• Display the number the user entered

• End program

Page 60: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

#include<stdio.h>

int main()

{

int a, b, c;

printf("Enter two numbers to add ");

scanf(a,b);

c = a + b;

printf("Sum of entered numbers = ",c);

return 0;

}Note: Actual syntax has been simplified

• Display “Enter two numbers to add”

• User enters two numbers

• Add the two numbers

• Display “Sum of the entered numbers = “ and the sum

• End program

Page 61: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

#include <stdio.h>

int main()

{

int n;

printf("Enter an integer");

scanf(n);

if (n/2 = 0)

printf("Even");

else

printf("Odd");

return 0;

}Note: Actual syntax has been simplified

• Display “Enter an integer”

• User enters an integer

• Determine if the integer is even

• If the integer is even display “Even”

• Else display “Oven”

• End program

Page 62: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 67

• “I want you to write on the chalk board, ‘I will not throw paper airplanes in class’, 500 times.”

Page 63: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 68

Programming Concepts

Count 1

I will not throw paper airplanes in class.

2

I will not throw paper airplanes in class.

3

I will not throw paper airplanes in class.…

499

I will not throw paper airplanes in class.

500

I will not throw paper airplanes in class.

501Variable Count which is type integer

Page 64: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 69

Variables

• Variables are declared, defined in the declaration section of the program.– Usually at the beginning of the program– Examples:

int height_in_inches

char first_initial

float price

Page 65: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 70

Variables

• Variables:– Can be given an initial value within the program– Value may change from:

• Program instructions• User input

Count 1

I will not throw paper airplanes in class.

2

I will not throw paper airplanes in class.

3

I will not throw paper airplanes in class.…

499

I will not throw paper airplanes in class.

500

I will not throw paper airplanes in class.

501

Page 66: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Programming Concepts

Count 123499500501Variable Count which is type integer

• Count = 1

• For Loop – As long as Count is less than or equal to 500– Add 1 to Count– Print “I will not throw paper airplanes in class”

• Exit program

Page 67: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Software Development

Page 68: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 73

Definitions

Software or Program

Instructions that tell the computer what to do

Programmer

Someone who writes computer programs

Page 69: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 74

Instruction SetA vocabulary (list) of instructions which can be executed

by the CPU• The only instructions the CPU can run or execute• Example of a CPU’s Instruction Set

CPU Instruction Set

Instruction Set Instruction0001 Move0010 Compare0011 Bit test0100 Bit clear0101 Bit set0110 Add0111 See group 101000 See groups 11, 13, 141001 Move byte

Page 70: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 75

First Generation Languages(Machine Language)

• Programming computers using the CPU’s instruction set• Also known as Machine Language• (timeline)

Machine Code FileA software file which contains the instructions from the CPU’s instruction set.

Page 71: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 76

Advantages of First Gen.

• Software programs execute (run) relatively very quickly

• Software programs are relatively small in size

• (Insignificant advantages today)

Disadvantages of First Gen.

• Difficult to write, very detailed and takes a long time

• Difficult to read

• Difficult to debug

debug = the process to find mistakes in a software program

First Generation Languages(Machine Language)

Page 72: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 77

Second Generation Languages(Assembly Language)

Assembly Language = The English-like instructions which are equivalent to the CPU’s instruction set

Source Code= The actual instructions written by a programmer

Compiler = Software which translates source code instructions of a particular language into machine code

Op-code Description 1 LOAD reg. R from cell XY. 2 LOAD reg. R with XY. 3 STORE reg. R at XY. 4 MOVE R to S. 5 ADD S and T into R. (2’s comp.) 6 ADD S and T into R. (floating pt.) 7 OR S and T into R. 8 AND S and T into R. 9 XOR S and T into R. A ROTATE reg. R X times. B JUMP to XY if R = reg. 0. C HALT.

Page 73: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 78

Question: Which of these two files (source code file or machine code file) will the user need to run this software program?

Advantages of Second Gen.

• Easier to read than first gen.

• Easier to write than first gen.

• Easier to debug than first gen.

Disadvantages of Second Gen.

• Still very difficult to write programs

Second Generation Languages(Assembly Language)

Page 74: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 79

Using a compiler

Page 75: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 80

Using an Interpreter

Page 76: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Interpreter versus Compiler

Rick Graziani [email protected] 81

Page 77: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 82

Third Generation Languages(High level languages)

Languages which are somewhere between machine language and the human language.

FORTRAN (Formula Translation) - 1950's

Language to allow scientists and engineers to program computers.

COBOL (Common Business Oriented Language) - 1960

Language primarily designed for US government and defense contractors to program business applications on the computer. Grace Hopper was one of the developers of COBOL.

BASIC (Beginner's All-purpose Symbolic Code) - 1960's

Alternative language to FORTRAN for beginning programming students.

Page 78: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 83

Pascal (named after Blaise Pascal, 17th century French mathematician) - 1970's

Language to teach proper structured programming.

Structured programming = Programming technique used to make programming more productive and easier to write. Stresses simplistic, modular programs.

ADA (named after Ada Lovelace (programmed the 19th century 'analytical engine') - late 1970's

Language developed to replace COBOL.

Third Generation Languages(High level languages)

Page 79: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 84

C (successor to BCPL or "B") - 1970's Popular programming language on computers from

microcomputers to super computers.Faster and more efficient language. Very powerful language.

Source code example of a C Program (Displays Hello World! on the screen.)#include <stdio.h>main(){ printf("Hello World!");}

C++ (pronounced "C plus plus") - 1980'sObject oriented language which is compatible with C.

JAVA

Third Generation Languages(High level languages)

Page 80: Computer Programming CS 1 Introduction to Computers and Computer Technology Rick Graziani Fall 2015

Rick Graziani [email protected] 85

Advantages

• Easier to read, write and debug

• Faster creation of programs

Disadvantages

• Still not a tool for the average user to create software programs

• Requires very good knowledge of programming and of the language

Third Generation Languages(High level languages)