SMoore
SMoore

Reputation: 23

Computing the average of an array

This is a code to take user imputed grades, find their average, the deviation, and display this information in a table style. My program takes user input and displays the information well, but it won't compute the average and the deviation properly. When run, it says the average is 0. I'm doing it the same why my teacher taught us, but I can't find my error.

import java.util.Scanner;

public class ClassScores{
public static void main(String[] args){
String[] names = {"Bashful","Doc","Dopey","Grumpy","Happy","Sleepy","Sneezy"};
double[] grades = new double[7];
double mean=0;
double[] difference = new double[7];

getScores(grades);
average(grades, mean);
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);
} 



public static double[] getScores(double[] grades)
{
Scanner kb= new Scanner(System.in);
System.out.println("Enter grades for students in alphabetical order.");
for (int i=0;i<grades.length; i++)
{
    grades[i]=kb.nextDouble();
}
return grades;  
}

public static double average(double[] grades, double mean)
{
double total = 0;
for (double i : grades)
{
    total += i;
}
if (grades.length>0)
{
    mean = total/grades.length;
}
return mean;
}

public static double[] deviation(double[] grades, double mean, double[] difference)
{
for (int i=0; i<grades.length; i++)
{
    difference[i]=grades[i]-mean;
}
return difference;
}

public static void displayResults(String[] names, double[] grades, double[] difference, double mean)
{
System.out.println("The average score is" +mean);
System.out.println("Student Name   Grade    Mean Deviation");

for (int i=0; i<names.length; i++)
{
    System.out.printf(names[i]);
    System.out.printf("%20f", grades[i]);
    System.out.printf("%20f", difference[i]);
    System.out.println();
}

}   

}

Here is the edited code for anyone who's curious.

import java.util.Scanner;

public class ClassScores{
public static void main(String[] args){
String[] names = {"Bashful","Doc","Dopey","Grumpy","Happy","Sleepy","Sneezy"};
double[] grades = new double[7];
double mean=0;
double[] difference = new double[7];

getScores(grades);
mean = average(grades);
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);
}



public static double[] getScores(double[] grades)
{
Scanner kb= new Scanner(System.in);
System.out.println("Enter grades for students in alphabetical order.");
for (int i=0;i<grades.length; i++)
{
    grades[i]=kb.nextDouble();
}
return grades;  
}

public static double average(double[] grades)
{
double total = 0;
for (double i : grades)
{
    total += i;
}
return total/(grades.length);
}

public static double[] deviation(double[] grades, double mean, double[] difference)
{
for (int i=0; i<grades.length; i++)
{
    difference[i]=grades[i]-mean;
}
return difference;
}

public static void displayResults(String[] names, double[] grades, double[] difference, double mean)
{
System.out.println("The average score is" +mean);
System.out.println("Student Name   Grade    Mean Deviation");

for (int i=0; i<names.length; i++)
{
    System.out.printf(names[i]);
    System.out.printf("%20f", grades[i]);
    System.out.printf("%20f", difference[i]);
    System.out.println();
}

}   

}

Upvotes: 0

Views: 2790

Answers (5)

OldProgrammer
OldProgrammer

Reputation: 12159

You have this method declared to return a value:

public static double average(double[] grades, double mean)

but in your main method, you do not use the result returned. You are then printing out the value of the other "mean" variable which is global to that function, and is assigned a value of 0.

double mean=0;
double[] difference = new double[7];

getScores(grades);
**average(grades, mean);**
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);

I think you want :

getScores(grades);
**mean = average(grades, mean);**
deviation(grades,mean,difference);
displayResults(names, grades, difference, mean);

Upvotes: 0

Code-Apprentice
Code-Apprentice

Reputation: 83517

average(grades, mean); sends copies of the variables to the average() method. The mean variable inside of average() has nothing to do with the mean variable inside of main() despite the same name. To fix the problem, you need to do something like

mean = average(grades, mean);

I also suggest that you remove the mean parameter and declare a local variable inside of average(). Then you can just do

mean = average(grades);

Upvotes: 0

wchargin
wchargin

Reputation: 16027

First, your average method should look more like this:

public static double average(double[] grades)
{
    // Error check up front.
    if (grades.length == 0) { 
        throw new InvalidArgumentException("length is 0");
    }

    // These next lines are good.
    double total = 0;
    for (double i : grades)
    {
        total += i;
    }

    // Then you can just divide and return.
    return total / (grades.length);
}

There's no reason to pass a mean parameter to the average method. That'd be like passing a Dog to a createDog method.

But more importantly, while you call average, you don't store the result anywhere. You're just ignoring it.

If you're coming from a background with pointers, remember that Java passes primitives by value - changing mean, a double, inside the method will have no effect on the value outside the method. Instead, have

double mean = average(grades);

So the underlying reason why mean is zero is because you set it to zero, and never change it:

double mean = 0;

Upvotes: 1

rgettman
rgettman

Reputation: 178253

You are returning your mean value, but you aren't assigning the result of the method call to average to anything. What you want is to assign the result of the method to the variable:

mean = average(grades);

And in your average method, you don't need to take mean as a parameter, just declare it locally and return it.

Upvotes: 0

jonvuri
jonvuri

Reputation: 5920

You never actually use the return result of the average method. You probably meant to pass that to displayResults, or assign it to mean in the main() method.

Upvotes: 2

Related Questions