Student's Name:
ID#
Date of Assignment:
Group:
College of Engineering
Electrical Engineering Department
Computer Programming
(CSC111)
Semester 462 (2025)
Assignment #2
Exercise-1: Knowledge Review. (0.6 Marks)
1) Can user-defined functions in MATLAB be nested within other user-defined functions?
Furthermore, are user-defined functions executed in the same manner as MATLAB's built-in
functions? If your response is no, please provide an explanation. (0.2)
Solution:
No, user-defined functions in MATLAB cannot be nested in the same way as built-in functions.
Each function is typically defined separately in its own file or within a script.
Built-in functions are predefined in MATLAB and optimized for performance, while user-defined functions re
2) In what situations would you choose to use the fprintf function instead of the disp function? Please
provide your reasons. (0.2)
Solution:
The fprintf function is used when formatted output is required, such as specifying decimal places or embed
The disp function is used for simple value display without formatting.
fprintf is preferable when printing multiple variables in a structured way, while disp is useful for quick output
3) Let A and B be two matrices of size 3×3 (0.2)
Do you think the following two MATLAB expressions will result in the same output?
C=A*B; %(Matrix multiplication)
C=A.*B; %(Element-wise multiplication)
If your answer is "No," provide an explanation of why the results will be different.
Solution:
No, the results will be different. The first operation (C = A * B) performs matrix multiplication following stand
where each element in the resulting matrix is obtained by multiplying and summing rows and columns.
The second operation (C = A .* B) performs element-wise multiplication, where corresponding elements of
Exercise-2: Basic Practice Review. (0.6 Marks)
1) Write the output results F and R for the following MATLAB code: (0.2)
MATLAB CODE:
X = [ 2 12 5; 4 7 9 ; 13 8 3];
F= isprime(X);
R= sum(F);
disp(F);
disp(R);
Solution:
F=
1 0 1
0 1 1
1 0 1
R=
2 1 3
2) Complete the following MATLAB code by adding two missing fprintf functions to display
the specified output lines. You must utilize the provided variables appropriately. (0.2)
MATLAB CODE:
planet = 'Mars';
moons = 2;
gravity = 3.71; % in unit of m/s²
feature = 'Red Dust Storms';
% Solution:
% Missing fprintf statement for the first output
fprintf('%s has %d moons and a gravity of %.2f meters per second squared.\n', planet, moons, gravity);
% Missing fprintf statement for the second output
fprintf('The planet is known for its %s.\n', feature);
3) Which of the following three MATLAB codes are correct, and which are incorrect? Please
provide your reasons. (0.2)
Solution:
MATLAB CODE 1:
Str = 'Signals & Systems';
L = length(Str);
disp(Str);
disp(L);
% Correct: Defines a string variable and calculates its length.
MATLAB CODE 2:
A = 5:1:10;
B = 2:1:6;
S = A + B;
% Incorrect: A and B have different lengths, causing an error when added.
MATLAB CODE 3:
V = 5:1:10;
K = 2:1:6;
H = V * K;
% Incorrect: Matrix multiplication requires compatible dimensions.
MATLAB CODE 4:
Y = 3:0.25:4;
D = 1:0.25:2;
Z = Y.^D;
% Correct: Raises elements of Y to the corresponding powers in D.
Exercise-3: Coding MATLAB Scripts. (0.8 Marks)
1) Write a MATLAB program that prompts the user to enter a non-negative integer. The
program should then calculate the cube of the entered number and identify all the prime
factors of the resulting cube. (0.3)
Solution:
n = input('Enter a non-negative integer: ');
cube = n^3;
factors = factor(cube);
fprintf('Cube of %d: %d\n', n, cube);
fprintf('Prime factors of %d: ', cube);
disp(factors);
2) Create a user-defined function named GCD_LCM that calculates both the Greatest Common
Divisor (GCD) and the Least Common Multiple (LCM) of two integers. The function should
take two integers a and b as inputs and return both the GCD and the LCM. (0.3)
Solution:
function [gcd_val, lcm_val] = GCD_LCM(a, b)
gcd_val = gcd(a, b);
lcm_val = lcm(a, b);
end
3) Create a user-defined function named Join_Date that concatenates the day, month, and year into a
single string in the format day-month-year. The function should take three strings, day, month,
and year, as inputs and return the concatenated date. (0.2)
Solution:
function concatenatedDate = Join_Date(day, month, year)
concatenatedDate = strcat(day, '-', month, '-', year);
end