Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part A. Write code that fills an array values with each set of numbers below. 1

ID: 3568811 • Letter: P

Question

Part A. Write code that fills an array values with each set of numbers below.

1 2 3 4 5 6 7 8 9 10

1 4 9 16 25 36 49 64 81 100

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 4 9 16 9 7 4 9 11

1 0 1 0 1 0 1 0 1

Part B. Consider the following array:

int [] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0};

What is the value of the total after the following loops complete?

a.

int total = 0;

for (int i =0; i < 10; i++)

{

            total = total + a[i];

           

}

cout << total << endl; // COSC 1415/1436

System.out.println(total); // COSC 1437

Output here:

b.

int total = 0;

for (int i =0; i < 10; i = i +2)

{

            total = total + a[i];

           

}

cout << total << endl; // COSC 1415/1436

System.out.println(total); // COSC 1437

Output here:

c.

int total = 0;

for (int i =2; i <= 10; i ++)

{

            total = total + a[i];

           

}

cout << total << endl; // COSC 1415/1436

System.out.println(total); // COSC 1437

Output here:

d.

int total = 0;

for (int i =1; i < 10; i = i +2*i)

{

            total = total + a[i];

           

}

cout << total << endl; // COSC 1415/1436

System.out.println(total); // COSC 1437

Output here:

e.

int total = 0;

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

{

            total = total + a[i];

}

cout << total << endl; // COSC 1415/1436

System.out.println(total); // COSC 1437

Output here:

Explanation / Answer

part A

int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int b[] = {1, 4, 9, 16, 25, 36, 49, 64, 81, 100};
int c[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int d[] = {1, 4, 9, 16, 9, 7, 4, 9, 11};
int e[] = {1, 0, 1, 0, 1, 0, 1, 0, 1};


part B

int [] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0};

a) 25

b) 13

c) ArrayIndexOutOfBoundsException
   reason : i <= 10 will be true when i = 10.
   so it throws exception

d) 6

e) 25