Hey everyone, having some troubles getting this down mostly step 4 i believe i have everything else correct not sure though. Im not sure how i would go by doing step 4 i can have it print the 9th value but im lost after that..

  1. Create a one-dimensional array of 99 double values. Then, use a for loop to add a random number from 0 to 100 to each element in the array. For each value, use the random method of the Math class to get a double value between 0.0 and 1.0, and multiply it by 100.

  2. Use an enhanced for loop to sum the values in the array. Then, calculate the average value and print that value on the console followed by a blank line. Compile and test this class.

  3. Use the sort method of the Arrays class to sort the values in the array, and print themedian value (the 50th value) on the console followed by a blank line. Then, test this enhancement.

4 .Print the 9th value of the array on the console and every 9th value after that. Then,test this enhancement

import java.util.Arrays;

public class ArrayTestApp
{
    public static void main(String[] args)
    {
    double[] testArray = new double [99];

   //add a random number to each element in the array
    for (int i=0; i < testArray.length; i++)
    testArray[i] = 100.0*Math.random(); 

   //sum the values and print out the average
    double average = 0.0;
    for(int i = 0; i < testArray.length; i++)
    average += testArray[i];       
    average /= 99;   
    System.out.println("Average is: " + average);
    System.out.println();

   //sort the values and print the median
    Arrays.sort(testArray);
    System.out.println("Median is: " + testArray[testArray.length/2]);
    System.out.println();

  //print the 9th value and every 9th value after



    }
}

Recommended Answers

All 9 Replies

Write down on paper the indexes for the elements that you are to print. Then look at the difference between each of the indexes. Look at the definition of the for statement. What does the for statement's third expression/clause do?

So the enhanced for loop would look like this:?

import java.util.Arrays;

public class ArrayTestApp
{
    public static void main(String[] args)
    {
    double[] testArray = new double [99];

   //add a random number to each element in the array
    for (int i=0; i < testArray.length; i++)
    testArray[i] = 100.0*Math.random(); 



   //sum the values and print out the average - enhanced for loop
    double sum = 0.0;
    for(double a: testArray)
    {
        sum += a;
    }
      double average = sum/testArray.length; 
    System.out.println("Average is: " + average);
    System.out.println();   

   //sort the values and print the median
    Arrays.sort(testArray);
    System.out.println("Median is: " + testArray[testArray.length/2]);
    System.out.println();

  //print the 9th value and every 9th value after

for the for loop would i create a for loop like this:? (step 4)

 for(int i = 9; i < testArray.length; i++)
    {
    System.out.println(testArray[i]);
    }

9th value of the array on the console and every 9th

How does your for loop look at every 9th? It will print 10th, 11th, 12th etc Remember the first is at 0

thanks alot guys for the help :) here is the finished code

import java.util.Arrays;

public class ArrayTestApp
{
    public static void main(String[] args)
    {
    double[] testArray = new double [99];

   //add a random number to each element in the array
    for (int i=0; i < testArray.length; i++)
    testArray[i] = 100.0*Math.random(); 



   //sum the values and print out the average
    double sum = 0.0;
    for(double a: testArray)
    {
        sum += a;
    }
      double average = sum/testArray.length; 
    System.out.println("Average is: " + average);
    System.out.println();   

   //sort the values and print the median
    Arrays.sort(testArray);
    System.out.println("Median is: " + testArray[testArray.length/2]);
    System.out.println();

  //print the 9th value and every 9th value after
   for (int i = 9; i < testArray.length; i = i + 9) {
     System.out.println("9th values: " + testArray[i]);
   }
  }
}

A question: is the 9th value at index=9 or index=8?
Array indexes start at 0

i was wondering that, good catch thanks again

import java.util.Arrays;

public class ArrayTestApp
{
    public static void main(String[] args)
    {
    double[] testArray = new double [99];

   //add a random number to each element in the array
    for (int i=0; i < testArray.length; i++)
    testArray[i] = 100.0*Math.random(); 



   //sum the values and print out the average
    double sum = 0.0;
    for(double a: testArray)
    {
        sum += a;
    }
      double average = sum/testArray.length; 
    System.out.println("Average is: " + average);
    System.out.println();   

   //sort the values and print the median
    Arrays.sort(testArray);
    System.out.println("Median is: " + testArray[testArray.length/2]);
    System.out.println();

  //print the 9th value and every 9th value after
   for (int i = 8; i < testArray.length; i = i + 9) {
     System.out.println("9th values: " + testArray[i]);
  }
 }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.