2. Overview
● Review: How to write a program?
● Substitution in mathematics & programming
● Building our first program
● Memory
● Getting input
● Displaying output
● Arithmetic expressions
● Sample programs
3. Substitution in mathematics & programming
● Software evolved first to serve solving
mathematical problems
● Therefore, it follows the main concepts of
evaluating expressions
● Substitution in mathematical expressions
works the same way in code
4. Substitution in mathematics & programming
𝑥 = 4
𝑦 = 𝑒 𝑥
● Solving the above mathematically by
substituting manually step by step:
𝑦 = 𝑒√4
𝑦 = 𝑒2
𝑦 = 2.7182
𝑦 = 7.38
5. Substitution in mathematics & programming
● In MATLAB, it is written as:
x = 4;
y = exp(sqrt(x));
● By substituting step by step:
1. First 4 is assigned to x
2. Then x is substituted for, y = exp(sqrt(4))
3. Function sqrt(4) is called which takes 4 as an input, gets
evaluated and returns back 2, now y = exp(2)
4. Function exp(2) is called which takes 2 as an input, gets
evaluated and returns back 7.38 which gets assigned to y
Exercise: x = 4; y = cos(exp(x/2-2)*pi)/sin(pi/2);
6. 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]
7. 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
8. 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);
9. 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 or
floating (Ex 10.2345) numbers
•Software Programs uses letters
and decimal numbers to name
the memory location.
15
12
45
2
-3
4
x
y
sum
A
A1
numOfBus
10. Memory Location Name
(Variables)
•Variable: a named space for storing a value
radius
•Valid names start with a letter, can contain
digits.
•Use meaningful variable names.
•MATLAB is case sensitive (area and Area are different)
Xyz 3xyz x-y ab2cd a12345 myvar AbCdE1234?
11. 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
12. 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)
?
14. Arithmetic Expressions
Operations
^ Exponentiation (5^2 is 25)
*, / Multiplication and division
+, - Addition and subtraction
Examples
x=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
15. Calculate area of a rectangle
(Program and Testing)
h=input(‘enter height: ’);
w=input(‘enter width: ’);
area = h * w;
disp (area);
enter height: 5
enter width: 4
20
5
4
20
h
w
area
16. 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: 5
49.3480
5
49.3480
radius
area
17. 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
18. 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
19. 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
20. 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 3
rem(100,2) = 0
dozen = fix(N/12);
r= rem(N,12);