0% found this document useful (0 votes)
2 views4 pages

Practice Midterm 2

The document contains three MATLAB programming problems. Problem 1 involves using a switch-case structure for arithmetic operations based on a predefined choice, Problem 2 requires classifying a student's letter grade using if-elseif-else statements based on a numeric score, and Problem 3 focuses on creating a 5x5 matrix of random integers and finding its maximum and minimum values using nested loops.

Uploaded by

dcarlosguzman
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)
2 views4 pages

Practice Midterm 2

The document contains three MATLAB programming problems. Problem 1 involves using a switch-case structure for arithmetic operations based on a predefined choice, Problem 2 requires classifying a student's letter grade using if-elseif-else statements based on a numeric score, and Problem 3 focuses on creating a 5x5 matrix of random integers and finding its maximum and minimum values using nested loops.

Uploaded by

dcarlosguzman
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/ 4

Practice Midterm 2

Problem 1
Write a MATLAB script that uses a switch-case structure to perform one
of four arithmetic operations based on a predefined variable choice. The
two numbers involved in the operation are also predefined.
Sample Solution:

num1 = 10;

num2 = 5;

choice = 3; % Change this value to test different cases

switch choice

case 1

result = num1 + num2;


fprintf('Result: %d + %d = %d\n', num1, num2, result);

case 2

result = num1 - num2;


fprintf('Result: %d - %d = %d\n', num1, num2, result);

case 3
result = num1 * num2;

fprintf('Result: %d * %d = %d\n', num1, num2, result);


case 4

if num2 == 0

fprintf('Error: Division by zero is not allowed.\n');

else
result = num1 / num2;
fprintf('Result: %d / %d = %.2f\n', num1, num2, result);

end
otherwise

fprintf('Invalid choice. Please choose a number between 1 and 4.\n');

end

Problem 2
Write a MATLAB script that classifies a student’s letter grade based on a
predefined numeric score. The program must use if, elseif, and else
statements (no switch-case). Predefine a variable score, which is a
number between 0 and 100.
Use if statements to classify the grade:
• 90–100 → Grade A
• 80–89 → Grade B
• 70–79 → Grade C
• 60–69 → Grade D
• < 60 → Grade F
If the score is outside the range 0–100, display an error message.
Sample Solution:

score = 87; % You can change this value to test different scenarios
if score < 0 || score > 100
fprintf('Error: Score must be between 0 and 100.\n');
elseif score >= 90
fprintf('Score: %d → Grade: A\n', score);

elseif score >= 80


fprintf('Score: %d → Grade: B\n', score);

elseif score >= 70


fprintf('Score: %d → Grade: C\n', score);

elseif score >= 60


fprintf('Score: %d → Grade: D\n', score);

else
fprintf('Score: %d → Grade: F\n', score);

end

Problem 3
Write a MATLAB program :

1. Creates a 5×5 matrix of random integers between 1 and 100 using randi.

2. Uses nested for loops to:

o Find the maximum value in the matrix.

o Find the minimum value in the matrix.

3. Come up with a written memory map for the entire nested loop for the first 5
iterations
% Step 1: Create a 5x5 random integer matrix from 1 to 100

A = randi([1, 100], 5, 5);

% Step 2: Initialize max and min with the first element

maxVal = A(1,1);

minVal = A(1,1);

% Step 3: Use nested loops to find the maximum and minimum

for i = 1:5

for j = 1:5

if A(i,j) > maxVal

maxVal = A(i,j);

end

if A(i,j) < minVal

minVal = A(i,j);

end

end

end

% Step 4: Display the matrix and results

disp('Matrix:');

disp(A);

fprintf('Maximum value: %d\n', maxVal);

fprintf('Minimum value: %d\n', minVal);

You might also like