Part# 1: Writing Exercise: (5 pts) Consider the following array: int[] a = { 2,
ID: 3567670 • Letter: P
Question
Part# 1: Writing Exercise: (5 pts)
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]; }
Note: The answers to the 5 questions (a through e) above should be typed in the block of comments in the Numbers.java file such as;
/*
a) {2, 4, 6, 8, 10, 12}: The array does not change anything because
Explanation / Answer
int[] a = { 2, 4, 6, 8, 10, 12};
a) it will copy value of a[i-1] in a[i]
So a[0] = 2;
a[1] = 2
a[2] = 4
a[3] = 6
a[4] = 8
a[5] = 10
b)It will copy the value of the a[i -1 ] in a[1]
a[5] = 10
a[4] = 8
a[3] = 6
a[2] = 4
a[1] = 2
a[0] = 2
c)I will copy value of a[i+1 ] to a[i]
a[0] = 4;
a[1] = 6
a[2] = 8
a[3] = 10
a[4] = 12
a[5] = 12
d)I t is same as number c but it will do it for alternate places causes i is incremented as i-2
a[0] = 4;
a[1] = 4
a[2] = 8
a[3] = 8
a[4] = 12
a[5] = 12
e)
a[0] = 2;
a[1] = 10
a[2] = 8
a[3] = 6
a[4] = 4
a[5] = 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.