Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Consider the following array: int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What is the value of

1. Consider the following array: int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 }; What is the value of total after the following loops complete? a) int total = 0; for (int i = 0; i < 10; i++) { total = total + a[i]; } b) int total = 0; for (int i = 0; i < 10; i = i + 2) { total = total + a[i]; } c) int total = 0; for (int i = 1; i < 10; i = i + 2) { total = total + a[i]; } d) int total = 0; for (int i = 2; i <= 10; i++) { total = total + a[i]; } e) int total = 0; for (int i = 1; i < 10; i = 2 * i) { total = total + a[i]; } f) int total = 0; for (int i = 9; i >= 0; i--) { total = total + a[i]; } g) int total = 0; for (int i = 9; i >= 0; i = i - 2) { total = total + a[i]; } h) int total = 0; for (int i = 0; i < 10; i++) { total = a[i] - total; }

2. What is wrong with each of the following code segments? a)

int[] values = new int[10]; for (int i = 1; i <= 10; i++) { values[i] = i * i; }

b)

int[] values; for (int i = 0; i < values.length; i++) { values[i] = i * i; }

3. Write code that fills an array values with each set of numbers below. a) 1 2 3 4 5 6 7 8 9 10 b) 0 2 4 6 8 10 12 14 16 18 20 c) 1 4 9 16 25 36 49 64 81 100 d) 0 0 0 0 0 0 0 0 0 0 e) 1 4 9 16 9 7 4 9 11 f) 0 1 0 1 0 1 0 1 0 1 g) 0 1 2 3 4 0 1 2 3 4

4.Rewrite the following loops without using the enhanced for loop construct. Here, values is an array of floating-point numbers. a) for (double x : values) { total = total + x; } b) for (double x : values) { if (x == target) { return true; } } c) int i = 0; for (double x : values) { values[i] = 2 * x; i++; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introductory Relational Database Design For Business With Microsoft Access

Authors: Jonathan Eckstein, Bonnie R. Schultz

1st Edition

1119329418, 978-1119329411

More Books

Students also viewed these Databases questions

Question

Effective Delivery Effective

Answered: 1 week ago