Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default [SOLVED] Multidimensional array

    I am trying to make a multidimensional array that saves temperatures in an month by day and hour. What I need is a method that put all the temperatures together and outputs and mean temperature for the whole month.. What I got now is a method that adds all temps together, put how can I make it so that it divides this number on all the hours in the hour??

    Here is code:
    PHP Code:
    // Overall temp, all put together:
    
    		public double finnGjesnittTotal(){
    			double sum = 0;
    			for (int dag = 0;dag <temp.length; dag++){
    			for (int time = 0; time < temp[dag].length; time++){
    				sum += temp[dag][time];
    	 		}
    		}
    		return sum;
    	}
    This outputs all the temperature together.. But I need a method that divides this on all the temperatuers.

    I tried to divide this on this:
    sum = sum / (lenth[time].length * length[dag].length);

    Does not work..
    Dag = day
    Time = hour
    Any clues.

  2. #2
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    Quote Originally Posted by Torgero View Post
    This outputs all the temperature together.. But I need a method that divides this on all the temperatuers.
    What exactly are you asking for when you ask for a method that divides the sum on all temperatures? What do you need the sum to be divided by? Think about what you really want to do, mathematically, then see if you can figure out how to write the function.

  3. #3
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    I need it to be divided by hours in the whole array..

    sum = sum / temp[time].length; ??????

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    so you basically need to count the number of times you add a temp to the sum.... if only you could use a counter int variable that incremented each time a temp were added...

  5. #5
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    I understand why dividing by temp.length dont work, but cant figure out how to use a counter in the function..

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    simply counter++ would work.

  7. #7
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    But then I need a loop inside the function... Couldnt work..

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    No you don't. No extra loop needed. You just use the one you are currently using to add sums. Come on think on this. It's not that hard.

  9. #9
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    A counter in Java is just a variable that adds one to it's self on every pass of the loop...
    Java Code:
               [B][COLOR="Blue"]int counter = 0;[/COLOR][/B]
                for (int i= 0; i < someThing.length; i++)
                   {
                     System.out.println("Array value = " + someThing[i]);
                     [COLOR="blue"][B]counter++;[/B][/COLOR]
                    }
    counter++ actually means:
    Java Code:
    counter = counter +1;
    So now the counter variable contains the number of times the "for" loop looped.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  10. #10
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    Yes but outside the loop, time equals zero..

    I tried:

    sum += temp[dag][time] / time; (Divided by zero message..)

    I tried it all..

    And may be easy, but everything is easy if you know it..

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    1) declare counter as an int variable before the loop.
    2) increment counter via counter++; inside the loop
    3) do the division of sum / counter after the loop.

    What you need to do is pretend your mind is the computer and walk through each step of your code including looping imagining what the computer is doing at that moment.

    For instance if I have code like so:I
    Java Code:
    int[] myArray = {1, 2, 3, 4, 5};
    int sum = 0;
    int counter = 0;
    for (int i = 0; i < myArray.length; i++)
    {
      sum += myArray[i];
      counter++;
    }
    I walk through each step in the code, and then I loop in my mind (or on paper) the 5 times through the loop, thinking what is happening to sum, what is happening to counter, each iteration through the loop. If the loop is huge, then I just do a few iterations, whatever it takes to get a feel for the code. This is how you learn to know what your program is doing, and how to make it work better.

  12. #12
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default Does not work..

    // Gjennomsnittet totalt:

    public double finnGjesnittTotal(){
    double sum = 0;
    double counter;
    for (int dag = 0;dag <temp.length; dag++){
    for (int time = 0; time < temp[dag].length; time++; counter++){


    sum += temp[dag][time] / (double) counter;

    }
    }
    return sum;
    }

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your code looks as if you've not read any of the recent posts, and it appears that you are not following any of our recommendations. Why?
    Last edited by Fubarable; 03-22-2009 at 07:23 PM.

  14. #14
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    Guess I dont get it..

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're doing your division within the loop and we told you not to do this.

    I think that you're at the point where you should to talk to your instructor, get some remedial help asap.

  16. #16
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    I find it hard to understand that somebody can understand nested "for" loops, but can't grap the concept of a counter and averages.
    Strange...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  17. #17
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Red face Ok, tried to follow your example..

    PHP Code:
    // Gjennomsnittet totalt:
    
    		public double finnGjesnittTotal(){
    			double sum = 0;
    
    			for (int dag = 0; dag <temp.length; dag++){
    			int counter = 0;
    			for (int time = 0; time < temp[dag].length; time++){
    				sum += (temp[dag][time]);
    				counter++;
    
    				sum = sum / (double) counter;
    	 		}
    		}
    		return sum;
    	}
    Seems to be some sort of mistake somewhere.. Thanks for them answers anyway...

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm about to pull my hair out.

    How can I tell you so that you'll understand not to do the division inside of the for loop? What about this statement doesn't make sense. Again, please see your teacher asap. You are missing some basic concepts that we can't help you with.

  19. #19
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    • Take the counter variable initializaion out of the "for" loop...
    • Don't reuse the sum variable... call it something like "avg" because that is what it is.

    Java Code:
    public double finnGjesnittTotal(){ 
                double sum = 0; 
                [COLOR="Blue"][B]int counter = 0;[/B][/COLOR] 
                [COLOR="Blue"][B]double avg = 0.0;[/B][/COLOR] 
                for (int dag = 0; dag <temp.length; dag++){ 
                
                for (int time = 0; time < temp[dag].length; time++){ 
                    sum += (temp[dag][time]); 
                    counter++; 
                 } 
            } 
            [B][COLOR="Blue"]avg = sum / (double) counter;[/COLOR][/B] 
            return [B][COLOR="Blue"]avg[/COLOR][/B]; 
        }
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  20. #20
    Torgero is offline Member
    Join Date
    Oct 2008
    Posts
    33
    Rep Power
    0

    Default

    Ok, thanks for them answers, should have followed your instructions better..

    Was just being frustrated having used my whole weekend on this thing..

Page 1 of 2 12 LastLast

Similar Threads

  1. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 07:09 AM
  2. Multidimensional hashtable?
    By jklsemicolon in forum New To Java
    Replies: 6
    Last Post: 08-17-2008, 06:23 AM
  3. Replies: 9
    Last Post: 06-25-2008, 05:48 AM
  4. Array Reflection: Multi Array Reflection
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:08 PM
  5. Multidimensional arrays
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •