Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Help with one-dimensional array

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with one-dimensional array

    Hey everyone, new to java here some troubles getting this down mostly step 4 i believe i have everything else correct not sure though.

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


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help with one-dimensional array

    Hello brandon66!
    Quote Originally Posted by brandon66 View Post
    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
    For task 2, it says it needs an enhanced for loop. Check the java tutorial and an example.
    For task 4, since you know how to loop through an array what is the problem you are facing?
    You need to start from the 9th element and then print every 9th element of your array instead of starting in the first element (index 0) and then do something with the next element until the last one, like you do in the other for loops.

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with one-dimensional array

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

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help with one-dimensional array

    Quote Originally Posted by brandon66 View Post
    so the enhanced for loop would look like this: ?
    Yes, this is the correct syntax for the enhanced for loop.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with one-dimensional array

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

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

  6. #6
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help with one-dimensional array

    Quote Originally Posted by brandon66 View Post
    for step 4 would i create a for loop like this:?

     for(int i = 9; i < testArray.length; i++)
        {
        System.out.println(testArray[i]);
        }
    That way your loop will print every element in the array with an index >=9. This is because in each iteration it goes to the next index (i++ means that i is incremented by one in each iteration). But you want it to print every 9th element.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    161
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Help with one-dimensional array

    Quote Originally Posted by brandon66 View Post
    for step 4 would i create a for loop like this:?

     for(int i = 9; i < testArray.length; i++)
        {
        System.out.println(testArray[i]);
        }
    To get it to print every 9th element after that, you could either check if i is divisible by 9, or increment i by 9 each time.
    Examples:
    for (int i = 9; i < testArray.length; i++) {
         if (i % 9 == 0)
              System.out.println(testArray[i]);
    }

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

  8. #8
    Junior Member
    Join Date
    Apr 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with one-dimensional array

    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 = 8; i < testArray.length; i = i + 9) {
         System.out.println("9th values: " + testArray[i]);
        }
      }
    }
    Last edited by brandon66; April 27th, 2012 at 09:40 AM. Reason: changed starting index from 9 to 8.

Similar Threads

  1. [SOLVED] 2 dimensional array storing help!
    By jts0541 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 31st, 2012, 03:31 PM
  2. Two-dimensional boolean array?
    By tcmei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 08:32 PM
  3. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  4. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM