You are on page 1of 4

CS10-8L: Computer Programming Laboratory

Machine Problem #3: Input and Output

OBJECTIVES
 To provide supplementary information on arrays.
 To enable the students to ask for input and display output in their programs.

GRADING SYSTEM

From (%) To (%) Grade

0.00 64.99 5.00


65.00 68.89 3.00
68.9 72.89 2.75
72.9 76.79 2.50
76.8 80.69 2.25
80.7 84.59 2.00
84.6 88.49 1.75
88.5 92.39 1.50
92.4 96.29 1.25
96.3 100.00 1.00

INSTRUCTIONS
A. More on Arrays and Array Operations
1. Try the following commands:
>> w=zeros(5) <Enter>
>> x=zeros(4,3) <Enter>
>> y=ones(6) <Enter>
>> z=ones(size(x)) <Enter>
>> a=eye(5) <Enter>
>> b=eye(3,4) <Enter>
>> length(x) <Enter>
>> size(b) <Enter>
>> arr1=[1:8] <Enter>
>> arr1(5:end) <Enter>

2. Matrix Operations
>> x=rand(3,4) <Enter>
>> y=rand(4,5) <Enter>
>> x*y <Enter>
>> y*x <Enter>
>> a=10*rand(3,2) <Enter>
>> b=10*rand(3,2) <Enter>
>> a.*b <Enter>
>> b.^a <Enter>
B. Documentation

Prepared by: Geldof Resuello


Prepared date: March 2020
1. Create the following script:

script3a.m
%This program calculates the area of a circle

%First the radius is assigned


radius = 5
%The area is calculated based on the radius
area = pi*(radius^2)

Then run the following in the Command Window:


>> help script3a <Enter>

C. Input function
1. Try the following:

>> rad = input(‘Enter the radius: ‘) <Enter>


>> letter = input(‘Enter a char: ‘,’s’) <Enter>
>> mystr = input(‘Enter a string: ‘, ‘s’) <Enter>
>> length(mystr) <Enter>
>> num = input(‘Enter a number: ‘) <Enter>

D. Displaying output data


1. To display output data, try the following:

>> disp(‘Hello’) <Enter>


>> disp(4^3) <Enter>
>> fprintf(‘The value of pi is %f \n’, pi) <Enter>
>> fprintf(‘The value is %d, for sure! \n’, 4^3) <Enter>

Note: %d and %f are called placeholders that indicate the type of value that is being printed.
%d integers (it actually stands for decimal integer)
%f floats
%c single characters
%s strings

Try the following:

>> fprintf(‘The int is %3d and the float is %6.2f\n’, 5, 4.9)


>> fprintf(‘%3.2f\n’, 1234.5678)
>> fprintf(‘%d\n’, 1234567.89)
>> fprintf(‘The integer is xx%5dxx and xx%-5dxx\n’, 3,3)
>> fprintf(‘The string is %s or %.4s\n’, ‘truncate’,’truncate’)
>> fprintf(‘Try this out: tab \t quote “ slash \\ \n’)

Prepared by: Geldof Resuello


Prepared date: March 2020
MACHINE PROBLEM
Answer the following problems using commands in Matlab. Make sure to indicate the commands in your
submission, as specified in the Answer Sheet. Important: Only upload the Answer Sheet (i.e. exclude the
Instructions part). Save the file as MP03_Lastname.pdf Check the deadline indicated in Blackboard.
Failure to submit on time will incur an automatic grade of 0 while failure to follow instructions will incur
deductions.

1. Create the following variables:


>> x = 12.34;
>> y = 4.56;
Then fill in the fprintf command statements using these variables that will generate the following results
a. “x is 12.340”
b. “x is 12”
c. “y is
4.6”
d. “y is 4.56 !!!”

2. Create the following scripts:


a. areaofrectangle.m
Write a script that will prompt the user for the length and width of a rectangle and print its area with two
decimal places. Put comments in the script.
Run your script and use length = 5.34 and width = 4.56

b. translatethis.m
On average, people in a region spend 8 to 10% of their income on food. Write a script that will prompt
the user for an annual income. It will then print the range that would typically be spent on food annually.
Run your script and put P205,000 as the annual income.

c. quadraticformula.m
Write a program that would ask for the values of a, b, and c, then calculate for x, where
−b ± √ b2−4 ac
x=
2a
Run your script and input a = 2, b = -10, c = 12

d. temperature.m
If C and F are Celsius and Fahrenheit temperatures respectively, the formula for conversion from
Celsius to Fahrenheit is F = 9C/5 + 32.
Write a script that will ask you for the Celsius temperature and display the equivalent Fahrenheit
temperature.Try it out on values 0, 100, -40 and 37.

Prepared by: Geldof Resuello


Prepared date: March 2020
IT131-8L Machine Problem Answer Sheet

Machine
3 (Input and Output) Score
Problem #  
Name Section
 

1.
a. Command:

b. Command:

c. Command:

d. Command:

2.
a. areaofrectangle.m
Command Window:

Script:

b. translatethis.m
Command Window:

Script:

c. quadraticformula.m
Command Window:

Script:

d. temperature.m
Command Window:

Script:

Prepared by: Geldof Resuello


Prepared date: March 2020

You might also like