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 2 of 2

Thread: Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)

    Hey guys,

    I've spent all day working on this and I can't seem to figure the rest out.

    Using this array:

    int[][] temperatures = {
    { 73, 71, 68, 69, 69, 77, 78 },
    { 76, 73, 72, 72, 75, 79, 76 },
    { 79, 82, 84, 84, 81, 78, 78 },
    { 75, 72, 68, 69, 65, 63, 63 }};

    I need to write a program that provides the following output:

    a) The average temperature for each week.
    b) The average temperature for each day.
    c) The high and low temperature for each week.
    d) The high and low temperature for each day.
    e) The average temperature of all the temperatures.


    PROBLEM:
    So far everything works except for C and D. Both are not giving me the correct "max" and "min" for per week or per day.

    The output I'm getting is this:

    Week 1 average temperature is 72.
    Week 2 average temperature is 74.
    Week 3 average temperature is 80.
    Week 4 average temperature is 67.

    Day 1 average temperature is 75.
    Day 2 average temperature is 74.
    Day 3 average temperature is 73.
    Day 4 average temperature is 73.
    Day 5 average temperature is 72.
    Day 6 average temperature is 74.
    Day 7 average temperature is 73.

    Week 1 highest temperature is 78.
    Week 1 lowest temperature is 73.
    Week 2 highest temperature is 79.
    Week 2 lowest temperature is 76.
    Week 3 highest temperature is 84.
    Week 3 lowest temperature is 81.
    Week 4 highest temperature is 84.
    Week 4 lowest temperature is 81.

    Day 1 highest temperature is 79.
    Day 1 lowest temperature is 73.
    Day 2 highest temperature is 82.
    Day 2 lowest temperature is 71.
    Day 3 highest temperature is 84.
    Day 3 lowest temperature is 68.
    Day 4 highest temperature is 84.
    Day 4 lowest temperature is 68.
    Day 5 highest temperature is 84.
    Day 5 lowest temperature is 65.
    Day 6 highest temperature is 84.
    Day 6 lowest temperature is 63.
    Day 7 highest temperature is 84.
    Day 7 lowest temperature is 63.

    Total sum of the array is 2069.

    I believe it's because I have a value that appears more than once in a row and/or column. I've updated my code to include "greater than or equal to" and "less than or equal to" but it's still not working.



    public class ArrayExample 
    {
    	public static void main(String[] args)
    	{
    		int[][] temperatures = {
    									{ 73, 71, 68, 69, 69, 77, 78 },
    									{ 76, 73, 72, 72, 75, 79, 76 },
    									{ 79, 82, 84, 84, 81, 78, 78 },
    									{ 75, 72, 68, 69, 65, 63, 63 }};
    		//This give me the weekly average.
    		int row,column,total;
    		int week = 1;
    		for(row = 0;row < temperatures.length;row++)
    			{  
    				total = 0;
    				int average = 0;
    				for(column = 0;column < temperatures[row].length;column++)
    					{
    						total = total + temperatures[row][column];
    					}
    						average = total / temperatures[row].length;
    						System.out.println("Week "+week+" average temperature is " + average + ".");
    						week++;
    			}
     
    	 	System.out.println();
     
    	 	//This gives me the daily average.
    		int day = 1;
    		for(column = 0;column < 7;column++)
    			{  
    				total = 0;
    				int average = 0;
    				for(row = 0;row < temperatures.length;row++)
     
    						total = total + temperatures[row][column];
     
    						average = total / temperatures.length;
    						System.out.println("Day "+ day +" average temperature is "+ average + ".");
    						day++;
    			}
     
    		System.out.println();
     
    		//This gives highest and lowest temperatures for each week.
    		int week2 = 1;
    		int highest = temperatures[0][0];
    		int lowest = temperatures[0][0];
    		for(row = 0;row < temperatures.length;row++)
    			{  
    				total = 0;
    				for(column = 0;column < temperatures[row].length;column++)
    					{
    						if(temperatures[row][column] >= highest)
    							highest = temperatures[row][column];
    						else if (temperatures[row][column] >= lowest)
    							lowest = temperatures[row][column];
     
    					}
    						System.out.println("Week "+week2+" highest temperature is " + highest + ".");
    						System.out.println("Week "+week2+" lowest temperature is " + lowest + ".");
    						week2++;
    			}	
     
    	 	System.out.println();
     
    	 	//This gives highest and lowest temperatures for each day.
    		int day2 = 1;
    		int highest2 = temperatures[0][0];
    		int lowest2 = temperatures[0][0];
    		for(column = 0;column < 7;column++)
    			{  
    				for(row = 0;row < temperatures.length;row++)
     
    						if (temperatures[row][column] > highest2)
    							highest2 = temperatures[row][column];
    						else if (temperatures[row][column] < lowest2)
    							lowest2 = temperatures[row][column];
     
    						System.out.println("Day "+ day2 +" highest temperature is "+ highest2 + ".");
    						System.out.println("Day "+ day2 +" lowest temperature is "+ lowest2 + ".");
    						day2++;
    			}
     
    		System.out.println();
     
    		//This gives me the total sum of the array.
    		int total2 = 0;
    		for(row = 0;row < temperatures.length;row++)
    				{  
    					for(column = 0;column < temperatures[row].length;column++)
    					total2 = total2 + temperatures[row][column];
    				}
    					System.out.println("Total sum of the array is " + total2 + ".");
    		}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Multi-D array. daily max/min/avg, weekly max/min/avg, sum total (99% done)

    Have you learnt about methods? I would move the code for each task into its own method instead of a huge ugly mess in the main method. As to your problem when do you reset max and min temps? The max temp for day 3 is 84 since no other day has a temp greater than that you leave it at 84.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 5
    Last Post: May 24th, 2011, 10:39 AM
  2. [SOLVED] Assigning Values to Multi Array
    By dredjohn in forum Collections and Generics
    Replies: 3
    Last Post: April 18th, 2011, 07:43 PM
  3. Help on 2D Multi Table Array
    By clevel211 in forum Object Oriented Programming
    Replies: 1
    Last Post: February 1st, 2011, 04:28 AM
  4. [SOLVED] How to declare an object of multi-dimension array?
    By FongChengWeng in forum Collections and Generics
    Replies: 7
    Last Post: January 14th, 2011, 01:17 AM
  5. Multi Dimensional Array help NEEDED URGENT
    By bonjovi4u in forum Loops & Control Statements
    Replies: 5
    Last Post: February 13th, 2010, 12:44 AM