Answer to Question #127861 in C++ for yuxuan l i

Question #127861
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.)

i←0
while i < n−2 do
if A[i]A[i+1]> A[i+ 2 ]then
returning i
i←i+ 1
return−1
1
Expert's answer
2020-07-30T10:25:43-0400
#include <iostream>

/** For simplicity, let's denote the length of the list by a constant value of 5.*/
const unsigned int n = 5;

/**  The function gets an array of five integers as its argument: 1, 2, 3, 4, 5. 
  *  Starting from the first list item, we multiply the current item by the next one and compare the obtained value with the third one. 
  *  As soon as the result of multiplication of the first two items is more than the third item, 
  *  we return the number of the first item used in multiplication. 
  *  If there are no sequences of two items in the list whose multiplication value is greater than the third item, we return -1. 
  *  The last two list items do not participate in the check because the third item is not in the list and there is nothing 
  *  to compare the multiplication value with.
  *  In our case we will get the following verifications:
  *  list item 0 : 1*2 > 3 is a false;
  *  list item 1 : 2*3 > 4 - true -> return 1.
  *  On the screen you will see 1.
  */
int questionAnswer(int A[n])
{
    int i = 0;
    while (i < n - 2)
    {
        if (A[i] * A[i + 1] > A[i + 2])
        {
            return i;
        }
        i++;
    }

    return -1;
}


int main()
{
    int A[n] = { 1,2,3,4,5 };
    std::cout << questionAnswer(A);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS