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

(a) Create a Vector of 100 random numbers from 0 to 2. Suppress your answers. (b

ID: 1766018 • Letter: #

Question

(a) Create a Vector of 100 random numbers from 0 to 2. Suppress your answers.

(b) Using a for loop, randomly pull out 10 numbers and place them in to a new vector using indexing. Remember that the rand() function will only give you values in decimal notation, however, to randomly choose indexes, you will have to use whole numbers instead of decimal numbers. Suppress your answers.

(c) Then, inside the for loop, using if statements complete the following logical arrangements: if the pulled out number is equal to or between /4 and 3/4 change it to a 100 in the new vector. For the numbers that are not equal to or that are not between /4 and 3/4 round them to the next closest integer in the new vector. Suppress your answers.

d) Display the new vector using the display function.

(e) Using another for loop sum all the number that are not equal to 100 of the new vector. You may have to refer to the lecture notes. Suppress your answers.

(f) Formally output the sum using the fprintf() in MATLAB.

(g) In a short paragraph describe how you could use For Loops in real life. That is, how are they helpful with calculations, numerical computations, etc. Be sure to include a specific example of how For Loops are used in real life scenarios.

Explanation / Answer

clc
clear all
close all
xmin=0;
xmax=2*pi;
n=100;
x=xmin+rand(1,100)*(xmax-xmin); % generates random number between 0 to 2pi
for i=1:10
y(i)=x(randi(100,1)); % generates random integer value with in 100 range
if(y(i)>=pi/4 && y(i)<=(3*pi/4))
y(i)=100;
else
y(i)=floor(y(i));
end
end
disp('The vector is: ')
disp(y)
sum=0;
for j=1:10
if(y(j)~=100)
sum=sum+y(j);
end
end
fprintf('The sum of numbers other than 100 is: %d ',sum)

Output:

The vector is:
2 5 6 6 5 100 3 0 5 5

The sum of numbers other than 100 is: 37

The vector is:
4 3 4 2 100 100 4 5 4 100

The sum of numbers other than 100 is: 26

g) In real life any item with certain life span can be included into for loop

Ex: life span of a battery. if battery is conditioned well it runs for a certain time, if not it breaks in between.