User storing scores of 10 students in an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • d24706
    New Member
    • Mar 2008
    • 3

    User storing scores of 10 students in an array

    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:
    [CODE=Java]
    //Arrayofscores.j ava
    //This programme asks the user to enter 5 exam scores and store them in an array
    import java.text.*;

    public class Arrayofscores
    {
    public static void main(String args[])
    {
    int [] scores = new int[10];
    int smallest, highest,temp,to tal=0;
    double average =0.0;

    //ask the user to enter 10 scores
    for (int i = 0;i<= scores.length-1;i++)
    {
    System.out.prin t("\n\nEnter Score " + (i+1) + ": ");
    scores[i] = UserInput.getIn t();
    }

    //find the lowest score
    smallest = scores[0];

    for (int i = 1; i <= scores.length-1;i++)
    if (scores[i] < smallest)
    smallest = scores[i];

    System.out.prin tln("\nThe lowest score is : " + smallest);

    //find the highest score
    highest = scores[0];

    for (int i = 1; i <= scores.length-1;i++)
    if (scores[i] > highest)
    highest = scores[i];

    System.out.prin tln("\nThe highest score is : " + highest);

    //find the average score
    for (int i = 0; i<=scores.lengt h-1;i++)
    total = total + scores[i];

    average = total/10.0;
    System.out.prin tln("\nThe average score is : " + average);

    } [/CODE]

    }


    Any suggestions would be greatly appreciated!!
    Last edited by BigDaddyLH; Mar 5 '08, 04:51 PM. Reason: added code tags
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    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

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      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?

      Comment

      • d24706
        New Member
        • Mar 2008
        • 3

        #4
        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;



        Code:
        public static float getGrade()
        			
        			{
        			float grade;
        			
        			if (score > 90) 
        			grade = "A"; 
        			else if (score > 75) 
        			grade = "B";
        			
        			System.out.println("\nThis is a " + getGrade(highest) + " grade.");
        			
        			return grade;

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          Originally posted by d24706
          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;



          Code:
          public static float getGrade()
          			
          			{
          			float grade;
          			
          			if (score > 90) 
          			grade = "A"; 
          			else if (score > 75) 
          			grade = "B";
          			
          			System.out.println("\nThis is a " + getGrade(highest) + " grade.");
          			
          			return grade;
          why are you making a recursive call? And why do you think getGrade(highes t) will work? Have you defined a getGrade method that takes an argument?

          Comment

          • d24706
            New Member
            • Mar 2008
            • 3

            #6
            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.

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              Originally posted by d24706
              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:
              [CODE=java]public static void main(String[] args)
              {
              int a;
              int b;

              int c = addTogether(a, b);
              }[/CODE]

              What would the method signature be for addTogether?

              Comment

              • nomad
                Recognized Expert Contributor
                • Mar 2007
                • 664

                #8
                Originally posted by d24706
                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;



                Code:
                public static float getGrade()
                			
                			{
                			float grade;
                			
                			if (score > 90) 
                			grade = "A"; 
                			else if (score > 75) 
                			grade = "B";
                			
                			System.out.println("\nThis is a " + getGrade(highest) + " grade.");
                			
                			return grade;


                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

                Comment

                • BigDaddyLH
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1216

                  #9
                  Originally posted by d24706
                  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;



                  Code:
                  public static float getGrade()
                  			
                  			{
                  			float grade;
                  			
                  			if (score > 90) 
                  			grade = "A"; 
                  			else if (score > 75) 
                  			grade = "B";
                  			
                  			System.out.println("\nThis is a " + getGrade(highest) + " grade.");
                  			
                  			return grade;
                  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.

                  Comment

                  Working...