Help with an arrays assignment


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help with an arrays assignment

Hybrid View

  1. #1
    Join Date
    Oct 2006
    Posts
    11

    Help with an arrays assignment

    The assignment is basically having to take 10 floating point numbers as inputs in an array and then finding the average. The program should then return the average followed by all of the numbers greater than the average.

    I already figured out how to find the average and displaying it but i cant figure out how to display the numbers greater than the average.

    This is what i have so far if it helps any

    import java.util.Scanner;
    public class ArraysAverage
    {
    public static void main(String[] args)
    {
    int[] list=new int[10];
    int num = 0, bignum=0;
    float sum = 0;

    Scanner reader = new Scanner(System.in);

    for(int i=0; i<list.length; i++)
    {
    System.out.print("Enter a number: ");
    list[i] = reader.nextInt();
    sum += list[i];

    }
    float avg = sum/list.length;
    System.out.println("The average is: " + avg );


    }
    public static double Total(int[] list){
    double Total = 0;
    for(int i=0; i<list.length; i++)
    {
    Total += list[i];

    }
    return Total;
    }
    public static double average(int[] list, double Total){
    double average = (Total/(list.length));
    return average;

    }
    }

  2. #2
    Join Date
    Dec 2005
    Posts
    97
    once you find your average, so can do something like this

    Code:
    for(int i = 0; i < list.length; i++)
    {
    if(list[i] > avg)
    System.out.println(list[i]);
    }

  3. #3
    Join Date
    Oct 2006
    Posts
    11
    So do i put that under
    float avg = sum/list.length;

    ?

  4. #4
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    float avg = sum/list.length;
    System.out.println("The average is: " + avg );
    It would be better if you insert it here.
    Further more to make it more fancy you can use this
    System.out.println(" i = " + i + list[i]);
    I would like to learn try both, one at a time and see the difference.
    Good luck

  5. #5
    Join Date
    Oct 2006
    Posts
    11
    Ya i got it now, thx for the help :tup:

Similar Threads

  1. entering data into Arrays via Scanner
    By aesoprock00 in forum Java
    Replies: 2
    Last Post: 02-12-2006, 12:17 AM
  2. Replies: 3
    Last Post: 10-21-2005, 11:06 AM
  3. Replies: 15
    Last Post: 05-09-2001, 05:40 AM
  4. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 12
    Last Post: 06-20-2000, 04:06 PM
  5. VB Arrays and memory allocation
    By Brian Leung in forum VB Classic
    Replies: 0
    Last Post: 06-20-2000, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center