Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
595 B
2
Indexable
Never
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
Leave a Comment