Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The built in MATLAB function MAX (and MIN) will determine both the location and

ID: 3847857 • Letter: T

Question

The built in MATLAB function MAX (and MIN) will determine both the location and value of the maximum (and minimum) element in an 1 times n array. Write a MATLAB script which performs this task from scratch (i.e. you may not use the built in max or min functions). Have your script first prompt the user for the length of the array and then individually prompt him/her for the n numbers exactly as demonstrated below. Then have your script display to the screen the maximum value in the array and the index location. Output should (with the difference of user input) EXACTLY look like the following: Please enter your name == > Jeff Jeff, please enter how many numbers you have == > 4 Please enter number 1. Jeff == > 6 Please enter number 2 Jeff == > 7 Please enter number 3 Jeff == > 4 Please enter number 4 Jeff == > 5 The largest element 7 was found in location 2 The smallest element 4 was found in location 3

Explanation / Answer

Matlab code:

clear all;
name = input('Enter your name','s');
fprintf(name);
n = input(',Enter how many numbers you have');
array = zeros(1,n);
for i = 1:n
fprintf('Please enter number %d', i);
array(i) = input('');
end

min_index = 1;
max_index = 1;
minn = array(1);maxx = array(1);
for i = 1:size(array,2)
if(array(i) < minn)
minn = array(i);
min_index = i;
end
if(array(i) > maxx)
maxx = array(i);
max_index = i;
end
end
fprintf('Largest element %d was found at location %d ',maxx,max_index);
fprintf('smallest element %d was found at location %d',minn,min_index);

Sample Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote