Professional Documents
Culture Documents
CS10-8L: Computer Programming Laboratory: Machine Problem #6: Modular Programs
CS10-8L: Computer Programming Laboratory: Machine Problem #6: Modular Programs
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
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)
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))
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:
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: ’);
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)
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!
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.
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:
lumpsum.m
Script:
inputyears.m
Script:
plotlump.m
Script: