# Multidimensional Array and Seperate Methods

• 10-09-2012, 07:06 PM
b721991
Multidimensional Array and Seperate Methods

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();
name[outercounter]=studentName;

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

}//end of inner loop

}//end of outer loop
System.out.println("");
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++){
Arrays.sort(a[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++){
Arrays.sort(a[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;
average=0;

}//end of for

}//end of average

}//end of class
• 10-09-2012, 09:25 PM
Fou-Lu
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:
Code:

```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:
Code:

```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.