Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 23rd, 2006, 2:47 PM   #1
programj
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 programj is on a distinguished road
Need help setting up this array

I'm trying to display an average of grades.
I don't know what I'm doing wrong but I'm trying to set up:

- a BufferReader

- float grades [] = new float [variable];

- use 3 'for loops'
one loop to put in grades while asking user to enter grades, a loop to get data inside array and compute average, and a loop to output grades

here is what I have so far:
import javax.swing.JOptionPane;

// Computes the average of a series of scores
public class averaginggrades
{
  // the array that holds the scores
  private static int[] grades;

  public static void main(String[] args)
  {
     // Prompt user to enter number of grades
     String input = JOptionPane.showInputDialog("Enter number of grades:");
     int numberOfgrades = Integer.parseInt(input);

     // initialize array
     grades = new int[numberOfgrades];

     // Prompt user to enter grades and store them in an array
     for (int i = 0; i < grades.length; i++)
     {
        input = JOptionPane.showInputDialog("Enter grade #" + (i + 1) + ": ");
        grades[i] = Integer.parseInt(input);
        System.out.println("grades #" + (i + 1) + " = " + grades[i]);
     }
// Compute sum of scores
     int sum = 0;

     for (int i = 0; i < grades.length; i++)
     {
        sum += grades[i];
     }
     int largest = grades[0];

     for (int i = 0; i < grades.length; i++)
     {
        if (grades[i] > largest)
        {
           largest = grades[i];
        }
     }
     int smallest = grades[0];

     for (int i = 1; i < grades.length; i++)
     {
        if(grades[i] < smallest)
        {
           smallest = grades[i];
        }
     }

     // Display average grades
     System.out.println("grade average = " + sum / grades.length);

     // output the array
     for(int x = 0; x < grades.length; x++)
     {
        System.out.print(grades[x] + " ");
     }
     System.out.println();
  }
}
programj is offline   Reply With Quote
Old Apr 23rd, 2006, 4:55 PM   #2
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
[PHP]
//used to get average of grades
public int Average()
{
int sumOfGrades = 0;
for (int i = 0; i < grades.length; i++)
{
sumOfGrades += grades[i];
}
//you can change the varible type, I am just using int for an example
int average = (sum / grades.length);
return average;
}
//used to output the grades
public void showGrades()
{
for (int i = 0; i < grades.length; i++)
System.out.println("Grade #" + i + 1 + " = " + grades[i]);
}
[/PHP]
and for the loop to put in grades while asking user to enter grades, it looks like it's in your program already. I hope this helps.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Apr 23rd, 2006, 7:22 PM   #3
programj
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 programj is on a distinguished road
where in my code do I add what you put?
and am I suppose to delete something to add what you have?
programj is offline   Reply With Quote
Old Apr 23rd, 2006, 7:31 PM   #4
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
[PHP]
import javax.swing.*;
// Computes the average of a series of scores
public class averaginggrades
{
// the array that holds the scores
private static int[] grades;

public static void main(String[] args)
{
// Prompt user to enter number of grades
String input = JOptionPane.showInputDialog("Enter number of grades:");
int numberOfgrades = Integer.parseInt(input);

// initialize array
grades = new int[numberOfgrades];

// Prompt user to enter grades and store them in an array
for (int i = 0; i < grades.length; i++)
{
input = JOptionPane.showInputDialog("Enter grade #" + (i + 1) + ": ");
grades[i] = Integer.parseInt(input);
System.out.println("grades #" + (i + 1) + " = " + grades[i]);
}
// Compute sum of scores
int sum = 0;

for (int i = 0; i < grades.length; i++)
{
sum += grades[i];
}
int largest = grades[0];

for (int i = 0; i < grades.length; i++)
{
if (grades[i] > largest)
largest = grades[i];
}
int smallest = grades[0];
for (int i = 1; i < grades.length; i++)
{
if(grades[i] < smallest)
smallest = grades[i];
}
// Display average grades
System.out.println("grade average = " + Average());
// outputs the grades
showGrades()
}
//used to get average of grades
public static int Average()
{
int sumOfGrades = 0;
for (int i = 0; i < grades.length; i++)
sumOfGrades += grades[i];
//you can change the varible type, I am just using int for an example
int average = (sum / grades.length);
return average;
}
//used to output the grades
public static void showGrades()
{
for (int i = 0; i < grades.length; i++)
System.out.println("Grade #" + i + 1 + " = " + grades[i]);
}
}
[/PHP]
This should be it. The whole code.
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Apr 23rd, 2006, 7:52 PM   #5
programj
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 programj is on a distinguished road
here's the error I got when I tried to compile it:

C:\Documents and Settings\Desktop\averaginggrades.java:48: ';' expected
}
^
1 error

Tool completed with exit code 1
programj is offline   Reply With Quote
Old Apr 23rd, 2006, 8:15 PM   #6
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 4 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
add a ";" to showGrades(), lol
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Apr 23rd, 2006, 8:45 PM   #7
programj
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 programj is on a distinguished road
lol ok I did that and now this comes up:

C:\Documents and Settings\Desktop\averaginggrades.java:56: cannot find symbol
symbol : variable sum
location: class averaginggrades
int average = (sum / grades.length);
^
1 error

Tool completed with exit code 1
programj is offline   Reply With Quote
Old Apr 23rd, 2006, 8:47 PM   #8
programj
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 programj is on a distinguished road
the up arrow is actually pointing under the "s" in sum
programj is offline   Reply With Quote
Old Apr 23rd, 2006, 8:51 PM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
According to the error message, it can't find the sum variable. So figure out what's going wrong. In that function, the variable doesn't exist. However, there is a sumOfGrades variable, and I'd bet that's what you're looking for.

You gotta try and do stuff yourself. Problem solving is the fun part of coding.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 23rd, 2006, 9:06 PM   #10
programj
Newbie
 
Join Date: Apr 2006
Posts: 7
Rep Power: 0 programj is on a distinguished road
Quote:
Originally Posted by Ooble
According to the error message, it can't find the sum variable. So figure out what's going wrong. In that function, the variable doesn't exist. However, there is a sumOfGrades variable, and I'd bet that's what you're looking for.

You gotta try and do stuff yourself. Problem solving is the fun part of coding.
fun went out the window a few hours ago,
I've been trying to figure this out all day that's why I came here for help
programj is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:42 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC