| | |
arrays
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
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
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
0
#3 Oct 23rd, 2009
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
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
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Oct 2009
Posts: 10
Reputation:
Solved Threads: 0
0
#4 Oct 23rd, 2009
i'm sorry couldn't get it clearly, i'm a new to programming and a slow learner. thanks for the reply
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 4
0
#5 Oct 23rd, 2009
Firstly, your for syntax is incorrect, for clarity, enclose your loop conditions in brackets:
Should look more like:
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:
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!
Java Syntax (Toggle Plain Text)
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(); }
Java Syntax (Toggle Plain Text)
for (int x = 0; x < count; x++) { grades[x] = input.nextShort(); }
Java Syntax (Toggle Plain Text)
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!
Last edited by eggmatters; Oct 23rd, 2009 at 12:10 pm.
1
#6 Oct 23rd, 2009
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
short [] grades = .... ; .. .. float avrg = average(grades);
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Arrays (C++)
- How to Return Multidimensional Arrays (C++)
- C file input/output 2D arrays. (C)
- passing arrays in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: limit the length of a Jtextfield???
- Next Thread: start java program from desktop or start menu
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






