# I need calculation

• January 27th, 2011, 11:25 AM
Swiss518
I need calculation
Code Java:

```/* Chapter 5: Programming Assignment 7 Programmer: Date: Program Name: AveragingGradesTemplate */   import java.io.*;   public class AveragingGradesTemplate { public static void main(String[] args) throws IOException { //Declaring variables BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in)); String grade, strSize; float average; int size;   System.out.print("How many grades will you enter? "); strSize = dataIn.readLine(); size = Integer.parseInt(strSize); float grades[] = new float[size];   for (int i=1; i<size; i++) {   //Get input from user System.out.print("Enter grade " + i + ": "); grade = dataIn.readLine(); grades[i] = Float.parseFloat(grade);   }   average = averageGrades(grades, size);       //Write a for loop to display grades         System.out.println(""); System.out.println("The average grade was "+average); }   public static float averageGrades(float[] g, int s) { float total = 0; float a; // write a for loop to calculate the total ?????????????????????????????????? ?? ????????????????????????????????? ?????????????????????????????????   a = total / s; return a; }   }```

I need to write a for loop for my calculations and I am lost.
• January 27th, 2011, 11:32 AM
KevinWorkman
Re: I need calculation
How would you do this by hand, without a computer?

Pretend you're given a stack of index cards that each show a number (that's your array). How would you go about calculating their total? Their average?

Pretend you have a friend who has no idea how to do that. Write out specific, simple instructions about what to do. When you have that done, you'll have an algorithm that should be pretty easy to translate to code.

PS- When posting code, make sure you use the code tags. Nobody likes to look at unformatted code.
• January 27th, 2011, 11:56 AM
Swiss518
Re: I need calculation
and so on
• January 27th, 2011, 12:03 PM
KevinWorkman
Re: I need calculation
Quote:

Originally Posted by Swiss518
and so on

Okay. That's not really what I asked, but if you're just looking for a tutorial on for loops (which I found as the first hit after googling "java for loops"), here y'ar: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
• January 27th, 2011, 12:08 PM
Swiss518
Re: I need calculation
It doesnt make sense. Can you give me an example to so what i need to do. this is what i got!!!

for (int i=1; i<size; i++)
• January 27th, 2011, 12:16 PM
KevinWorkman
Re: I need calculation
What doesn't make sense?

That's a fine start. Maybe try printing out the value of i each time, to give you an idea of what's going on?
• January 27th, 2011, 12:19 PM
Swiss518
Re: I need calculation
[i] =80
[i]= 70

or less I nees to take a variabe and set it to something.
• January 27th, 2011, 12:26 PM
KevinWorkman
Re: I need calculation
Quote:

Originally Posted by Swiss518
[i] =80
[i]= 70

or less I nees to take a variabe and set it to something.

I'm not sure what you're trying to say. Focus on one thing at a time. First, write a for loop that simply prints out a series of numbers (like 1-10, or 0-9).

Then look at the array tutorial (again, which a quick google search found immediately) and figure out how to access specific indices: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

When you have both of those working separately, then worry about combining them.