Question

Consider the following array: int[] a = { 3, 5, 2, 2, 4, 7, 0, 8,...

Consider the following array: int[] a = { 3, 5, 2, 2, 4, 7, 0, 8, 9, 4 }; What are the contents of the array a after the following loops complete? (show how you arrive at the answer)

a) for (int i = 1; i < 10; i++) { a[i] = a[i - 1];

b) for (int i = 9; i > 0; i--) { a[i] = a[i - 1]; }

Homework Answers

Answer #1

Given, Asked to solve and explain logical code where an array is given with some pre-defined value and 2 questions with different iteration logic.

[Answer]

a) for (int i = 1; i < 10; i++)

{

a[i] = a[i - 1];

}

For this part as we can see in code that in for loop we are iterating the value from 1 to 9 which is the total index of array a. Inside loop we are exchanging the value of previous element with current one.

Also we can notice that we started loop with index 1 but array stores the value from index 0.

Therefore, a[i] = a[i-1] => a[1] = a[0] (a[i-1] => a[0] => 3) and hence the value of current index will be changed with value 3.

Same happened in next iteration when i=2

a[i] = a[i-1] => a[2] = a[2-1] => a[2] = a[1] (a[1] in previous iteration we got the value 3. So again the value 3 will go inside the current index of a)

Same exchange of value will continue will loop ends and all the array value will be changed to 3.

Output:

b) for (int i = 9; i > 0; i--)

{

a[i] = a[i - 1];

}

As we already saw how the data got exchange as we took the previous element in loop.

Here, in this, code the iteration starts from the max length of array that means here we are exchanging the value of current one with previous in descending order as index.

When i=9: a[i] = a[i-1] => a[9] = a[9-1] => a[9] = a[8] => a[9] = 9

Therefore, the value 4 which was there in a[9] will be changed to value in a[8] which is 9.

Again when i=8: a[i] = a[i-1] => a[8] = a[7] =>a[8] = 8

Hence, we can see the pattern that the array is shifting values by 1.

For the value when i=1: a[i] = a[i-1] => a[1] = a[0] => a[1] = undefined(no value in array)

So the array will omit one of the end value and print rest.

Output:

Please let me know in the comments if you need more help in this.

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
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]...
Given the following unordered array: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] W X D T P N R Q K M E If the array was being sorted using the SHELL sort and the halving method, and sorting into ASCENDING order as demonstrated in the course content, list the letters in the resulting array, in order AFTER the FIRST pass. [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c =...
int i,sum=0;   int array[8]={//You decide this};   int *a,*b,*c;   a = array;   b = &array[3];   c = &array[5];   c[-1] = 2;   array[1] = b[1]-1;   *(c+1) = (b[-1]=5) + 2;   sum += *(a+2) + *(b+2) + *(c+2);   printf("%d %d %d %d\n", sum, array[b-a], array[c-a], *a-*b); array[8]={ ?? }; what is array[8]?
Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for...
Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int k = 3; k < arr.length - 1; k++) arr[k] = arr[k + 1]; Which of the following represents the contents of arr as a result of executing the code segment? a. {1, 2, 3, 5, 6, 7, 8} b. {1, 2, 3, 4, 5, 6, 7} c. {1, 2, 3, 5, 6, 7} d. {1, 2, 3, 5, 6, 7, 7} e....
Given array 8, 5, 3, 7, 1, 6, 4, 2. List array to show the array...
Given array 8, 5, 3, 7, 1, 6, 4, 2. List array to show the array changes in memory. You may list out array after each partition call is terminated. book’s Quick Sort algorithm
Given this array: int sequence[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 1,...
Given this array: int sequence[10] = { 3, 4, 5, 6, 7, 8, 9, 10, 1, 2 }; Write a C++ program to ask the user to enter a number, if the number can be found in the array, your program should display "Found". Otherwise, your program should display "Not found". For example, if the user enters 7, your program should display "Found". If the user enters 11, your program should display "Not found".
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6...
exampleInput.txt 1 2 3 0 2 3 4 0 1 3 5 0 1 2 6 1 5 6 8 2 4 6 7 3 4 5 9 10 5 8 9 4 7 9 6 7 8 6 How can I detect when 'cin' starts reading from a new line. The amount of numbers in each row is unknown. I need them in type 'int' to use the data. Also, it will be executed like so, "./a.out < sampleInput.txt".
Consider the following: period 1, 2, 3, 4, 5, 6, 7, 8 demand 7, 8, 9,...
Consider the following: period 1, 2, 3, 4, 5, 6, 7, 8 demand 7, 8, 9, 10, 14, 16, 13, 16 a. using a trend projection, forecast the demand for period 9 b. calculate the MAD for this forecast Show all work! do not use excel or phstat!!!
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a...
Giving an array of integers A[1:8] ={5, 1, 3, 2, 7, 6, 8, 4}. Build a MAX-HEAP on A. Show the steps using binary tree representation or array view. Use heap sort: show the array after the first, the second, and the third of heap sort
Which of the following is a valid array initialization? Group of answer choices int array[2] =...
Which of the following is a valid array initialization? Group of answer choices int array[2] = [3, 4]; int array[10]; None of the others int array[] = {1, 39}; int array(2) = {5, 10};
A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14...
A = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20] In MATLAB. Use a fully vectorized code (ie. no loops) to determine when the numbers have increased to at least 15 in the above array. Report answer to command window.