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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help in multidimensional array

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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 [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]);
     
    }
     
     
    }}
    Last edited by Stefan_Lam; January 13th, 2010 at 11:22 PM.


  2. #2
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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[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]);
    }
    }
    }
    Last edited by psrkiran; January 14th, 2010 at 12:18 AM.

  3. #3
    Junior Member
    Join Date
    Nov 2009
    Posts
    15
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help in multidimensional array

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

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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;
         }
     
         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';
              }
         }
    }

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

    Stefan_Lam (January 14th, 2010)

Similar Threads

  1. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  2. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 05:41 PM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM