Part II -Review for Final Exam . The purpose of writing these programs is to pra
ID: 3703916 • Letter: P
Question
Part II -Review for Final Exam . The purpose of writing these programs is to practice indexing through a vector using loops. . For each of these problems, do not use the MATLAB commands: sort, sum, min, and max. . Use the following vector for all problems: V 17 9 -89 3-8-5 1 10 10 0 -7] Note: You should use the MATLAB command length(V) to allow you to determine the length of a generic vector. Use this value as the STOP value in your for loop. for k- START:1:STOP end Problem 1: Write a script file that will determine how many entries in a vector are larger than some specified value. The user should input the specified value. The outputs will be the following statement: "The number of entries larger than (display your specified value here) is: (display the number of entries here)". You should use Jprintf to display the statement with your values embedded. Paste below the script file and the fprintf outputs using the vector, V, and the specified value of 3 Problem 2: Write a script file that will find the sum of all the positive entries in V and the sum of all the negative entries in V. An fprintfstatement should be used to display the value for the sum of all the positive entries and the sum of all the negative entries. Reminder: don't use the built-in function, sum Paste below the script file and the fprintf outputs using the vector: V statement should be used to display the largest entry and the smallest entry Reminder: don't use the built-in functions max or min Paste below the script file and the fprintf outputs using the vector: DExplanation / Answer
Problem 1:
V = [ 7 9 -8 9 3 -8 -5 1 10 10 0 -7 ];
prompt = 'Enter a number : ';
% get user input
n = input(prompt);
count = 0;
% traverse through V
for i = 1 : length(V)
% if current element is greater than n
if V(i) > n
count = count + 1;
end
end
fprintf(' No of elements greater than % d : %d ', n, count);
Sample Output
Enter a number : 4
No of elements greater than 4 : 5
Problem 2:
V = [ 7 9 -8 9 3 -8 -5 1 10 10 0 -7 ];
% store sum of positive numbers
pos_sum = 0;
% store sum of negative numbers
neg_sum = 0;
% traverse through V
for i = 1 : length(V)
% if current element is positive
if V(i) >= 0
pos_sum = pos_sum + V(i);
% if current element is negative
else
neg_sum = neg_sum + V(i);
end
end
fprintf(' Sum of positive numbers : %d ', pos_sum);
fprintf('Sum of negative numbers : %d ', neg_sum);
Sample Output
Sum of positive numbers : 49
Sum of negative numbers : -28
Problem 3:
V = [ 7 9 -8 9 3 -8 -5 1 10 10 0 -7 ];
% store maximum element
max_val = V(1);
% store minimum element
min_val = V(1);
% traverse through V
for i = 2 : length(V)
% if current element is greater than max_val
if V(i) > max_val
max_val = V(i);
end
% if current element is smaller than min_val
if V(i) < min_val
min_val = V(i);
end
end
fprintf(' Minimum element : %d ', min_val);
fprintf('Maximum element : %d ', max_val);
Sample Output
Minimum element : -8
Maximum element : 10