Question

write a Matlab program ( solve the three questions). please use array and create your own function to check primality Questio
0 0
Add a comment Improve this question Transcribed image text
Answer #1

%% question 1

% This program finds first 1000 prime numbers and store in the array
% arr_of_primes
clc
clear all

arr_of_primes = []; % Array of prime numbers
N = 2; % starting with N = 2
while length(arr_of_primes)<1000 % while no of elements in P is less than 1000
logic = ifPrime(N); % check if N is prime number
% logic = 1, if N is prime number
% logic = 0, if N is not a prime number
if(logic==1) % if N is prime number, store it in array P
arr_of_primes = [arr_of_primes, N];
end
N = N +1; % check next number
end

%% fuction file

function logic = ifPrime(N)
logic = 1; % assume N is prime number
% 2 is a prime nuber, no need to check
% if N is not 2, check if it is not a prime number
if(N~=2)
% first devide by 2, if remender is zero,it is not a prime no
% otherwise apply next check
r = mod(N,2); % remender when devide N by 2
q = floor(N/2); % quotent when devide N by 2
if(r==0) % if remender when devide by 2 is zero, it is not a prime no
logic = 0;
else if(N>3) % Else if number is greater than 2, do fuether check
% start deviding with 3
div = 3; % div is divisor variable
while div < q
r = mod(N,div);
q = floor(N/div);
if(r==0)
logic = 0;
break
else
div = div + 2;
end
end
end
end
end
end

/////////////////////////////////////////////////////////////////////////////// (2)

function [ arr_des, arr_asc ] = sort_descending_ascending( arr )
% This function arrange input array into in ascending and descending order
% Only non-negative integer should be input to this function

% assign first element of arr to variables arr_des and arr_asc
arr_des = arr(1);
arr_asc = arr(1);

% Arranging in accending and descending order one-by-one element
for k = 2:length(arr)
% descending order
if(arr(k)<=arr_des(k-1)) % if upcoming element is greater than last element in array arr_des
arr_des = [ arr_des, arr(k)];
else % otherwise, find the right postion in array arr_des and put it at right place
j = 1;
while arr_des(j)>arr(k)
j = j + 1;
end
arr_des = [arr_des(1:j-1), arr(k), arr_des(j:end)];
end
% ascending order
if(arr(k)>=arr_asc(k-1)) % if upcoming element is less than last element in array arr_asc
arr_asc = [ arr_asc, arr(k)];
else % otherwise, find the right postion in array arr_des and put it at right place
j = 1;
while arr_asc(j)<arr(k)
j = j + 1;
end
arr_asc = [arr_asc(1:j-1), arr(k), arr_asc(j:end)];
end
end
end

Command Window >> clear all >> [arr_des, arr_asc] - sort_descending_ascending([1 5 9 0 7 3 64 9 4 6 17]) arr_des - 764 17 9 9

Add a comment
Know the answer?
Add Answer to:
write a Matlab program ( solve the three questions). please use array and create your own...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT