469,293 Members | 1,364 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

User storing scores of 10 students in an array

3
Hello,

I hope someone can help me. I had to do the following assignment( i have most of it done just cant finish it off, question and source code below).

Question?
Write a Java program that asks the user to input the scores, as a percentage( e.g 87.4), of 10 students. The scores entered must be stored ina n array.

The programme must determine:

The lowest score and its equivalent grade (ie A,B,etc)
The highest " " " " "
The average score and its "

The bit i cant do is tie in the equivalent grade with the lowest highest and average score.

Code:
Expand|Select|Wrap|Line Numbers
  1. //Arrayofscores.java
  2. //This programme asks the user to enter 5 exam scores and store them in an array
  3. import java.text.*;
  4.  
  5. public class Arrayofscores
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         int [] scores = new int[10];
  10.         int smallest, highest,temp,total=0;
  11.         double average =0.0;
  12.  
  13.         //ask the user to enter 10 scores
  14.         for (int i = 0;i<= scores.length-1;i++)
  15.         {
  16.         System.out.print("\n\nEnter Score " + (i+1) + ": ");
  17.             scores[i] = UserInput.getInt();
  18.         }
  19.  
  20.             //find the lowest score
  21.             smallest = scores[0];
  22.  
  23.             for (int i = 1; i <= scores.length-1;i++)
  24.             if (scores[i] < smallest)
  25.                 smallest = scores[i];
  26.  
  27.     System.out.println("\nThe lowest score is : " + smallest);
  28.  
  29.             //find the highest score
  30.             highest = scores[0];
  31.  
  32.             for (int i = 1; i <= scores.length-1;i++)
  33.             if (scores[i] > highest)
  34.                 highest = scores[i];
  35.  
  36.         System.out.println("\nThe highest score is : " + highest);
  37.  
  38.                 //find the average score
  39.             for (int i = 0; i<=scores.length-1;i++)
  40.             total = total + scores[i];
  41.  
  42.             average = total/10.0;
  43.         System.out.println("\nThe average score is : " + average);    
  44.  
  45.             }    
}


Any suggestions would be greatly appreciated!!
Mar 5 '08 #1
8 13293
BigDaddyLH
1,216 Expert 1GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Mar 5 '08 #2
RedSon
5,000 Expert 4TB
First I suggest you sort your array after your user enters the 10 elements. Then element [0] is your smallest and element [9] is your largest, then to find the average you add up each element 0 through 9 and divide by the number of elements 10. After that when you output all you need to do is determine which letter grade corresponds to what value.

This should be fairly easy to work out, no?
Mar 5 '08 #3
d24706
3
Im still struggling to figure out the last piece, what i have come up with since and i think im getting on the right track is;



Expand|Select|Wrap|Line Numbers
  1. public static float getGrade()
  2.  
  3.             {
  4.             float grade;
  5.  
  6.             if (score > 90) 
  7.             grade = "A"; 
  8.             else if (score > 75) 
  9.             grade = "B";
  10.  
  11.             System.out.println("\nThis is a " + getGrade(highest) + " grade.");
  12.  
  13.             return grade;
  14.  
Mar 5 '08 #4
RedSon
5,000 Expert 4TB
Im still struggling to figure out the last piece, what i have come up with since and i think im getting on the right track is;



Expand|Select|Wrap|Line Numbers
  1. public static float getGrade()
  2.  
  3.             {
  4.             float grade;
  5.  
  6.             if (score > 90) 
  7.             grade = "A"; 
  8.             else if (score > 75) 
  9.             grade = "B";
  10.  
  11.             System.out.println("\nThis is a " + getGrade(highest) + " grade.");
  12.  
  13.             return grade;
  14.  
why are you making a recursive call? And why do you think getGrade(highest) will work? Have you defined a getGrade method that takes an argument?
Mar 5 '08 #5
d24706
3
sorry,im lost,looked over my notes and i just cant figure out methods,can someone tell me what the code should look like to get this to work.
Mar 5 '08 #6
RedSon
5,000 Expert 4TB
sorry,im lost,looked over my notes and i just cant figure out methods,can someone tell me what the code should look like to get this to work.
If you have:
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args)
  2. {
  3.    int a;
  4.    int b;
  5.  
  6.    int c = addTogether(a, b);
  7. }
What would the method signature be for addTogether?
Mar 6 '08 #7
nomad
664 Expert 512MB
Im still struggling to figure out the last piece, what i have come up with since and i think im getting on the right track is;



Expand|Select|Wrap|Line Numbers
  1. public static float getGrade()
  2.  
  3.             {
  4.             float grade;
  5.  
  6.             if (score > 90) 
  7.             grade = "A"; 
  8.             else if (score > 75) 
  9.             grade = "B";
  10.  
  11.             System.out.println("\nThis is a " + getGrade(highest) + " grade.");
  12.  
  13.             return grade;
  14.  


One could use two arrays which must be of the same length do get a letter grade. So if someone enter 90 the result would be an A.
Think about it.

nomad
Mar 6 '08 #8
BigDaddyLH
1,216 Expert 1GB
Im still struggling to figure out the last piece, what i have come up with since and i think im getting on the right track is;



Expand|Select|Wrap|Line Numbers
  1. public static float getGrade()
  2.  
  3.             {
  4.             float grade;
  5.  
  6.             if (score > 90) 
  7.             grade = "A"; 
  8.             else if (score > 75) 
  9.             grade = "B";
  10.  
  11.             System.out.println("\nThis is a " + getGrade(highest) + " grade.");
  12.  
  13.             return grade;
  14.  
Another thing (another!) wrong with this code is the type. What is the type for a grade? A float? Then why are you trying to return strings like "A" or "B? Sit down away from the keyboard and decide what the type for a grade should be.
Mar 6 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

22 posts views Thread by guitarromantic | last post: by
22 posts views Thread by Sandman | last post: by
namcintosh
3 posts views Thread by namcintosh | last post: by
13 posts views Thread by Eric IsWhoIAm | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.