Write a program that (a) generates a vector with 20 random integer elements with integers between 10 and 30, (b) replaces all the elements that are not even integers with random integers between 10 and 30, and (c) repeats

Respuesta :

Answer:

listRand=[];

%Use for loop and randi to

%generate 20 random numbers

for i=1:20

listRand (i)=randi([10, 30]);

end

%Display the random number generated.

fprintf('20 random elements are:\n')

disp(listRand)

%Initialize the variables.

countIterations = 1;%for number of iterations

flagCheck=1;

%Iterate until all numbers are even.

while(flagCheck ==1)

%Update the count.

countIterations = countIterations +1;

%Generate the random number

%and check if it is even

%Continue until even number is generated.

for i =1:20

if(~mod(listRand (i),2)==0)

listRand (i)=randi([10, 30]);

end

end

%Set the flag.

flagCheck =0;

%Check if any odd number is left.

for i=1:20

if(~mod(listRand (i),2)==0)

flagCheck =1;

break

end

end

%Check if any odd number is left.

if(flagCheck ==1)

continue

else

flagCheck =0;

end

end

%Display the number of iterations.

fprintf('\nNo of iterations needed are: %d',countIterations)

fprintf('\n\n')

%Display the vector.

disp(listRand)

Explanation:

The given code is in MATLAB.

ACCESS MORE