CS10-8L: Computer Programming Laboratory: Machine Problem #6: Modular Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

CS10-8L: Computer Programming Laboratory

Machine Problem #6: Modular Programs

OBJECTIVES
 To understand, create, and implement user-defined functions as used in modular 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. Calling a User-Defined Function from a Script
Create the following script, which calls the function we created in the previous exercise, calcarea:

areascript.m
% This script calculates the area of a circle
% It prompts the user for the radius
radius = input(‘Please enter the radius:’);
% It then calls our function to calculate the
% area and then prints the result
area = calcarea(radius);
fprintf(‘For a circle with a radius of %.2f,’,radius)
fprintf(‘the area is %.2f\n’,area)

B. Functions that Return More than One Value


The general form of a function that calculates and returns more than one value:

Prepared by: Geldof Resuello


Prepared date: March 2020
Create the following function:

areacirc.m
function [area, circum] = areacirc(rad)
% This function calculates the area and
% the circumference of a circle
area = pi * rad .* rad;
circum = 2 * pi * rad;

Since the function is returning two values, it is important to capture and store them in separate values. In
the Command Window, call the function using the following:

>> [a c] = areacirc(4)
>> disp(areacirc(4))

C. Functions that Accomplish a Task Without Returning Values


The general form of a function that does not return any values is:

Create the following function:

printem.m
function printem(a,b)
% This function prints two numbers in a sentence format
fprintf(‘The first number is %.1f and the second is %.1f\n’,a,b)

Since the function isn’t calculating anything, there aren’t any output arguments in the function header and
no “=”.

>> printem(3.3,2)
>> x = printem(3.3,2)

D. Modular Programs
In a modular program, the solution is broken down into modules, and each is implemented as a function.
The script is typically called the main program.

Example:
In calculating the area of a circle, there are three steps:
 Get the input (the radius)
 Calculate the area
 Display the results
In a modular program, there would be one main script that calls three separate functions to accomplish
each task.
Create the following script/ functions:

Prepared by: Geldof Resuello


Prepared date: March 2020
calcandprintarea.m
% This is the main script to calculate the
% area of a circle
% It calls 3 functions to accomplish this
radius = readradius;
area = calcarea(radius);
printarea(radius,area)

readradius.m
function radius = readradius
%This function prompts the user and reads the radius
disp(‘When prompted, please enter the radius in inches.’)
radius = input(‘Enter the radius: ’);

calcarea.m % No need to create as we have already created this


function area = calcarea(rad)
% This function calculates the area of a circle
area = pi * rad * rad;

printarea.m
function printarea(rad,area)
% This function prints the results
fprintf(‘For a circle with a radius of %.2f inches,\n’,rad)
fprintf(‘the area is %.2f inches squared.\n’,area)

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 MP06_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.

Create the scripts for the following problems.

1.) lovedicegame.m
Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which
would represent the result of rolling a die. The program would then generate a random integer between
1 and 6 and compare its value to the value entered by user.
If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!”
If the user’s die is smaller, it should display, “Gising na friend, di ka niya mahal!”
If the results are the same, it should display, “Move on na ghorl”.

The program should ask if the user wants to play another game (to be answered by either ‘Y’ or ‘N’). If
the user answers ‘Y’, the game continues. If the user answers ‘N’, the game ends and it will display,
“Mabuti naman at natauhan ka na!”.

The program should have a main script named lovedicegame.m and calls on the following functions:

a. A function readdice.m to prompt the user and read in the integer input.
b. A function genrandomdice.m to generate a random integer between 1 and 6.
c. A function comparedice.m to compare the input and the generated integer and display the
results.

Sample run:

>> lovedicegame
>> Do you want to play the lovedice game (Y/N)? Y
>> Enter your dice: 5
>> My dice result: 6
>> Gising na friend, di ka niya mahal!
>> Play again (Y/N)? Y
>> Enter your dice: 3
>> My dice result: 1
>> Mahahanap mo na ang forever mo. Sana all!
>> Play again (Y/N)? Y
>> Enter your dice: 2
>> My dice result: 2
>> Move on na ghorl!
>> Play again? N
>> Mabuti naman at natauhan ka na!

Run the game for values:


3 1 4 1 6

Prepared by: Geldof Resuello


Prepared date: March 2020
2.) lumpsum.m
The lump sum S to be paid when interest on a loan is compounded annually is given by
n
S=P ( 1+i )
where P is the principal invested, i is the interest rate, and n is the number of years.

Write a modular program that will plot the amount S as it increases through the years from 1 to n. The
main script lumpsum.m will call a function inputyears.m to prompt the user for the number of years
(and error-check to make sure that the user enters a positive integer).

The script will then call a function plotlump.m that will plot S for years 1 through n. It shall use 0.05 for
the interest rate and Php100,000 for P.

Run your program using:


Number of Years = - 5
Number of Years = 10

Prepared by: Geldof Resuello


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

Machine
6 (Modular Programs) Score
Problem #  
Name Section
 

1.
Command Window Results:

lovedicegame.m
Script:

readdice.m
Script:

genrandomdice.m
Script:

comparedice.m
Script:

2.
Command Window Results:

Plot Results (for number of years = 10):

lumpsum.m
Script:

inputyears.m
Script:

plotlump.m
Script:

Prepared by: Geldof Resuello


Prepared date: March 2020
Prepared by: Geldof Resuello
Prepared date: March 2020

You might also like