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

Lab 20.1: Basic Array Skills Let\'s work on some basic skills by creating a prog

ID: 3810664 • Letter: L

Question

Lab 20.1: Basic Array Skills Let's work on some basic skills by creating a program that contains a few experiments with arrays. If you are asked to print out a value, be sure to print a label that indicates what the value represents, unless otherwise indicated. For example, ifyou are asked to print a variable z, the output might look like this: z 8.0 Create a new NetBeans project named CPS150 Lab20 1. Add your name at the top after the eauthor tag, and then add Java statements that perform each of the following tasks a Create an array x of 20 random double values, each between 1.0 and 100.0 (.e., declare/create the array, then populate it with 20 random doubles). b) Output the number of items in the array by printing the expression x.length. c) Output the first array item, x[0]. d) Output the last array item. Be careful to choose the right index. e Print the expression x[x.length. 1]. Why is this value the same as in part (d)? Use a for loop to print all the values in the array without labels. g) Use a loop to print all the values in the array with labels to indicate what each element is h) Use a for loop to print all the values in the array in reverse order with labels to indicate what each element is.

Explanation / Answer

Code for Lab 20.1:-

The answer for Lab 20.1 (e) is :-Yes, the answer of lab 20.2 (d) and lab 20.2 (e) are same because both point to same index. As the last index = 19 and arr.length = 20 (i.e the length ) and length - 1 points the last index. Therefore, (arr.length)-1 points the last index also due to which both answers are same

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
   public static void main (String[] args) throws java.lang.Exception
   {
        double Min=1.0;
        double Max=100.0;
       double[] arr=new double[20];
       for(int i=0;i<20;i++){
            arr[i]=Min + (double)(Math.random() * ((Max - Min) + 1));
       }
       System.out.println("The no of items is :"+arr.length);
       System.out.println("The first array item is :"+arr[0]);
       System.out.println("The last array item is :"+arr[19]);
       System.out.println("The first array item is :"+arr[(arr.length)-1]);
       for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
       }
       for(int i=0;i<arr.length;i++){
            System.out.println("arr["+i+"]:    "+"element : "+arr[i]);
       }
       System.out.println();
       System.out.println("THE REVERSE ORDER :");
       for(int i=(arr.length)-1;i>=0;i--){
            System.out.println("arr["+i+"]:    "+"element : "+arr[i]);
       }
   }
}