Question

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...

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.

Solutions

Expert Solution

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.


Related Solutions

K CONSTANT EQUILIBRIUM What does it mean when K=1 and when K= 0
K CONSTANT EQUILIBRIUM What does it mean when K=1 and when K= 0
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele)...
Translate the following C++ code to Pseudocode: int main() { stack<char> stk,stk2; int length =0,ele; while(cin>>ele) { stk.push(ele); length++; } if(length%2) stk.pop(); for (int i=0;i<length/2;i++) { ele=stk.top(); stk.pop(); stk2.push(ele); } int flag=1; for(int i=0;i<length/2;i++) { if(stk.top()==stk2.top()) { stk.pop();stk2.pop(); } else { flag=1; break; } } if(flag==1) cout<<"NOT palindrome"; else cout<<"palindrome"; }
Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
What does the RISC-V code below do? Write the C equivalent. Assume that i is in...
What does the RISC-V code below do? Write the C equivalent. Assume that i is in register x5 and that the base address of array A that holds doubleword integers is in x20. addi x5, x0, 0 addi x6, x0, 50 addi x28, x20, 0 loop: bge x5, x6, end ld x7, 0(x28) bge x7, x0, next sub x7, x0, x7 sd x7, 0(x28) next: addi x5, x5, 1 addi x28, x28, 8 jal x0, loop end: Can you rewrite...
From 1+K*L(s)=0 L(s) = 1/((s+1)(s+2)(s+10)) Solve for gain K where the root locus passes through the...
From 1+K*L(s)=0 L(s) = 1/((s+1)(s+2)(s+10)) Solve for gain K where the root locus passes through the damping ratio. z=0.176. With out Matlab. Please show all work. Note: I know we need to use some trig and the magnitude criteria but u cannot seem to figure it out. Thank you!
Plot the function y = 10(1 = e-x/4) over the interval 0 ≤ x ≤ xmax, using a while loop to
Plot the function y = 10(1 = e-x/4) over the interval 0 ≤ x ≤ xmax, using a while loop to determine the value of xmax such that y(xmax) = 9.8.Properly label the plot. The variable y represents force in newtons, and the variable x represents time in seconds.
1. For the equation x2+y2−2x−4y−11=0, do the following. (a) Find the center (h,k) and radius r...
1. For the equation x2+y2−2x−4y−11=0, do the following. (a) Find the center (h,k) and radius r of the circle. (b) Graph the circle. (c) Find the intercepts, if any.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT