0% found this document useful (0 votes)
89 views

Greedy Algoritma

Here are three executions of the program with outputs: [EXECUTION 1] The amount is $24.15 Please pay to checkout : $50 The exchange is : $25.85 Amount in words : twenty five dollars and eighty five cents [EXECUTION 2] The amount is $12.34 Please pay to checkout : $20 The exchange is : $7.66 Amount in words : seven dollars and sixty six cents [EXECUTION 3] The amount is $49.99 Please pay to checkout : $50 The exchange is : $0.01 Amount in words : one cent

Uploaded by

Agra Arimbawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Greedy Algoritma

Here are three executions of the program with outputs: [EXECUTION 1] The amount is $24.15 Please pay to checkout : $50 The exchange is : $25.85 Amount in words : twenty five dollars and eighty five cents [EXECUTION 2] The amount is $12.34 Please pay to checkout : $20 The exchange is : $7.66 Amount in words : seven dollars and sixty six cents [EXECUTION 3] The amount is $49.99 Please pay to checkout : $50 The exchange is : $0.01 Amount in words : one cent

Uploaded by

Agra Arimbawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

KOMPUTASI NUMERIK

KELOMPOK 4

OLEH:
I PUTU AGRA ARIMBAWA (1929101032)
ANAK AGUNG DIAN WIDYA ASTUTI (1929101033)
AGUS CHANDRA ARYADI (1929101034)
I KETUT SUMANTRA (1929101035)
NYOMAN SUTRISNA JANUREKSA (1929101036)
I PUTU ASHADI SEDANA PRATAMA (1929101037)
LUH PUTU DIAH KUSUMADEWI 1929101038)

TAHUN
2019
7. A symmetric (5 x 5) Pascal matrix is displayed on the right. Write a
MATLAB program that creates an n x n symmetric Pascal matrix. Use
the program to create 4 x 4 and 7 x 7 Pascal matrices.

Source Code :

%{
Create a symmetric Pascal matrix n x n size.
n inputted by user.
Input : n
Output: matrix n x n
%}

% Clear memory and screen


clear;
clc;

% User input size of n x n matrix


n = input('Please input matrix size : ');
% The matrix initialized by 1 for all value
M = ones(n,n);
temp = 0;
for i = 2:n
for j = 2:n
%{
Put the value of previous row with same column at
temporary
variable
%}
temp = M(i, j-1) + M(i-1, j);
% Fill the current value with temporary value
M(i,j) = temp;
end
end
% Display matrix result
disp(M);

Output Matrix 4 x 4
Output Matrix 7 x 7

14. The value of 1t can be estimated from the expression:

Write a MATLAB program in a script file that determine 1t for any number of terms.
The program asks the user to enter the number of terms, and then calculates the
corresponding value of 1t. Execute the program with 5, 10, and 40 terms. Compare the
result with pi. (Use format long.)

Source Code :
%{
Create the approximation pi and compare the value with pi.
inputLength inputted by user.
Input : inputLength
Output: the approximation pi and compare value
%}

% Clear memory and screen


clear
clc

% User inputted data length


inputLength = input('Please input data length : ');
% Call void function called approximatePi with inputLength as
parameter
approximatePi(inputLength)

% Function approximate pi value


function approximatePi(n)
% Initiate const, temp, result variable
const = 2;
temp = 0;
result = 1;
% Looping process and execute approximate pi calculation
for i = 1:n
temp = sqrt(const + temp);
result = (result * temp) / const;
end;

% Calculate to get the pi approximation


piApprox = const / result;

