Results 1 to 5 of 5
  1. #1
    speaknspell is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Array help counting # of grades

    It's a lot like other programs already posted, but not quite--I promise I searched! The program calcs the sum, the average, the highest and lowest score. I have no problems with that. BUT it supposed to also tell me how many scores were 90 or above, 80 to 90, 70 to 80. That's the problem I'm having. I know the code is ugly, but I don't really know what to do with it. Any help is really appreciated:)

    Java Code:
    public class Chp8Assign1
    {
    	public static void main (String [] args)
    	{
    		int myArray[] =  new int[args.length];
    		//for loop to convert String args array to integer myArray
    		for(int a=0; a<args.length;a++)
    		{
    			myArray[a]=Integer.parseInt(args[a]);
    		}
    		
    
    		int sum = 0;
    		int largest = myArray[0];
    		int smallest = myArray[0];
    
    
    		int gradeA = 0;   //variable for grades 90-100
    		int gradeB = 0;  //variable for grades 80-90
    		int gradeC = 0;  //variable for grades 70-80
    		int gradeD = 0;  //variable for grades 60-70
    		int gradeF = 0;  //variable for grades below 60
    		int aGrade=0;  //to count number of gradeA
    		int bGrade=0;  //to count number of gradeB
    		int cGrade=0;  //to count number of gradeC
    		int dGrade=0;  //to count number of gradeD
    		int fGrade=0;  //to count number of gradeF
    
    		for(int i=0;i<myArray.length;i++)  //loops through the array to sum, find largest number, then smallest, then check for number of A's, B's, etc.
    		{
    			sum=sum+myArray[i];
    			if(myArray[i]>largest)
    				largest = myArray[i];
    			if(myArray[i]<smallest)
    				smallest = myArray[i];
    				
    			if((myArray[i]>=90)&&(myArray[i]<=100)) //checks for grading
    				gradeA=aGrade;	
    			if((myArray[i]>=80)&&(myArray[i]<90))
    				gradeB=bGrade;	
    			
    			if((myArray[i]>=70)&&(myArray[i]<80))
    				gradeC=cGrade;	
    
    			if((myArray[i]==60)&&(myArray[i]<70))
    				gradeD=dGrade;	
    
    			if(myArray[i]<60)
    				gradeF=fGrade;	
    			
    		}//end of for
    		System.out.println("The sum is " + sum);
    		System.out.println("The average is " + sum/myArray.length);
    		System.out.println("The largest number is " + largest);
    		System.out.println("The smallest number is " + smallest);
    		System.out.println("Thenumber of students with scores of 90-100 (A) is " + aGrade++);
    		System.out.println("The number of students with scores of 80-90 (B) is " + bGrade++);
    		System.out.println("The number of students with scores of 70-80 (C) is " + cGrade++);
    		System.out.println("The number of students with scores of 60-70 (D) is " + dGrade++);
    		System.out.println("The number of students with scores below 60 (F) is " + fGrade++);
    	}//end of main
    }//end of program
    Last edited by speaknspell; 04-16-2009 at 11:00 PM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    can you please comment your code to say what each portion is supposed to do? i just see a whole bunch of setting variables to be 0.

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    you need a counter to keep track of the number of students. What you're doing now is just setting the last student to get that grade as gradeA, gradeB etc. try
    Java Code:
    if((myArray[i] >= 60) && (myArray[i] <70)){
         gradeD++;
    }
    and print out gradeD instead of gradeD++
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  4. #4
    speaknspell is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Sorry 'bout that. I edited the original message to reflect the comments.

  5. #5
    speaknspell is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Thanks -- it worked!! I'm so appreciative. I'm new to this forum and other forums keep track of successful help per person. Is there one of those in this one so I can add a "Yes, very helpful" to yours?

Similar Threads

  1. Recursive Counting
    By zlwilly in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 09:42 PM
  2. Replies: 3
    Last Post: 12-14-2008, 02:52 PM
  3. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 08:33 PM
  4. Need help with counting letters
    By mrdestroy in forum New To Java
    Replies: 15
    Last Post: 10-22-2008, 02:33 PM
  5. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 06:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •