hi to everybody. I'm just wonderin' if someone can help me with arrays. I am making a program of computing an average grade. I manage to execute the first requirements. The problem i have is how can i compute the grades that had been entered using a seperate method
here is the syntax of my command.

import java.util.Scanner;
public class gradesArray {

    /**
     * @param args the command line arguments
     */
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) {
        // TODO code application logic here

    System.out.print("\nPlease enter the student's full name - ");
    String studentName = input.nextLine();

    System.out.print("\nHow may grades will you be entering for " + studentName
            + "? - ");
   int count = input.nextInt();
   short[] grades = new short [count];/// array

   System.out.print("\nPlease enter the grades one at a time, pressing enter "
         +  "after each value.");
   for (int x = 0; x < count; x++)
   grades[x] = input.nextShort();

}

    }

public static string[]gettingAverage()
{
int temp = n1
        to
}
}

thanks

Recommended Answers

All 6 Replies

the second static method should have array of shorts to represent set of student grades. It returns as output an array of floats containing the min., max and the average grades

Have 3 methods that accept a:
short [] as argument and return the min, the max, and the average.

And next time use code tags. Press the button that says (CODE) when you create a new post

Have 3 methods that accept a:
short [] as argument and return the min, the max, and the average.

And next time use code tags. Press the button that says (code) when you create a new post

i'm sorry couldn't get it clearly, i'm a new to programming and a slow learner. thanks for the reply

Firstly, your for syntax is incorrect, for clarity, enclose your loop conditions in brackets:

int count = input.nextInt();
   short[] grades = new short [count];/// array
   
   System.out.print("\nPlease enter the grades one at a time, pressing enter "
         +  "after each value.");
   for (int x = 0; x < count; x++)
   grades[x] = input.nextShort();

}

Should look more like:

for (int x = 0; x < count; x++) {

   grades[x] = input.nextShort();
}

Ok so now you have a global variable in main called "grades" which is an array of positive whole integer values. You've done the hard part. So you have a method:

public static string[]gettingAverage()
{
int temp = n1
        to
}

So you need to figure out how you can use this method to calculate the averages. You will need to give your method access to your grades[] array. Also, why is it returning an array of strings (by defining it: public static string[])? Should it return a number that represents an average perhaps?

At any rate, re-write your function "gettingAverage()" to be able to accept an argument (Let me know if you need help with that) the argument should be - take a big guess - your grades array (and its size).

You've already demonstrated that you can loop through an array and perform an operation on it's elements, so implementing that to determine an average should be no problem for you. Good luck!

public static float average(short [] grades) {
  // calculate the average using a for loop
}

And to call it after you have your array created and entered values in it:

short [] grades = .... ;
..
..

float avrg = average(grades);
commented: Congrats on 200th solved.. :) (Well marked solved anyway) +4

thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.