0

I need to find the deviation of multiple homework values. In order to do this, i need to store a variable homework, that is in a for loop, so it deletes itself every time it executes. Also, the number of variables i need in order to store the seperate homework score values is unknown, so i can't just make a list of variables. I think what i need to do is do a list of arrays, but im not sure if im doing it right.

here is the code of the method that includes the array i have created...

 public double computeHomeworkDeviation(int homework){
    int[] homeworkScore = new int[totalStudents];
    if(computeHomeworkDeviationCounter < totalStudents){
        homeworkScore[computeHomeworkDeviationCounter] = homework;
        computeHomeworkDeviationCounter++;
    }
    else{
        for(int k = 1; k <= totalStudents; k++){
            top += Math.pow(homeworkScore[totalStudents - k] -    homeworkAverage, 2);
        }
    homeworkDeviation = Math.sqrt(top / totalStudents); 
    }
    return homeworkDeviation;
}

this method is called to a for loop and calls the argument of the homework variable that needs to be stored in an array list. What i have isn't working, what am i doing wrong? (i don't believe the array values are even being stored)

edit: I now split it into 2 methods.

setArrayMethod...

 public void setHomeworkArray(int homework){
    homeworkScore[l] = homework;
    l++;
}

computeHomeworkDeviationMethod...

 public double computeHomeworkDeviation(int homework){
        for(int k = 1; k <= totalStudents; k++){
            top += Math.pow(homeworkScore[totalStudents - k] -          homeworkAverage, 2);
        }
    homeworkDeviation = Math.sqrt(top / totalStudents); 
    return homeworkDeviation;
    }

am getting ArrayOutOfBoundsException error still.

6
  • 1
    What i have isn't working, Not working in which sense. error? wrong output? what Commented Nov 1, 2013 at 23:25
  • I am getting the wrong value for homeworkDeviation and i believe it has something to do with storing the homework values in the array. Commented Nov 1, 2013 at 23:26
  • move int[] homeworkScore = new int[totalStudents]; outside the function Commented Nov 1, 2013 at 23:27
  • where is computeHomeworkDeviationCounter declared? Commented Nov 1, 2013 at 23:27
  • There is only one array assignment in your code. Your for loop does not assign anything Commented Nov 1, 2013 at 23:29

1 Answer 1

1

Your hunch is correct, the values aren't storing properly. I'm not sure what exactly you want your program to do but I think you'd want a loop instead of if when storing values in the array:

while(computeHomeworkDeviationCounter < totalStudents){
    homeworkScore[computeHomeworkDeviationCounter] = homework;
    computeHomeworkDeviationCounter++;
}

This also means you'll have to remove the else statement. Finally, to avoid errors if this method is executed several times, I recommend resetting the variables computeHomeworkDeviationCounter when the method is called:

public double computeHomeworkDeviation(int homework){
int[] homeworkScore = new int[totalStudents];
computeHomeworkDeviationCounter = 0;

...or even having it as a method-local variable if it isn't used anywhere else:

public double computeHomeworkDeviation(int homework){
int[] homeworkScore = new int[totalStudents];
int computeHomeworkDeviationCounter = 0;

And likewise for the variable top if appropriate for the rest of your program.

Sign up to request clarification or add additional context in comments.

1 Comment

i edited my submission...also, the values do not need to be reset in the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.