# Need help in multidimensional array

• January 13th, 2010, 11:18 PM
Stefan_Lam
Need help in multidimensional array
As title shown above,anyone know how to fix this?

I wanna get the out put like dis:
I m getting confusing about the coding as trying to do...hope someone can help me..^^

Output:
Example:
Employee number 1001
average number of his/her feedback is 2.83
The grade for this employee is C
Employee number 1002

and so on until 1005..

I think the program is easy ,I just get some confuse in some part...

Coding:
Code :

```public class latihan { public static void main (String []args) {   int employeenumber[]={1001,1002,1003,1004,1005}; float averageFeedback=0; float a[]=new float [5]; float [][][] result={ {{3.5f, 2.2f, 2.8f}}, {{2.6f, 3.0f, 3.7f}}, {{4.0f, 3.4f, 1.0f}}, {{1.0f, 1.9f, 3.9f}}, {{4.0f, 4.0f, 4.0f}} }; char arraygrade[]=new char[5];       for(int i=0;i<result.length;i++) { a[i]=averageFeedback/3;       for(int j=0;j<result[i].length;j++) {   for(int k=0;k<result[i][j].length;k++)   { averageFeedback+=(result[i][j][k]);   if(averageFeedback<2) { arraygrade[k]='D'; } else if(averageFeedback<3) { arraygrade[k]='C'; } else if(averageFeedback<4) { arraygrade[k]='B'; } else { arraygrade[k]='A';   }           } }     } for(int l=0;l<5;l++) {   System.out.println("Employee number "+employeenumber[l]); System.out.println("average number of his/her feedback is "+a[l]); System.out.println("The grade for this employee is "+arraygrade[l]);   }     }}```
• January 14th, 2010, 12:02 AM
psrkiran
Re: Need help in multidimensional array
Man don't get confusion man

Code :

```public class latihan { public static void main (String []args) { int employeenumber[]={1001,1002,1003,1004,1005}; float averageFeedback=0; float a[]=new float[5]; float [][] result = {{3.5f, 2.2f, 2.8f}, {2.6f, 3.0f, 3.7f}, {4.0f, 3.4f, 1.0f}, {1.0f, 1.9f, 3.9f}, {4.0f, 4.0f, 4.0f}}; char arraygrade[]=new char[5]; for(int i=0;i<result.length;i++) {   for(int j=0;j<result[i].length;j++) averageFeedback+=(result[i][j]);   a[i]=averageFeedback/3;   if(a[i]<2) { arraygrade[i]='D'; } else if(a[i]<3) { arraygrade[i]='C'; } else if(a[i]<4) { arraygrade[i]='B'; } else { arraygrade[i]='A'; }   }   for(int l=0;l<5;l++) { System.out.println("Employee number "+employeenumber[l]); System.out.println("average number of his/her feedback is "+a[l]); System.out.println("The grade for this employee is "+arraygrade[l]); } } }```
• January 14th, 2010, 01:22 AM
Stefan_Lam
Re: Need help in multidimensional array
Sorry but the average seems doesnt work...where to fix it?do u know pskrian..
• January 14th, 2010, 07:52 PM
helloworld922
Re: Need help in multidimensional array
You're trying to compute the average before you've calculated the sums. Also, for each employee you need to reset the averageFeedBack variable so their feedback is calculated independent of everyone else's (I would just declare averageFeedback inside of the main loop to ensure it's reset each time through). For some reason, you've also got the array of results declared a 3-d array rather than a 2d array. FYI, in general people use doubles instead of floats because computers now have tons of memory, and doubles give more precision (and for some historical reasons, doubles have become the standard floating-point precision data type in many programming languages)

Code :

```// 2d arrays look like this float [][] result={ {3.5f, 2.2f, 2.8f}, {2.6f, 3.0f, 3.7f}, {4.0f, 3.4f, 1.0f}, {1.0f, 1.9f, 3.9f}, {4.0f, 4.0f, 4.0f} };```

Code :

```// averaging code for (int i = 0; i < 5; i++) { float averageFeedback = 0; for (int j = 0; j < 3; j++) { averageFeedback += results[i][j]; } a[i] = averageFeedback / 3; }```

A better approach to this problem would be to use an object that represents an employee, and have methods inside of that object that will automatically calculate the average feedback for that employee. Then, you'd only need to create an array of employees and use the employee's methods to generate all the information you need, cutting back greatly on code you need to type, debug, and maintain.
Code :

```public class Employee { public int employeeNum; public double [] feedback;   public Employee(int employeeNumber, double[] feedback) { this.employeeNum = employeeNumber; this.feedback = feedback; }   public double averageFeedback() { double sum = 0; for (int i = 0; i < this.feedback.length; i++) { sum += feedback[i]; } return sum / feedback.length; }   public char feedbackGrade() { double ave = averageFeedback(); if (ave < 2) { return 'D'; } else if (ave < 3) { return 'C'; } else if (ave < 4) { return 'B'; } else { return 'A'; } } }```