-1

I need to find and return the average of all values in the array. Every other part of my code works and compiles. Could someone please help me with finding the average part? IT is working with my GUI I have with this class

import java.awt.*;
import java.util.Random;  //for our random number generator




public class StatsArray {

//instance variables
private int size;  //how big is the array
private int[] stats; // an array of integers

//default constructor -overloaded method
StatsArray() {
    size = 10;
    stats = new int[size] ;  //instantiate the array called stats
}

public void display(Graphics g)
{
    int x = 50;  //coordinates for displaying
    int y = 40;

    //display the array with position number
    for(int i = 0; i < stats.length; i++)
    {
        g.drawString("Stats [" + i + "] = "+ stats[i],
         x, (y + 15 * i));
    }
}

public void fillArray()
 {
    /*fill the array with random numbers (int) in the range 0 - 100.*/
    Random random = new Random();

for (int i = 0; i < stats.length; i++)
    stats[i] = random.nextInt(100+1);

}

public int getSum() 
{
    //add up all the values in the array
       int sum = 0;// Variable to keep track of sum
  for (int i = 0; i < stats.length; i++) //For loop to cycle through the aray at each element
  {
     sum += stats[i]; // Add each element onto the total of sum
  }

    return sum;// Returns sum, which is the added total of all the elements
 }

public int getMax() 
{
    //return the maximum value in the array
        int max = stats[0];

        for (int i = 0; i < stats.length; i++)
        {
            if (max < stats[i])
            {
                max = stats[i];
            }
        }
        return max;
}

public int getMin()
{
    //return the minimum value in the array

        int min = stats[0];

        for (int i = 0; i < stats.length; i++)
        {
            if (min > stats[i])
            {
                min = stats[i];
            }
        }
        return min;
}


**public double getAverage() 
{
    //return the average
        return ;
}**

public int countValues(int lowRange, int highRange) 
{
    //count how many numbers are >= lowRange and <= highRange
    int count=0;
    for(int i=0;i<stats.length;i++)
    {
         if(stats[i]>=lowRange && stats[i]<=highRange)
         {
               count++;
         }
    }
    return count;
}

public boolean isValueFound(int someNumber) 
{
    //check to see if someNumber is in the array

    return true;
}


public void sortArray() 
{
    /*sort the array in ascending order - selection sort*/

    int tempValue;
    int min;

    for (int i = 0; i < (stats.length - 1); i++)
    {
        min = i;
        for (int j = (i + 1); j < (stats.length); j++)
        {
            if (stats[j] < stats[min])
            {
                min = j;
            }
        }
        tempValue = stats[min];
        stats[min] = stats[i];
        stats[i] = tempValue;


    }

}

2 Answers2

1

Change getAverage to

 public double getAverage( )
 {
      return ( double )getSum( ) / ( double )stats.length;
 }

Edit

In response to your comment:

Once again just iterate your array. While iterating, check to see if you come across the desired value. If so, return true.

public boolean isValueFound( int someNumber )
{ 
    for( int i = 0; i < stats.length; i++ )
    {
        if( stats[ i ] == someNumber )
        {
            return true;
        }
    }

    return false;
}

Also see How can I test if an array contains a certain value?

Community
  • 1
  • 1
ssell
  • 6,429
  • 2
  • 34
  • 49
  • This works thanks a lot! and for the next part I have to do which I didn't notice is the: public boolean isValueFound(int someNumber) how would I do this part? – Trigonometreh Oct 30 '13 at 20:15
  • Thanks again ssell! My GUI is working perfectly now! – Trigonometreh Oct 30 '13 at 20:19
  • No problem. Don't forget to accept an answer so that the question is effectively closed. – ssell Oct 30 '13 at 20:23
  • 1
    This is somewhat nitpicky (and your answer is worth an upvote anyway) but I would use the size variable instead of stats.length in calculations. It doesn't matter in this case, with the current code, but using size instead of stats.length allows to keep the size of the array "in use" and the size allocated for the statistics in total separate. – kviiri Oct 30 '13 at 20:25
  • Glad you pointed that out. Ill edit the answer to reflect. Thanks! – ssell Oct 30 '13 at 20:26
1
return getSum() / size;

This works, but has a caveat. Since getSum() and size are integers, you're actually getting the floor of the actual average. If you want a floating point division (which I think you do, as the return type is double!), use the following:

return (double)getSum() / size;
kviiri
  • 3,282
  • 1
  • 21
  • 30