Lecture 1: First Program - Cairo University

Summary

This document is a lecture on computer programming, particularly focusing on the initial concepts of programming using MATLAB. It includes explanations of computer hardware, software, memory, arithmetic expressions, and examples.

Full Transcript

Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions Overview Computer hardware & software How to write a program? Building our first program Memory Getting input Displaying output Arithmetic expres...

Lecture 1 First Program Memory variables, input, disp, arithmetic expressions, math functions Overview Computer hardware & software How to write a program? Building our first program Memory Getting input Displaying output Arithmetic expressions Sample programs Computer Hardware Input/Output Keyboard Screen Hard disk Devices Bus CPU Memory Arithmetic Unit Control Unit Logic Unit (RAM) (ALU) 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. 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 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] Building Our First Program Calculate 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. 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 h →Get 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 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); Memory Memory is divided into bytes x 15 Each byte has its own address y 12 represented by binary numbers sum 45 4 or 8 bytes form a memory A 2 location to store integer (45) or floating (10.2345) numbers A1 -3 Software Programs uses letters numOfBus 4 and decimal numbers to name the memory location (A, A1,..). 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 Get input from user h=input(‘enter height: ’); h 5 Message to the user Memory location to store the input Keyword to hold the program execution to get input from the keyboard >> enter height: 5 Write a Matlab statement to take the circle radius from the user and store it ? in ‘R’. Get an array from user X=input(‘enter array elements: ’); >> enter array elements: [4 5 2 6 8] X(1) 4 X(2) 5 length(X): number of elements X(3) 2 To access the array elements use X(index) X(4) 6 Where index is an integer from 1 to length(X) X(5) 8 ? What is the value of: X(4)= X(6)= Display to Screen disp(h); h 5 Memory location a 102 Keyword to display to display to the screen >> 5 disp(a); >> 102 Arithmetic Expressions Operations x 4 ^ Exponentiation (5^2 is 25) *, / Multiplication and division y 3.5 +, - Addition and subtraction z 2 a 6.5 Examples b 8.5 x=4; y=3.5; z=2; a= x +y – 5 / (3+z); b=a + x / 2; Operations are executed only one at a time according to their precedence. We will study it further in future lectures Calculate area of a rectangle (Program and Testing) h=input(‘enter height: ’); h 5 w=input(‘enter width: ’); w 4 area 20 area = h * w; disp (area); enter height: 5 enter width: 4 20 Calculate area of a circle (Algorithm, Program, Testing) 1. Get radius r r=input(‘enter radius:’); 2. Calculate area = pi*r^2 area =pi*r^2; 3. Display area disp(area); radius 5 area 49.3480 enter radius: 5 49.3480 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 Calculate number of buses to transfer students (Program) numS=input(‘enter number of students:’); busC=input(‘enter bus capacity:’); numB = numS/busC; → numB = ceil(numS/busC); disp(numB); If numS=60 and busC = 50 then numB = 60/50 = 1.2 ??? But it should be 2 ceil(1.2) = 2 ceil round up to nearest integer 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 Calculate number of dozens and remainder (Program) N=input(‘enter a number’); dozen = N/12; → dozen = fix(N/12); r = ?? → r= rem(N,12); disp(dozen); disp(r); fix(5.8) = 5 fix removes the fraction rem(10,3) = 1 rem calculates the reminder of dividing 10 by 3 rem(100,2) = 0 Summary Computer hardware & software How to write a program? Building our first program Memory Getting input Displaying output Arithmetic expressions Sample programs Thank You

Use Quizgecko on...
Browser
Browser