Question

8 0/1 point How could the following code be re-written with a while loop? % find the index of the first value in A divisible
Tprint(The index of the tirst number divisible by 3T5 % end , index: O i=1; while i < length(A) if mod(A(i),3) == 0 index =
if i <= length(A) fprintf(The index of the first number divisible by 3 is %d\ni): end O i=1; while mod(A(i),3) -= 0 && i <=
0 0
Add a comment Improve this question Transcribed image text
Answer #1
Question 8:
Values of i in the for loop are from length(A) down to 1
This code finds the first index value that is divisible by 3.

Answer:

while mod(A(1),3) == 0 && i <= length(A) i=i+1; end ifi <= length(A) fprintf(The index of the first number divisible by 3 is

Add a comment
Know the answer?
Add Answer to:
8 0/1 point How could the following code be re-written with a while loop? % find...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Translate this matlab code to python title('Starting Number vs Number of Elements in Seq') ylabel('Number of...

    Translate this matlab code to python title('Starting Number vs Number of Elements in Seq') ylabel('Number of Elements in the sequence in the sequence') xlabel('Starting Number') steps = []; vec = 1:25; for i=1:length(vec) s = []; num = i; while true s(length(s)+1) = num; if num == 1 break; elseif mod(num, 2) == 0 num = num/2; else num = (3*num)+1; end end fprintf('%d = ', i) disp(s) fprintf('\n') steps(i) = length(s); end plot(vec, steps)

  • using C++ 1. Convert the following while loop into a for loop: int count = 0;...

    using C++ 1. Convert the following while loop into a for loop: int count = 0; while (count < 10) x = count + sin(x); y = count + cos(y); count++;

  • Convert the following while loop into a for loop. int 1 - 50: int sum-07 while...

    Convert the following while loop into a for loop. int 1 - 50: int sum-07 while (sum < 1000) sum - sum + 1; Question 35 (2 points) Saved Given an int variable k that has already been declared, use a while loop to print a single line consisting of 80 dollar signs. Use no variables other than k. int sum = 0; for(int i = 100;sum < 10000;1-- sum = sum + i;

  • Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do...

    Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1 1. Describe what it does and compute what value is returned when the input is the list {1, 2, 3, 4, 5}. (Hint: We're using 0-based array indexing, so 0 would represent the index of the first element, 1 the second element, etc.) 2. Identify and describe the worst-case input. 3. Identify and...

  • How many Iterations will this while loop perform? int ico), j(10); cout << "i = "...

    How many Iterations will this while loop perform? int ico), j(10); cout << "i = " << i << endl; cout << "j = " << j << endl; while (i > j) { cout << "j-" « j << endl; j += 2; cout << "i = " << i << endl; } cout << "i = << i << endl; cout << "j = " << j << endl; 5 6 C 8 10 Infinite times Does the...

  • (1 point) Find the length of the curver r(t) = i +3t'j + tºk, 0<t</96 L

    (1 point) Find the length of the curver r(t) = i +3t'j + tºk, 0<t</96 L

  • LOVOU AWN 1. def remove_all_from_string(word, letter): i=0 3. while i<len(word)-1: if word[i] = letter: num =...

    LOVOU AWN 1. def remove_all_from_string(word, letter): i=0 3. while i<len(word)-1: if word[i] = letter: num = word.find(letter) word - word[:num] + word[num+1:] i=0 i=i+1 return word 10 print remove_all_from_string("hello", "1") 5 points Status: Not Submitted Write a function called remove_all_from_string that takes two strings, and returns a copy of the first string with all instances of the second string removed. This time, the second string may be any length, including 0. Test your function on the strings "bananas" and "na"....

  • 3. Analyze the time complexity of the following program segm i = 1; s = 0;...

    3. Analyze the time complexity of the following program segm i = 1; s = 0; while (i<n) { S += i; i *= 2:

  • QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++)...

    QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++) { //outer int index = i; for (int j = i + 1; j < arr.length; i++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; }//for i In the array= 16 13 15 14 19 24 9 3, the index of the smallest number at the outer iteration 4 = Answer

  • Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code be...

    Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code below? %%Matlab code for Question 5. syms X y intlx exp(x),0,2); %integration of x*exp(x) fprintf("htlntegration of x"2*exp(x) for [O 3] is %2.5f.\n,y) %Integration using Trapizoidal rule nn [100 1000]; for ii 1:2 a:0; b-3; %Integration limit ns-1:nn(i) integral Values-zeros(size(ns)); ourFunction-@(x) x.*2.*exp(x); for n-ns val(n)-trapizoidal (ourFunction,a,b,nn(i); end fprintf nlntegration of x 2*exp(x) using Trapizoidal rule for [O 3]\n') fprintf('1tTrapizoidal integration value for n-%d...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT