Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need help in multidimensional array

1. ## 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:
```public class latihan {
public static void main (String []args)
{

int employeenumber[]={1001,1002,1003,1004,1005};
float averageFeedback=0;
float a[]=new float ;
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}}
};

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)
{
}
else if(averageFeedback<3)
{
}
else if(averageFeedback<4)
{
}
else
{

}

}
}

}
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]);

}

}}```  Reply With Quote

3. ## Re: Need help in multidimensional array

Man don't get confusion man

```public class latihan {
public static void main (String []args)
{
int employeenumber[]={1001,1002,1003,1004,1005};
float averageFeedback=0;
float a[]=new float;
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}};
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)
{
}
else if(a[i]<3)
{
}
else if(a[i]<4)
{
}
else
{
}

}

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]);
}
}
}```  Reply With Quote

4. ## Re: Need help in multidimensional array

Sorry but the average seems doesnt work...where to fix it?do u know pskrian..  Reply With Quote

5. ## 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)

```// 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}
};```

```// 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.
```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;
}

{
double ave = averageFeedback();
if (ave < 2)
{
return 'D';
}
else if (ave < 3)
{
return 'C';
}
else if (ave < 4)
{
return 'B';
}
else
{
return 'A';
}
}
}```  Reply With Quote

6. ## The Following User Says Thank You to helloworld922 For This Useful Post:

Stefan_Lam (January 14th, 2010)