In: Computer Science
hat does the code below do?
clear vec = randi(20,1,100)-10; iter=0; k=1; while k<= length(vec) if vec(k)<0 vec(k)= vec(k) * -1; end k = k+1; end
a.
This code generates a vector with 100 random integer elements between 20 and 100, and changes the sign of the elements coverting all poivitive ones to negative and all negative ones to positive.
b.
This code generates a vector with 20 random integer elements between 1 and 100, and changes the sign of all the elements.
c.
This code generates a vector with 20 random integer elements between -100 and 100, and replaces all the negative integers with a positive integer of the same absolute value.
d.
This code generates a vector with 100 random integer elements between -10 and 10, and changes the sign of all negative elements.
e.
This code generates a vector with 20 random integer elements between 1 and 100, and changes the sign of all negative elements.
The answer is D as it generates a vector with 100 random integer elements between -10 and 10, and changes the sign of all negative elements.
In this, we have taken a smaller array to understand. In this code, randi generates an array with 1 row and 10 columns with max element is array be 20 and minimum element be 1. Then we subtract 10 from every element.
And then our code when traversing through the array and if it finds a negative number then it changes it to a positive number by multiplying -1 to it.
Now I have displayed old x when our code has not converted a positive integer to negative. and then I have displayed a new x.
YOUR CODE'S OUTPUT:
ALL POSITIVE INTEGERS
Please Upvote
If you face any problem comment below.