Untitled
unknown
plain_text
a year ago
595 B
9
Indexable
function index = linearSearch(array, target)
index = -1;
for i = 1:length(array)
if array(i) == target then
index = i;
break;
end
end
endfunction
numElements = input("Enter the number of elements in the array: ");
array = zeros(1, numElements);
for i = 1:numElements
array(i) = input("Enter element " + string(i) + ": ");
end
target = input("Enter the element to search for: ");
index = linearSearch(array, target);
if index == -1 then
disp("Element not found.");
else
disp("Element found at position: " + string(index));
end
Editor is loading...
Leave a Comment