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

Write enhanced for loops for the following tasks. a.?Printing all elements of an

ID: 3556135 • Letter: W

Question

Write enhanced for loops for the following tasks. a.?Printing all elements of an array in a single row, separated by spaces.

b.?Computing the maximum of all elements in an array.

c.?Counting how many elements in an array are negative.

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++; }

Rewrite the following loops, using the enhanced for loop construct. Here, values is an array of floating-point numbers.

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

b.?for (int i = 1; i < values.length; i++) { total = total + values[i]; }

c.?for (int i = 0; i < values.length; i++) { if (values[i] == target) { return i; } }

Explanation / Answer

Write enhanced for loops for the following tasks.

a. Printing all elements of an array in a single row, separated by spaces.

for(double x: values)
System.out.print(x+" ");

b. Computing the maximum of all elements in an array.

double max = values[0];
for(double x : values)
if(x > max) max = x;
System.out.println("Maximum element is " + max);

c. Counting how many elements in an array are negative.

int count = 0;
for(double x : values)
if(x<0) count++;
System.out.println("Negative numbers are " + count);

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; }

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

b. for (double x : values) { if (x == target) { return true; } }

for(int i=0; i<values.length; i++)
{
   if(values[i] == target)
   {
       return true;
   }
}

c. int i = 0; for (double x : values) { values[i] = 2 * x; i++; }

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

Rewrite the following loops, using the enhanced for loop construct. Here, values is an array of floating-point numbers.

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

for (double x : values)
{
total = total + x;
}

b. for (int i = 1; i < values.length; i++) { total = total + values[i]; }

for (double x : values)
{
total = total + x;
}
total = total - values[0];

c. for (int i = 0; i < values.length; i++) { if (values[i] == target) { return i; } }

int i=0;
for (double x : values)
{
   if (x == target)
   {
       return i;
   }
   i++;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote