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

Mathlab Q1. Instruction Text % For loop code: vec = [45, -1, 7, 0, -37, 4,...
Mathlab Q1. Instruction Text % For loop code: vec = [45, -1, 7, 0, -37, 4, -3]; newvec = zeros(1,numel(vec)); % pre-allocate newvec with zeros for idx = 1:numel(vec) if vec(idx) > 1 & vec(idx) < 0 numerator = 3*vec(idx)^3; denominator = 9*vec(idx)^2 + 3; else numerator = 2*vec(idx)^3 - 2*vec(idx); denominator = 2*vec(idx)^2 - 2/vec(idx); end newvec(idx) = numerator/denominator; end
How many asterisks does the following code fragment print? a = 0 while a < 100:...
How many asterisks does the following code fragment print? a = 0 while a < 100: b = 0 while b<55:    print('*', end='')    b += 1   print() a += 1
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
x = 1:0.5:10 and a = 7.5, what does the following code do? (x > a)...
x = 1:0.5:10 and a = 7.5, what does the following code do? (x > a) .* (x-5).^2 Explain it qualitatively and then provide the solution.
Write assembly code for the following C procedure: i = 1; sum = 0; while (i...
Write assembly code for the following C procedure: i = 1; sum = 0; while (i ≤ n) { sum += i; ++i; } i and sum corresponds to $s1 and $s2. n is preloaded into $s3
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...
How do you translate this pseudocode to regular code in C++? for i :1 to length(A)...
How do you translate this pseudocode to regular code in C++? for i :1 to length(A) - 1 j = i while j > 0 and A[j - 1] > A[j] swap A[j] and A[j - 1] j = j -1
Please code the program showing the output below. using C language 1. Using 'if' or 'while'...
Please code the program showing the output below. using C language 1. Using 'if' or 'while' or 'for' and 'break' statement / only using <stdio.h> A bC dEf GhIj KlMnO 2. a program that identifies the largest number that can be expressed in short. Using only loop (ex.for,if,while) and break statement only using <stdio.h>
1. Give the O-runtime depending on n for the code snippet below (n > 0). for(...
1. Give the O-runtime depending on n for the code snippet below (n > 0). for( j = n; j <= 0; j = j - 1)                                     for( k = 1; k <= n; k = k + 1)                                                 print(" "); 2. Give the O-runtime depending on n for the code snippet below (n > 0).            for( j = 1; j <= n; j = j + 1)                                     for( k = 1; k <= n; k =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT