HELP in arrays with methods

two dimentional arrays

Page 1 of 1

9 Replies - 2031 Views - Last Post: 02 April 2007 - 07:50 AM Rate Topic: -----

#1 wille2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-October 06

HELP in arrays with methods

Posted 30 March 2007 - 05:25 PM

I'm suppose to use a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and higest and lowest temperature of the year.

Compile and input 1,2,3,4,5,6,7,8,9,10,11,12 and repeat.

This turns out like this:

Highest Index Temps: 2
Highest Average Temps: 2.0
Lowest Index Temps: 3
Lowest Average Temps: 2.0

Right now I am having problems with my arrays between the methods and parameters trying:

public class Temp
{
  private int column, row, IndexHigh,IndexLow,highTemps, lowTemps, lowest, largest,AvgHigh, AvgLow;

  private int[][] yearTemp;

  public Temp()
  {
	AvgHigh = 0;
	AvgLow = 0;
	IndexHigh = 0;
	IndexLow = 0;
	row = 0;
	column = 0;
	largest = 0;
  }



  public void sethighTemps(int [][]arrhigh1)
  {	
	
	for(column = 0; column <= arrhigh1.length; column++)
	{
	highTemps = arrhigh1[0][column];
	}
  }
  public int gethighTemps()
  {
	return highTemps;
  }



  public void setlowTemps(int [][]arrlow1)
  {
	for(column = 0; column <= arrlow1.length; column++)
	{
	lowTemps = arrlow1[1][column];
	}
  }
  public int getlowTemps()
  {
	return lowTemps;
  }




  public void setIndexHigh(int[][] arrhigh2)
  {
	for(column = 0; arrhigh2.length > column; column++)
	{
		if(largest < arrhigh2[0][column])
			{
			largest = arrhigh2[0][column];
			}
	}
	
  }
  public int getIndexHigh()
  {
	return largest;
  }
  


  public void setIndexLow(int[][] arrlow2)
  {
	for(column = 0; arrlow2.length >= column; column++)
	{
		int low = arrlow2[1][column];

		lowest = Math.min(low ,arrlow2[1][column]);

		//f(lowest < arrlow[1][column])
		//   {
		//int low = arrlow[1][column];
		//   }   
	}	
  }
  public int getIndexLow()
  {
	return lowest;
  }



  public void setAvgHigh(int[][] arrhigh3)
  {
	for(column = 0; arrhigh3.length >= column; column++)
	{
	AvgHigh = (AvgHigh + arrhigh3[0][column]) /arrhigh3.length;
	}
  }
  public double getAvgHigh()
  {
	return AvgHigh;
  }



  public void setAvgLow(int[][] arrlow3)
  {
	for(column = 0; arrlow3.length >= column; column++)
	{
	AvgLow = (AvgLow + arrlow3[1][column]) /arrlow3.length;
	}
  }
  public double getAvgLow()
  {
	return AvgLow;
  }

}



import java.util.*;
public class TempStats
{
  public static void main(String [] args)
  {

	int[][] arrhigh;
	int[][] arrlow;
	int largest = 0;
	int lowest = 0;
	double AvgHigh = 0;
	double AvgLow = 0;

	Scanner reader = new Scanner(System.in);
	Temp temperature = new Temp();

	int Rowhighlow = 2;
	int ColumnTemps = 12;
	int[][] MyTemps = new int[Rowhighlow][ColumnTemps];

	  int month = 1;

	  for(int i = 0; i < ColumnTemps; i++)
	  {
	  System.out.print("Enter Highest Temperature for Month "+month +" :");
	  MyTemps[0][i] = reader.nextInt();
	  temperature.sethighTemps(MyTemps);

	  temperature.setIndexHigh(MyTemps);
	  largest = temperature.getIndexHigh();

	  temperature.setAvgHigh(MyTemps);
	  AvgHigh = temperature.getAvgHigh();

	  month += 1;
	  }


	month = 1;


	  for(int i = 0; i < ColumnTemps; i++)
	  {
		
	  System.out.print("Enter Lowest Temperature for Month "+month +" :");
	  MyTemps[1][i] = reader.nextInt();
	  temperature.setlowTemps(MyTemps);

	  temperature.setIndexLow(MyTemps);
	  lowest = temperature.getIndexLow();

	  temperature.setAvgLow(MyTemps);
	  AvgLow = temperature.getAvgLow();

	  month += 1;
	  }
	System.out.println();
	System.out.println("Highest Index Temps: "+largest);
	System.out.println("Highest Average Temps: "+AvgHigh);
	System.out.println("Lowest Index Temps: "+lowest);
	System.out.println("Lowest Average Temps: "+AvgLow);
	
  }
}


