Consider the following array: int[] a = { 2, 4, 6, 8, 10, 12}; Write the content
ID: 3567726 • Letter: C
Question
Consider the following array:
int[] a = { 2, 4, 6, 8, 10, 12};
Write the contents of the array a after the following loops, and explain the reason in your words. Use the original data above for each question. (1 Point each)
a) for (int i = 1; i < 6; i++) { a[i] = a[i - 1]; }
b) for (int i = 5; i > 0; i--) { a[i] = a[i - 1]; }
c) for (int i = 0; i < 5; i++) { a[i] = a[i + 1]; }
d) for (int i = 4; i >= 0; i-=2) { a[i] = a[i + 1]; }
e) for (int i = 1; i < 6; i++) { a[i] = a[5 - i]; }
answer example ...
Explanation / Answer
// for (int i = 1; i < 6; i++) { a[i] = a[i - 1]; }
//Array a content are{ 2, 2, 2, 2, 2, 2}
// for (int i = 5; i > 0; i--) { a[i] = a[i - 1]; }
//Array a content are{ 2, 2, 4, 6, 8, 10}
//for (int i = 0; i < 5; i++) { a[i] = a[i + 1]; }
//Array a content are{ 4,6,8,10,12,12}
//for (int i = 4; i >= 0; i-=2) { a[i] = a[i + 1]; }
//Array a content are{ 4, 4, 8,8,12,12}
for (int i = 1; i < 6; i++) { a[i] = a[5 - i]; }
//Array a content are{ 2, 10, 8, 8, 10, 2}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.