%{
Display approximate pi and display the difference value
between pi
and the approximation. Display as long format.
%}
fprintf('For input length = %d the approximation is
%1.10f.\n', n, piApprox);
fprintf('The difference between the pi and the approximation
is %1.10f.\n', abs(pi-piApprox));
end

Output Execute the program with ‘5 ‘

Output Execute the program with ‘10 ‘

Output Execute the program with ‘40 ‘


19. The Pythagorean theorem states that a 2 + b2 = c2. Write a MATLAB program in a
script file that fmds all the combinations of triples a, b, and c that are positive integers
all smaller or equal to 50 that satisfy the Pythagorean theorem. Display the results in
a three-column table in which every row corresponds to one triple. The first three rows
of the table are:

Source Code :

% Prevent the user not inputted value smaller than 1


if (n < 1)
fprintf('Please input maximum value more than 0\n');
% Prevent the user not inputted value greater than 50
elseif (n > 50)
fprintf('Please input maximum value not more than 50\n');
% Execute matrix
else
% The matrix initialized by 0 for all value
M = zeros(n,3);
row = 1;
for i=1:n
for j=i:n
% Pythagorean theorem calculation
csqr = (i^2) + (j^2);
% Square root the result of calculation
c = sqrt(csqr);
%{
Compare the result of calculation with square root
the
result of the calculation. If the compare is the
same will fill
the current matrix value with the i, j, c value.
After that will increment the row pointer value.
%}
if ((ceil(c) == sqrt(csqr)) && (c <= 50))
M(row, 1) = i;
M(row, 2) = j;
M(row, 3) = c;
row = row + 1;
end
end
end

% Remove zero rows


M(~any(M,2), :) = [];
% Remove zero columns
M(:, ~any(M,1)) = [];
% Display matrix result
disp(M)
end
Output Execute the program with ‘25 ‘

Output Execute the program with ‘50 ‘

24. Write a program that determines the change given back to a customer in a self-service
checkout machine of a supermarket for purchases of up to $50. The program generates
a random number between 0.01 and 50.00 and displays the number as the amount to
be paid. The program then asks the user to enter payment, which can be one $1 bill,
one $5 bill, one $10 bill, one $20 bill, or one $50 bill. If the payment is less than the
amount to be paid, an error message is displayed. If the payment is sufficient, the
program calculates the change and lists the bills and/or the coins that make up the
change, which has to be composed of the least number each of bills and coins. For
example, if the amount to be paid is $2.33 and a $10 bill is entered as payment, then
the change is one $5 bill, two $1 bills, two quarters, one dime, one nickel, and two
pennies. Execute the program three times.

Source Code :

%{
Generate random numbers with two decimal places between 0.01
until 50.00.
The generated number becomes the amount and users need to pay
by inputted pay amount.
Users will get the amount in words from the change.
Input : pay amount.
Output: amount in words.
%}

% Clear memory and screen


clear
clc

% Initiate number as string


number = char('one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'ten');
% Initiate bills div
bills = [50 20 10 5 1 0.25 0.1 0.05 0.01];
min=0.01;
max=50.00;
% Create random number between 0.01 until 50.00
randomNumber = min+rand()*(max-min);
% Set two decimal places
amount = round(randomNumber, 2);

fprintf('The amount is $%.2f\n', amount);


pay=0;
%{
Ask the user to input pay amount, prevent user inputted
payment amount smaller
than amount
%}
while ~pay
% Input pay amount
pay = input('Please pay to checkout : $');
if (pay < amount)
fprintf('Your payment is not enough, supposed to be
greater or equal to $%.2f\n', amount);
pay=0;
end
end

% Calculate payment and initiate some variables


changeAmount = pay - amount;
change = changeAmount * 100;
divValue = 0;
modValue = 0;
message = '';
if (changeAmount > 0)
% Create and concatenate amount of words from the change
amount
for i = 1:length(bills)
% Multiple by 100 to make it easy to get div and mod value
changeCompare = bills(i) * 100;

% Check the change value is greater of equal to change


compare value
if (change >= changeCompare)
% Get the div value
divValue = floor(change / changeCompare);
% Get the mod value
modValue = round(change - (divValue * changeCompare));

% Selected the bills condition at bills array


if (bills(i) == 50)
message = strcat(message, number(divValue, :), '
$50 Bill, ');
elseif (bills(i) == 20)
message = strcat(message, number(divValue, :), '
$20 Bill, ');
elseif (bills(i) == 10)
message = strcat(message, number(divValue, :), '
$10 Bill, ');
elseif (bills(i) == 5)
message = strcat(message, number(divValue, :), '
$5 Bill, ');
elseif (bills(i) == 1)
message = strcat(message, number(divValue, :), '
$1 Bill, ');
elseif (bills(i) == 0.25)
if (divValue > 1) % plural quarter
message = strcat(message, number(divValue, :),
' quarters, ');
else % singular quarter
message = strcat(message, number(divValue, :),
' quarter, ');
end
elseif (bills(i) == 0.1)
message = strcat(message, number(divValue, :), '
dime, ');
elseif (bills(i) == 0.05)
message = strcat(message, number(divValue, :), '
nickel, ');
elseif (bills(i) == 0.01)
if (divValue > 1) % plural penny
message = strcat(message, number(divValue, :),
' pennies, ');
else % singular penny
message = strcat(message, number(divValue, :),
' penny, ');
end
end

% Replace the value of change by mod value


change = modValue;
end
end
else
message = 'zero,';
end

% Display the change amount


fprintf('The exchange is : $%.2f\n', changeAmount);
% Display the change amount
fprintf('The exchange is : $%.2f\n', changeAmount);
% Remove last character to make it clear word
clearMessage = message(1:end-1);
% Display clear message of change amount
fprintf('Amount in words : %s\n', clearMessage);

Output Execute the program amount to be paid is ‘$30 ‘

Output Execute the program amount to be paid is ‘$60 ‘

Output Execute the program amount to be paid is ‘$46‘

You might also like