Question

1) Please convert the for‐loop in the following codes to a while loop. Both versions (for‐...

1) Please convert the for‐loop in the following codes to a while loop. Both versions (for‐ loop and while‐loop) should produce the same result.

x=0;

y=randi(100,1,5);

for i = 1:5

x=x+y(i);

end

disp(x);

Homework Answers

Answer #1

Explanation of the given code:

x=0;

y=randi(100,1,5); //here upper limit=100 ,lower limit =1 and it generate any 5 random numbers between 1 and 100

for i = 1:5 // we are taking range of i from 1 to 5

x=x+y(i); // in this line we are adding the five random numbers.

end

disp(x); // we displaying the result of sum of five random numbers

NOTE: in this code we used randi function so result is not same when we run the code multiple time the function give different random numbers.

Here the given code (for loop) is taking randomly 5 numbers from the given range i.e., (100,1) and printing the sum of those 5 numbers

Here

upper limit= 100

lower limit=1

no of random numbers to be added=5

randi() function is used to generate random numbers in given range

So every time we execute the code we get different result

given for loop code with output

while loop code:

x=0;

i=0;

y=randi(100,1,5);

while i<5

i=i+1;

x=x+y(i);

end

disp(x);

output for while loop code:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
c++ Convert the following for loop to a while loop: for (int x = 50; x...
c++ Convert the following for loop to a while loop: for (int x = 50; x > 0; x-=2) {      cout << x << " seconds to go.\n"; }
Can you rewrite this MATLAB code using a while loop instead of a for loop? %formatting...
Can you rewrite this MATLAB code using a while loop instead of a for loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0...
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0 if(x<1): print('Error') exit() print('Initial value is: ',x,' ',end='') while(x!=1): if(x%2==0): x=x/2 else: x=3*x+1 count+=1 if(x!=1): rint('Next value is: ',int(x),' ',end='') else: print('Final value ',int(x),', ',end='') print('number of operations performed ',int(count),' ',end='') break
1. The conditon of a "while" loop and a "for" loop are executed at the ____________...
1. The conditon of a "while" loop and a "for" loop are executed at the ____________ of these loops. 2. What will the following code fragment print?    for (i = 0; i < 4; i++)        cout << i + 1; cout << i; 3. What is the result in answer below? int int1=16; int int2=5; int answer=0; answer = int1 / int2 + (5 * 2);
Problem 2 (explain each codes please) If more information if needed please let me know 1)...
Problem 2 (explain each codes please) If more information if needed please let me know 1) % time vector t=linspace(0,1,1000); x = zeros(size(t)); y = zeros(size(t)); Meaning: 3)% loop to find the value of x and y for different values of time for i=1:length(t) x(i) = v*cos(angle_theta)*t(i); y(i) = h+v*sin(angle_theta)*t(i)-(0.5*g*(t(i).^2)); end Meaning: 4)% find the first index where y<=0 i.e the ball hits the ground I = find(y<=0,1); fprintf('The ball hits the ground at distance of %f meters\n',x(I)); Meaning: 5)...
how can i convert the while loop into a for loop? string input;     cin >> input;...
how can i convert the while loop into a for loop? string input;     cin >> input;     cout << endl;     int count = 0;     int size = input.size(); if(input.size() == 6) {         while(size > 0) {             if(count < 3) {                 if((input.at(count) >= 'A' && input.at(count) <= 'Z') || (input.at(count) >= 'a' && input.at(count) <= 'z')) {                     count++;                 }                 else {                     cout << "error: letter expected at position " << count + 1;                     return 0;                 }             }             else if(input.at(count) >= '0' &&...
Given the following while loop, rewrite it as a for loop. int x = 0; while(x...
Given the following while loop, rewrite it as a for loop. int x = 0; while(x < 10) {      printf("%d\n", x);      x++; }
1: A) Given the following vectorized code: >>x=[1:10]; >>f=x.^2+2; Rewrite this code using a for loop...
1: A) Given the following vectorized code: >>x=[1:10]; >>f=x.^2+2; Rewrite this code using a for loop that defines each element of f one at a time. Make sure to preallocate memory to f before filling each spot. B) See the following code. Rewrite the code in one line using the find function and a For loop. then write it again using a while loop x=[-1 3 7 2 4 0]; v=[]; for i=1:length(x) if x(i)<=2 v=[v, x(i)]; end end please...
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 =...
1. Convert the following pseudo code routine into MIPS assembler language: - Set register $t0 = 1 - In four separate instructions add 2, then 3, then 4, then 5 into register $t0 $t0 = $t0 + 2 + 3 + 4 + 5 - Copy the result from register $t0 into register $t1 2. Convert the following into MIPS assembler language: - Set register $t0 = 0 - Initialize the register $t1 = 10 - Use register $t2 as...
JAVA - take each for loop and rewrite as a while statement a) int result =...
JAVA - take each for loop and rewrite as a while statement a) int result = 0; for (int i = 1; i <= 10; i++) { result = result + i;} System.out.println(result); b) int result = 1; for (int i = 1; i <= 10; i++) {result = i - result;} System.out.println(result); c) int result = 1; for (int i = 5; i > 0; i--) {result = result * i;} System.out.println(result); d) int result = 1; for (int...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT