Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Thanked 0 Times in 0 Posts

    Multidimensional Array and Seperate Methods

    This is a gradebook that is suposed receive so many grades and I'm having a hard time getting the high grade, low grade and average, I also need the Letter grade from the average. Can you please help:/

    import java.util.*;
    public class P_MidtermProject {

    public static void main(String[] args) {
    int outercounter=0;
    int howMany=0;
    Scanner input = new Scanner(System.in);

    System.out.print("How many students are there?: ");
    int numStudents = input.nextInt();
    String name [] = new String[numStudents];
    System.out.print("Enter the number of grades for the semester: ");
    howMany = input.nextInt();
    double [][] gradebook = new double [numStudents][howMany];

    for(outercounter = 0; outercounter<name.length; outercounter++ ){

    System.out.print("Enter the students name: ");
    String studentName = input.next();

    for(int innercounter = 0; innercounter<howMany; innercounter++){

    System.out.print("Enter grade: ");
    int grade = input.nextInt();

    }//end of inner loop

    }//end of outer loop
    System.out.println("Student Name\tHigh Score\tLow Score\tAverage\t\tGrade");
    for(int inc = 0; inc<name.length;inc++){
    System.out.print(name[inc] + "\t\t");

    }//end of main
    public static void high(double a[][]){

    for(int nn=0;nn<a.length;nn++){
    for(int counter=0; counter<a.length-1; counter++ ){
    for(int count=0; count<a[counter].length-1; count++ ){
    if (a[counter][count+1] < a[counter][count]){
    double tempG = a[counter][count];
    a[counter][count] = a[counter][count+1];
    a[counter][count+1] = tempG;

    }//end of if
    }//end of inner loop
    }//end of outer loop

    }//end of for

    }//end of high
    public static void low(double a[][]){
    for(int count=0;count<a.length;count++){

    }//end of for

    }//end of low

    public static void average(double a[][]){
    double average = 0;
    for (int inc=0; inc<a.length;inc++){

    for (int inctwo=0;inctwo<a[0].length;inctwo++){
    average = average + a[inc][inctwo];}//end of inner loop

    a[inc][0] = average/a[inc].length;

    }//end of for

    }//end of average

    }//end of class

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,668 Times in 2,637 Posts
    In the future, please wrap all code in [code][/code] or [php][/php] tags. It preserves the formatting.
    Since this is an assignment, we can't just give you code. We can give suggestions though.
    The first thing I'd suggest is actually changing high, low and average signatures:
    public static double high(double[] a);
    public static double low(double[] a);
    public static double average(double[] a);
    Arrays.sort will sort your array such that it will go from "smallest" to "highest" value. So with doubles, the lowest number will be the first, and the highest number is the last. You will need to check if an offset exists before accessing it, but that's a simple check on the length (assuming you haven't covered try/catch). Average is of course simply the sum of the array divided by the number of items within it. These changes will save you tonnes of code as they can be done in a little as one line of code (not looping required within the methods at all).

    With the above changes, you could simply use them directly in the output if desired:
    System.out.print(name[inc] + "\t\t");
    System.out.print(high(gradebook[inc]) + "\t\t");
    System.out.print(low(gradebook[inc]) + "\t\t");
    System.out.print(average(gradebook[inc]) + "\t\t\n");
    Note that the access level has changed from multidimensional to single dimension. This is because we do not want to factor in high, low, average, or grade numbers between multiple people; we only care about this specific iteration of student.

    As for grade assignment, if its overall then you simply accept an double and use if/elseif syntax to assign a resulting letter and return that.

    Nice work otherwise, it's looking good. Nice and clean. One thing to mention is that using a Scanner. Using nextInt/nextDouble methods (use nextDouble instead since you are calculating as double values), it does leave the linefeeds on the scanner. This isn't a problem here since you don't use the nextLine() function, but if you did you could pull an infinite loop if you use input for a looping control. Easily cleared by chomping the nextLine() or issuing a scanner clear.


Posting Permissions

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