1 2 Previous Next 19 Replies Latest reply: Mar 27, 2007 10:13 PM by 807599 RSS

    Help with arrays

    807599
      I would like some help with a question on arrays

      I am supposed to find the mean and the mode of a 2 D array

      I created a nested for loop the totaled the array = that to int total

      and then I set int average = total / array.length, and it is not correctly averaging the array,

      my professor said that array.length won't work for a 2D array only a single array. she suggested a counter but I am not sure on how to code this?

      Can someone help me please?

      I have no idea on how to find the mode.
        • 1. Re: Help with arrays
          807599
          Simple. Declare a variable before the for loop and initialise to zero. Inside the loop everytime you had a value to the total, you also increment your counter by one.
          • 2. Re: Help with arrays
            807599
            Thank you for the reply i wanted to make sure this is what you meant
            is this code correct ?

            int counter = 0;          
            for (int rowNumber[] : Studentgrades)
                 {
                 for (int columnNumber : rowNumber)
                 
                 total += columnNumber;
                 
                           counter++;
            int average = total / counter;
                                     average = Mean;
            }
            • 3. Re: Help with arrays
              807599
              it complies but i still get a zero for an anser that is why i asked

              thanks again
              • 4. Re: Help with arrays
                807599
                Basically.

                Is the declaration of average inside the loop? If so you only need to do it once, outside the loop once you have added all the values. Also, after assinging average a value, why are you immediately reassigning it to be Mean?
                • 5. Re: Help with arrays
                  807599
                  Hear is my whole code for the project:

                  public class assignmentone
                  {
                  public static void main (String args[])
                  {
                  int Studentgrades[][] = { {88, 99, 89, 88, 77},
                  {90, 89, 70, 78, 100},
                  {94, 74, 86, 89, 63},
                  {99, 76, 66, 89, 64},
                  {89, 76, 89, 88, 56},
                  {98, 76, 89, 88, 99},
                  {98, 89, 66, 88, 89} };
                                                
                            
                       int Maxgrade = Studentgrades[0][0];
                  int Mingrade = Studentgrades[0][0];
                  int Mean = 0;
                       int Mode = 0;
                            int total = 0;
                            int grade = 0;
                       int counter = 0;          
                            
                                 for (int Testgrade[]: Studentgrades)
                  {
                       for (int grades: Testgrade)
                                 {

                       if (grades < Mingrade)
                            Mingrade = grades;
                                 
                  if (grades > Maxgrade)
                            Maxgrade = grades;
                                      
                       
                       total += grades;
                                 counter++;
                  int average = total / counter;
                                           average = Mean;
                            
                  }
                            }
                                      
                       System.out.printf("The max grade is %s\n",Maxgrade);
                  System.out.printf("The min grade is %s\n",Mingrade);
                       System.out.printf("The mean is %s\n", Mean);
                            System.out.printf("The Mode is %s", Mode);
                       }          
                            
                  }

                  no reason on the changing it to mean, i could delete that save a variable lol
                  • 6. Re: Help with arrays
                    807599
                    it complies and the Max and Min come back correctly but the Mean still says zero
                    • 7. Re: Help with arrays
                      807599
                      Remember, the right hand side of an assignment statement the value is assigned to the left hand side!

                      Message was edited by:
                      flounder
                      • 8. Re: Help with arrays
                        807599
                        that worked, when i changed average = mean to mean = average, it works fine now thank you so much, it is the simple that get me all the time,
                        thanks again
                        • 9. Re: Help with arrays
                          807599
                          Two things, you are still doing the calculation inside the loop, so it does the calculation everytime around instead of ONCE outside the loop. Also, why bother with the average variable?
                          Mean = total / counter;
                          • 10. Re: Help with arrays
                            807599
                            Okay, thanks i did get rid of the average variable and just change it to Mean = total/ counter; but i'm not sure of what you mean doing the calculation inside the loop , how do i move it outside the loop
                            • 11. Re: Help with arrays
                              3004
                              Okay, thanks i did get rid of the average variable
                              and just change it to Mean = total/ counter; but i'm
                              not sure of what you mean doing the calculation
                              inside the loop , how do i move it outside the loop
                              for (...) {
                                // inside the loop
                              }
                              
                              // outside the loop
                              Why are you calculating the average after every item (inside the loop) and then doing nothing with it, rather than just calculating it once at the end (outside the loop)?
                              • 12. Re: Help with arrays
                                807599
                                oh okay i see what you mean i will change that thanks a bunch
                                • 13. Re: Help with arrays
                                  807599
                                  Any thoughts on how to do the mode?
                                  • 14. Re: Help with arrays
                                    3004
                                    Any thoughts on how to do the mode?
                                    What part are you having trouble with?

                                    Do you know what mode is and how to do it "manually"?

                                    If yes, then have you taken a stab at doing it in Java? What part is leaving you stuck? (I think I know, but it'll be better if you can explain it yourself.)
                                    1 2 Previous Next