Question

5. (2 points) What are the values in array x after the following code executes (list all of the elements of the array)? CS111
0 0
Add a comment Improve this question Transcribed image text
Answer #1

X contains the following elements after the execution of the code:

x={2.0 , -1.0 , 3.0 , -2.0 , 4.0 , -3.0}

As the length of the array is 6

Loop will execute less then 6 that is i value will execute till 5: i<x.length: i<6

Explanation: Every element the array depends on this step x[i]=x[i-1]+x[i]

x[0]=2.0

For value of i=1

x[1] = x[1-1] + x[1] ;

x[1] = 2.0 + -3.0

x[1] = -1.0

********************************

For value of i=2

x[2] = x[2-1] + x[2] ;

x[2] = -1.0 + 4.0

x[2] = 3.0

*******************************

For value of i=3

x[3] = x[3-1] + x[3] ;

x[3] = 3.0+ -5.0

x[3] = -2.0

******************************

For value of i=4

x[4] = x[4-1] + x[4] ;

x[4] = -2.0 + 6.0

x[4] = 4.0

*****************************

For value of i=5

x[5] = x[5-1] + x[5] ;

x[5] = 4.0 + -7.0

x[5] = -3.0

******************************

C: \Users\suguna\Desktop>java loop 2.0 1.0 3.0 2.0 4.0 3.0

Add a comment
Know the answer?
Add Answer to:
5. (2 points) What are the values in array x after the following code executes (list...
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
  • What are the values stored in the array a after the following code executes? int[] a...

    What are the values stored in the array a after the following code executes? int[] a = new int[10]; a[0] = 1; for (int i = 1; i < a.length; i++) a[i] = 2 * a[i - 1] - 1;

  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • 1. (TCO 1) What is the output of the following C++ code? int list[5] = {0,...

    1. (TCO 1) What is the output of the following C++ code? int list[5] = {0, 5, 10, 15, 20}; int j; for (j = 0; j < 5; j++)      cout << list[j]; (Points : 4)         0 1 2 3 4         0 5 10 15         0 5 10 15 20         5 10 15 20 Question 2.2. (TCO 1) What is stored in alpha after the following code executes? int alpha[5] = {0}; int j; for (j = 0; j...

  • What will be the value of w after the following section of code executes: int w...

    What will be the value of w after the following section of code executes: int w = 4, 9 = 3; if (a < 5) if (w == 7) W = 3; else W = 3; else if (w > 3) W = 2; else W = 1; Select one 2.3 0 b.o 0.2 d. 1

  • After the following code executes, to what will the character string x be equal? #include <iostream>...

    After the following code executes, to what will the character string x be equal? #include <iostream> int main() { char x[20] = "Hello World"; char y[20]; __asm {   cld;   lea EDI, x;   mov ecx, 5;   mov al,60   rep stosb;    nop } }

  •   Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

      Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion:    Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) {     j=i;     temp=arr[i];                while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...

  • Use the following code, which initializes an array x with 1000 random integers, to answer the...

    Use the following code, which initializes an array x with 1000 random integers, to answer the questions a and b. java.util.Random generator = new java.util.Random( );//accessing Random class final int MAX = 1000; int[] x = new int[MAX]; for( int j = 0; j < MAX; j++ ) { x[j] = generator.nextInt( MAX ) + 1; } a. Write partial code to print all array elements of x in reverse order (one array element per line) b. Write partial code...

  • Question 15 After the following code executes, what is the output if user enters 0? int...

    Question 15 After the following code executes, what is the output if user enters 0? int x =-1; cout << "Enter O or 1:"; cin >> X; if (x) cout << true else cout << false O nothing will be displayed true O false 0

  • In the blank below, write the contents of array arr after the following code is executed...

    In the blank below, write the contents of array arr after the following code is executed up to the comment indicated in the main procedure below? #include <iostream> #include <algorithm> using namespace std; void do something(int list[], const int size); int main() { const int SIZE(10); int arr[SIZE] = { 5, 8, 1, 7, 3, 9, 4, 6, 10, 2 }; do somethingCarr, SIZE); ** Write the contents of array arr in the space below when execution reaches here. */...

  • What is wrong with the following code that prints out the elements of a 2x3 array?...

    What is wrong with the following code that prints out the elements of a 2x3 array? for (int i 0; i < 3; ++i) { for (int j = 0; j < 2: ++j) { printf("x(%d] [%d] ". %.2f\n", i, j, x[i][j]); O Expression ali]li] is incorrect O There is nothing wrong with the code 0 %2t is invalid O It is printing for a 3x2 array

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