arrays

Thread Solved

Join Date: Oct 2009
Posts: 10
Reputation: hello214 is an unknown quantity at this point 
Solved Threads: 0
hello214 hello214 is offline Offline
Newbie Poster

arrays

 
0
  #1
Oct 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: hello214 is an unknown quantity at this point 
Solved Threads: 0
hello214 hello214 is offline Offline
Newbie Poster

passing arrays to methods

 
0
  #2
Oct 23rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: hello214 is an unknown quantity at this point 
Solved Threads: 0
hello214 hello214 is offline Offline
Newbie Poster
 
0
  #4
Oct 23rd, 2009
Originally Posted by javaAddict View 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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: eggmatters is an unknown quantity at this point 
Solved Threads: 4
eggmatters eggmatters is offline Offline
Junior Poster in Training
 
0
  #5
Oct 23rd, 2009
Firstly, your for syntax is incorrect, for clarity, enclose your loop conditions in brackets:
  1. int count = input.nextInt();
  2. short[] grades = new short [count];/// array
  3.  
  4. System.out.print("\nPlease enter the grades one at a time, pressing enter "
  5. + "after each value.");
  6. for (int x = 0; x < count; x++)
  7. grades[x] = input.nextShort();
  8.  
  9. }
Should look more like:
  1. for (int x = 0; x < count; x++) {
  2.  
  3. grades[x] = input.nextShort();
  4. }
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:

  1.  
  2. public static string[]gettingAverage()
  3. {
  4. int temp = n1
  5. to
  6. }

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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso
 
1
  #6
Oct 23rd, 2009
  1. public static float average(short [] grades) {
  2. // calculate the average using a for loop
  3. }

And to call it after you have your array created and entered values in it:
  1. short [] grades = .... ;
  2. ..
  3. ..
  4.  
  5. float avrg = average(grades);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 10
Reputation: hello214 is an unknown quantity at this point 
Solved Threads: 0
hello214 hello214 is offline Offline
Newbie Poster
 
0
  #7
33 Days Ago
thanks for the help
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC