lecture 1 first program - cairo university · lecture 1 first program memory variables, input,...

23
Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions

Upload: others

Post on 08-May-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Lecture 1First Program

Memory variables, input, disp, arithmetic expressions, math

functions

Page 2: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Overview

• Computer hardware & software

• How to write a program?

• Building our first program

• Memory

• Getting input

• Displaying output

• Arithmetic expressions

• Sample programs

Page 3: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Computer Hardware

Memory Unit

(RAM)Control Unit

Arithmetic Logic Unit

(ALU)

Keyboard Screen Hard disk

Bus

CPU

Input/OutputDevices

Page 4: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Hardware Functionality

• Control unit (The boss): commands all component to do its task.

• Memory Unit: stores data• ALU: calculates arithmetic and logical expressions. It

reads the operands from memory, perform the operation and store the resultant value in memory.

• Keyboard: getting the data from the user to the memory

• Screen: showing the data from the memory to the user• Hard disk: stores data permanently. So data is getting

from memory to HD and vice versa.

Page 5: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Software

• A computer can perform a specific set of instructions, one at a time.– Get input (from keyboard to memory)

– Display output (from memory to screen)

– Compute an arithmetic expression (^, *, /, +, -)

– Compute a logical expression and branch selection

– Repeat instructions

• A software program is a sequence of such instructions

Page 6: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

How to write a program?

1. Read the problem statement, and identify– The input and its range

– The output

– The relationship between the input and the output (how to compute the output) [Comprehend]

2. Write your thoughts as a sequence of steps. [Algorithm]

3. Convert these steps to Code. [Program]

4. Test your code and compare your program result against a human result. [Testing]

Page 7: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Building Our First ProgramCalculate area of a rectangle

How I solve this problem as a human?

I get the values of the height and the width from you and I store them in my memory.

Then I multiply them in my brain and store the result in my memory

and inform you about the resultant value.

Page 8: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate area of a rectangle (Problem Algorithm)

How a computer can solve this problem?

I get the values of the height and the width from you and I store them in my memory.

Get height as hGet width as w

Then I multiply them in my brain and store the result in my memory

Calculate area = h*w

and inform you about the resultant value.

display area

Page 9: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate area of a rectangle(Algorithm Program)

1. Get height h

h=input(‘enter height: ’);

2. Get width w

w=input(‘enter width: ’);

3. Calculate area = h * w

area = h * w;

4. Display area

disp (area);

Page 10: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Memory

• Memory is divided into bytes

• Each byte has its own address represented by binary numbers

• 4 or 8 bytes form a memory location to store integer (45) or floating (10.2345) numbers

• Software Programs uses letters and decimal numbers to name the memory location (A, A1,..).

15

12

45

2

-3

4

x

y

sum

A

A1

numOfBus

Page 11: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Memory Location Name (Variables)

• Variable: a named space for storing a value

A / A1 / radius / ..

• Valid names start with a letter, may contain digits.

• Use meaningful variable names.

• MATLAB is case sensitive (area and Area are different)

Xyz 3xyz x-y ab2cd a12345 myvar AbCdE1234 ?

Page 12: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Get input from user

h=input(‘enter height: ’);

Memory location to store the input

Keyword to hold the program execution to get input from the keyboard

Message to the user

Write a Matlab statement to take the circle radius from the user and store it in ‘R’.

>> enter height:

5h

?

5

Page 13: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Get an array from user

X=input(‘enter array elements: ’);>> enter array elements: [4 5 2 6 8]

length(X): number of elements

To access the array elements use X(index)Where index is an integer from 1 to length(X)

What is the value of: X(4)= X(6)=

4

5

2

6

8

X(1)

X(2)

X(3)

X(4)

X(5)

?

Page 14: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Display to Screen

disp(h);

Memory location to displayKeyword to display

to the screen

>> 5

5

102

h

disp(a);

>> 102

a

Page 15: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Arithmetic Expressions

Operations^ Exponentiation (5^2 is 25)*, / Multiplication and division+, - Addition and subtraction

Examplesx=4; y=3.5; z=2;a= x +y – 5 / (3+z);b=a + x / 2;

4

3.5

2

6.5

8.5

x

y

z

a

b

Operations are executed only one at a time according to their precedence. We will study it further in future lectures

Page 16: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate area of a rectangle (Program and Testing)

h=input(‘enter height: ’);

w=input(‘enter width: ’);

area = h * w;

disp (area);

enter height: 5enter width: 420

5

4

20

h

w

area

Page 17: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate area of a circle (Algorithm, Program, Testing)

1. Get radius r

2. Calculate area = pi*r^2

3. Display area

r=input(‘enter radius:’);

area =pi*r^2;

disp(area);

enter radius: 549.3480

549.3480

radius

area

Page 18: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate number of buses to transfer students (Algorithm)

1. Get number of students as numS

2. Get the bus capacity as busC

3. Calculate num of buses as

numB = numS/busC (round up)

4. Display numB

Page 19: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate number of buses to transfer students (Program)

numS=input(‘enter number of students:’);

busC=input(‘enter bus capacity:’);

numB = numS/busC;

disp(numB);

ceil(1.2) = 2 ceil round up to nearest integer

numB = ceil(numS/busC);

If numS=60 and busC = 50 then numB = 60/50 = 1.2 ???But it should be 2

Page 20: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate number of dozens and remainder (Algorithm)

1. Get a number as N

2. Calculate dozen = N/12 (no fraction)

3. Calculate the reminder (r) of dividing N by 12

4. Display dozen

5. Display r

Page 21: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Calculate number of dozens and remainder (Program)

N=input(‘enter a number’);

dozen = N/12;

r = ??

disp(dozen);

disp(r);

fix(5.8) = 5 fix removes the fraction

rem(10,3) = 1 rem calculates the reminder of dividing 10 by 3rem(100,2) = 0

dozen = fix(N/12);

r= rem(N,12);

Page 22: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Summary

• Computer hardware & software

• How to write a program?

• Building our first program

• Memory

• Getting input

• Displaying output

• Arithmetic expressions

• Sample programs

Page 23: Lecture 1 First Program - Cairo University · Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions. ... Bus CPU Input/Output Devices

Thank You

Course Site: http://scholar.cu.edu.eg/?q=eldeib/classes/genn004-computers-engineers

Computers for Engineers – GENN004