It should run like this:

Highest Index Temps: 12
Highest Average Temps: 6.5
Lowest Index Temps: 1
Lowest Average Temps: 6.5

i just noticed it just runs at index of 2 and stops there

Is This A Good Question/Topic? 0
  • +

Replies To: HELP in arrays with methods

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: HELP in arrays with methods

Posted 31 March 2007 - 11:46 AM

in loops such as
  public void sethighTemps(int [][]arrhigh1)
  {	
	for(column = 0; column <= arrhigh1.length; column++)
	{


arrhigh1.length will be 2 not 12
Was This Post Helpful? 0
  • +
  • -

#3 wille2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-October 06

Re: HELP in arrays with methods

Posted 31 March 2007 - 09:46 PM

so u are saying this:
	int Rowhighlow = 2;
	int ColumnTemps = 12;


should be this?
	int Rowhighlow = 2;
	int ColumnTemps = 2;

Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: HELP in arrays with methods

Posted 01 April 2007 - 12:22 AM

View Postwille2511, on 1 Apr, 2007 - 04:46 AM, said:

so u are saying this:
	int Rowhighlow = 2;
	int ColumnTemps = 12;


should be this?
	int Rowhighlow = 2;
	int ColumnTemps = 2;

no I mean the value returned by arrhigh1.length is 2
Was This Post Helpful? 0
  • +
  • -

#5 wille2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-October 06

Re: HELP in arrays with methods

Posted 01 April 2007 - 10:11 AM

then is there a way i can make the length to 12 and not 2 cause i need to input two temperatures of the 12 months for this program to work?
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: HELP in arrays with methods

Posted 01 April 2007 - 10:22 AM

You can use the number 12 directly...or the variable ColumnTemps.
Was This Post Helpful? 0
  • +
  • -

#7 wille2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-October 06

Re: HELP in arrays with methods

Posted 01 April 2007 - 03:55 PM

Would someone try to show me where in the code should be change at?


View PostAmadeus, on 1 Apr, 2007 - 10:22 AM, said:

You can use the number 12 directly...or the variable ColumnTemps.



i think number 12 directly gives me an runtime error
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: HELP in arrays with methods

Posted 01 April 2007 - 04:12 PM

Have you tried?
for(column = 0; column <= 11; column++)


Remember, since java arrays are zero indexed, you are beginning at zero...the 12th element is at position 11.
Was This Post Helpful? 0
  • +
  • -

#9 wille2511  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 24-October 06

Re: HELP in arrays with methods

Posted 02 April 2007 - 07:05 AM

it wont work cause the output will show like this

Enter Highest Temperature for Month 1 :1
Enter Highest Temperature for Month 2 :2
Enter Highest Temperature for Month 3 :3
Enter Highest Temperature for Month 4 :4
Enter Highest Temperature for Month 5 :5
Enter Highest Temperature for Month 6 :6
Enter Highest Temperature for Month 7 :7
Enter Highest Temperature for Month 8 :8
Enter Highest Temperature for Month 9 :9
Enter Highest Temperature for Month 10 :10
Enter Highest Temperature for Month 11 :11
Enter Lowest Temperature for Month 1 :1


it missed month 12.

i just need the problem about my arrays trying to send 12 indexs to Temps i inputed and not to end up just sending 2 or 3 indexs and the rest will end up useless
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: HELP in arrays with methods

Posted 02 April 2007 - 07:50 AM

There are a couple of reasons why it is performing that way...first and foremost, you are using a variable named months as the display, and a variable named i for the counter. As they serve the same purpose, you can probably get away with one of them. Perhaps something like:
for(int i = 0; i < ColumnTemps; i++){
   System.out.print("Enter Lowest Temperature for Month "+(i+1) +" :");
   ///rest of your code.
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1