# temperature 2 dimensional array

Page 1 of 1

## 13 Replies - 8285 Views - Last Post: 02 February 2009 - 02:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=80667&amp;s=6e1521515d87b31ff4c6507f6ac53797&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cici2004

Reputation: 0
• Posts: 6
• Joined: 13-January 09

# temperature 2 dimensional array

Posted 13 January 2009 - 10:11 AM

The 2 underlined parts require a return statement but cannot find symbol for what I have. I'm trying to get 10 inputs for fahrenheit and celsius and then print the max temperature.

```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package project3;

import java.util.Scanner;/**
*
*/
public class Program7 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

double fahrenheit; // degrees in Fahrenheit
double celsius; // degrees in Celsius
int choice; // 1 for F to C and 2 for C to F
celsius = 0;
fahrenheit = 0;

do {
System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
choice = input.nextInt();

if (choice == 1)
celsius = tempC(celsius);
else if (choice == 2)
fahrenheit = tempF(fahrenheit);
else if (choice == 3)
exit();
else System.out.println("Invalid selection");

}  while (choice != 3);
}

public static double tempC (double celsius) {
Scanner input = new Scanner(System.in);
System.out.print("Enter degrees fahrenheit");
double fahrenheit = input.nextDouble();
double[][] inputFahr = new double[10][2];
for (int i = 0; i < inputFahr.length; i++)
inputFahr[i][0] = fahrenheit;
celsius = (5.0/9)*(fahrenheit - 32);
for (int i = 0; i < inputFahr.length; i++)
inputFahr[i][1] = celsius;
System.out.println(fahrenheit + " degrees fahrenheit equals " +
celsius + " degrees celsius");
return celsius;
}

public static double max(double[][] inputFahr) {
for (int i = 0; i < inputFahr[i].length; i++) {
double max = inputFahr[i][0];
if (inputFahr[i][0] > max)
max = inputFahr[i][0];
System.out.println("Maximum temperature is " + max);
return max;
}
[u]return max;[/u]
}

public static double tempF (double fahrenheit) {
Scanner input = new Scanner(System.in);

System.out.print("Enter degrees celsius");
double celsius = input.nextDouble();
double[][] inputCel = new double[10][2];
for (int i = 0; i < inputCel.length; i++)
inputCel[i][1] = celsius;
fahrenheit = (9.0/5)* celsius + 32;
for (int i = 0; i < inputCel.length; i++)
inputCel[i][0] = fahrenheit;
System.out.println(celsius + " degrees celsius equals " +
fahrenheit + " degrees fahrenheit");
return fahrenheit;
}

public static double max2(double[][] inputCel) {
for (int i = 0; i < inputCel[i].length; i++) {
double max2 = inputCel[i][1];
if (inputCel[i][1] > max2)
max2 = inputCel[i][1];
System.out.println("Maximum temperature is " + max2);
return max2;
}
[u]return max2;[/u]
}

public static void exit (){
System.out.println("Goodbye");
// TODO code application logic here
}

}

```

Is This A Good Question/Topic? 0

## Replies To: temperature 2 dimensional array

### #2 Martyr2

• Programming Theoretician

Reputation: 5227
• Posts: 14,010
• Joined: 18-April 07

## Re: temperature 2 dimensional array

Posted 13 January 2009 - 10:41 AM

First of all in each of those functions you are going to want to get rid of the first "return" statement. Because what is going to happen is that you are going to enter your loop and first iteration it will return, not processing anything else. So look at the logic there for a moment and make sure you completely loop through the arrays testing each against max or max2 and setting the result. At the end of the loop you will then return the max.

Next, you will want to first define max/max2 to something PRIOR to entering the loop. Because remember variables defined in a loop are only local to that loop. This is called "local scope" and so you need to put it in a scope that is the whole function. You do that by putting it inside the function, but outside the loop. That way the return statement at the end can see it.

```public static double max2(double[][] inputCel) {
// Set initial value for max2
double max2 = inputCel[0][1];

// Now loop through the array and set max2 to a higher value as we come across it
for (int i = 0; i < inputCel[i].length; i++) {
if (inputCel[i][1] > max2) {
max2 = inputCel[i][1];
}
}

// Now that max2 is defined in function scope, we can return it.
return max2;
}

```

So take a look at the in code comments on this and you should see what I am doing here. This hasn't been fully tested but should work just fine from what I can see here. Model the other function just like this as well. Enjoy!

"At DIC we be max2 setting code ninjas... we also set the bar extremely low when it comes to the ladies. jk"

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12317
• Posts: 45,417
• Joined: 27-December 08

## Re: temperature 2 dimensional array

Posted 13 January 2009 - 12:33 PM

Rather than using a 2d array, I'd create 2 arrays of equal size (or better yet an arraylist) and use 1 findMax method.

### #4 cici2004

Reputation: 0
• Posts: 6
• Joined: 13-January 09

## Re: temperature 2 dimensional array

Posted 14 January 2009 - 08:55 AM

I changed the max and max2, but I am having problem defining max/max2 and what it is equal to. This is what I have.
``` public static void main(String[] args) {
Scanner input = new Scanner(System.in);

double fahrenheit; // degrees in Fahrenheit
double celsius; // degrees in Celsius
int choice; // 1 for F to C and 2 for C to F
celsius = 0;
fahrenheit = 0;
double max;
double max2;
max = max(inputFahr[0][0]);
max2 = max2(inputCel[1][0]);
do {
System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
choice = input.nextInt();

if (choice == 1)
celsius = tempC(celsius);
else if (choice == 2)
fahrenheit = tempF(fahrenheit);
else if (choice == 3)
exit();
else System.out.println("Invalid selection");
System.out.println("Maximum temperature is " + max);
System.out.println("Maximum temperature is " + max2);
}  while (choice != 3);

}
```

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12317
• Posts: 45,417
• Joined: 27-December 08

## Re: temperature 2 dimensional array

Posted 14 January 2009 - 10:44 AM

It looks like you're placing elements from the 2d array into the max variable. Try using the max variable instead to hold indices. Let me go through an algorithm and let you implement it:

```0. Start
1. max <-- 1, get array[]
2. for(i = 0; i < array.length; i++)
3.   if(array[i] > array[max])
4.		  max = i;
END IF
END FOR
5. return max;

```

When implementing this method, you can also specify which section of the 2d array you want to view, so you don't have write 2 methods for farenheir and celsius. Let me show you:

```public static int findMax(int[][] x, int index){
for(int i = 0; i < length of side1, i++){
//x[i][index]
}
}

```

Hope this helps!

### #6 Martyr2

• Programming Theoretician

Reputation: 5227
• Posts: 14,010
• Joined: 18-April 07

## Re: temperature 2 dimensional array

Posted 15 January 2009 - 01:04 PM

You are passing your arrays wrong to the functions. You pass just the name, not the subscripts. By using the subscripts you are saying that pass the VALUE located at that subscript. Pass the whole array by name. I did hit one snag in the code for max and max2 that I gave you, so change inputCel[i].length to inputCel[0].length because all dimensions will be the same length we want to get the length from the first row of the array.

Here is an example code showing you how this would work....

```public class passarray {
public static void main(String args[]) {
double[][] inputCelcius = new double[2][2];
inputCelcius[0][0] = 10.0;
inputCelcius[0][1] = 5.78;
inputCelcius[1][0] = 7.88;
inputCelcius[1][1] = 6.23;

// Notice how we pass the array just by name, not with any subscripts.
double topValue = max2(inputCelcius);

System.out.println("Top value in second dimension is: " + topValue);

}

public static double max2(double[][] inputCel) {
// Set initial value for max2
double max2 = inputCel[0][1];

// Now loop through the array and set max2 to a higher value as we come across it
for (int i = 0; i < inputCel[0].length; i++) {
if (inputCel[i][1] > max2) {
max2 = inputCel[i][1];
}
}

// Now that max2 is defined in function scope, we can return it.
return max2;
}
}

```

At the end our value for "topValue" will be 6.23 because it is the highest value of those in the second dimension (5.78 and 6.23).

But the idea here is how we passed the array to the function. Just use the name, no subscripts. Using subscripts would be trying to pass the value in the array, not the array itself.

Hope that makes sense. Enjoy!

"At DIC we be multidimensional code ninjas... we code our way through the fabric of time and space. We are that leet."

### #7 cici2004

Reputation: 0
• Posts: 6
• Joined: 13-January 09

## Re: temperature 2 dimensional array

Posted 19 January 2009 - 07:43 AM

Now after only putting in one temperature it says maximum is 0.

```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package project3;

import java.util.Scanner;/**
*
*
*/
public class Program7 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

double fahrenheit; // degrees in Fahrenheit
double celsius; // degrees in Celsius
int choice; // 1 for F to C and 2 for C to F
celsius = 0;
fahrenheit = 0;
double max;
double max2;
double[][] inputFahr = new double[10][2];
double[][] inputCel = new double[10][2];
max = maxF(inputFahr);
max2 = maxC(inputCel);

do {
System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
choice = input.nextInt();

if (choice == 1)
celsius = tempC(celsius);
else if (choice == 2)
fahrenheit = tempF(fahrenheit);
else if (choice == 3)
exit();
else System.out.println("Invalid selection");
System.out.println("Maximum temperature is " + max);
System.out.println("Maximum temperature is " + max2);
}  while (choice != 3);

}

public static double tempC (double celsius) {
Scanner input = new Scanner(System.in);
System.out.print("Enter degrees fahrenheit");
double fahrenheit = input.nextDouble();
double[][] inputFahr = new double[10][2];
for (int i = 0; i < inputFahr.length; i++)
inputFahr[i][0] = fahrenheit;
celsius = (5.0/9)*(fahrenheit - 32);
for (int i = 0; i < inputFahr.length; i++)
inputFahr[i][1] = celsius;
System.out.println(fahrenheit + " degrees fahrenheit equals " +
celsius + " degrees celsius");
return celsius;
}
public static double maxF(double[][] inputFahr) {
double max = inputFahr[0][0];
for (int i = 0; i < inputFahr[0].length; i++) {
if (inputFahr[i][0] > max)
{
max = inputFahr[i][0];
}
}
return max;
}

public static double tempF (double fahrenheit) {
Scanner input = new Scanner(System.in);

System.out.print("Enter degrees celsius");
double celsius = input.nextDouble();
double[][] inputCel = new double[10][2];
for (int i = 0; i < inputCel.length; i++)
inputCel[i][1] = celsius;
fahrenheit = (9.0/5)* celsius + 32;
for (int i = 0; i < inputCel.length; i++)
inputCel[i][0] = fahrenheit;
System.out.println(celsius + " degrees celsius equals " +
fahrenheit + " degrees fahrenheit");
return fahrenheit;
}

public static double maxC(double[][] inputCel) {

double max2 = inputCel[0][1];
for (int i = 0; i < inputCel[0].length; i++){
if (inputCel[i][1] > max2)
{	max2 = inputCel[i][1];
}
}
return max2;
}

public static void exit (){
System.out.println("Goodbye");
// TODO code application logic here
}

}

```

### #8 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: temperature 2 dimensional array

Posted 19 January 2009 - 07:59 AM

These lines are from your code..

```double[][] inputFahr = new double[10][2];
double[][] inputCel = new double[10][2];
max = maxF(inputFahr);
max2 = maxC(inputCel);

```

Between declaring the array and calling on the methods to find the maximum, you never assign any arrayslot any value and since the array is initialized to 0 by default, that will be the maximum.

### #9 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: temperature 2 dimensional array

Posted 19 January 2009 - 08:10 AM

It seems you have a couple of problems other than that in your code. Correct me if I'm wrong or if there's something I have overseen with this project.

In this project, you're supposed to:

* Prompt the user for 1 of 3 options, C -> F, F -> C or quit.
* Conversion: read 10 temperatures and convert them to the other scale
* Find the maximum value of the two arrays of temperature

Which unit are you used to? Celsius or Fahrenheit? <- please answer this question

This post has been edited by Gloin: 19 January 2009 - 08:12 AM

### #10 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: temperature 2 dimensional array

Posted 19 January 2009 - 08:47 AM

Personally I would have just made one of each method,

```public static double[] readTemperature() {
//Create an array of double (tempArray)
//Read in the ten temperature-values into tempArray, consider input to be of the unit you're used to.
//Return tempArray
}

public static double maxTemp(double[] temp) {
//Find the maximum value in the array (temp). Use a loop and find the maximum, store it in maxT.
//return maxT
}

public static double convertTemperatureToOtherType(double temp) {
// Here you just apply your conversion formula so if you prefer Fahrenheit you have
// cTemp = (5.0/9)*(temp - 32); otherwise use the inverse
//return cTemp
}

```

As a rule of thumb, a method should be assigned to do one thing only. I.e, don't have a method that takes care of input, calculations and output, make one for input, one for calculations and one for output.
The three methods will be called like following. (First declare two arrays + variables)

```double[] inputFahr = new double[10];
double[] inputCel = new double[10];
double maxC;
double maxF;

inputFahr = readTemperature(); //This will put the input values into the fahrenheit array.
inputCel = readTemperature(); //This will put values into the Celsius array, however, the values are in fahrenheit.
maxF = maxTemp(inputFahr);
maxC = maxTemp(inputCel); // The values are still in Fahrenheit
for (int i=0; i < inputCel.length; i++)
System.out.println(convertTemperatureToOtherType(inputCel[i]));
System.out.println("The maximum temperature was: " + convertTemperatureToCelsius(maxC));

```

The output should really also be taken care of by it's own method.. (The last three lines)

Do you see how this works?
Since you are the programmer you can always control how things should work. You know you're always working with temperatures of different units and you treat the values accordingly.
If you want to compare the values to find the max temperature of the two arrays combined then you compare values from the first and converted values from the other.
Having methods that are dedicated to one task makes the methods much easier to write, understand and correct if anything goes wrong. Even finding errors is easier because you can put in some debugging text in any function to see if it works properly.

If you have any further questions about this, just ask. I would really recommend you change your existing code to this type of design. I'm not saying you should delete your code but use the parts that work to build these smaller methods.

This post has been edited by Gloin: 19 January 2009 - 08:59 AM

### #11 cici2004

Reputation: 0
• Posts: 6
• Joined: 13-January 09

## Re: temperature 2 dimensional array

Posted 27 January 2009 - 09:34 AM

ok this is what i have. still getting maximum equals 0. I'm familiar with both celsius and fahrenheit. sorry it took me so long to post. it doesn't say maximum equals 0 until i push 3 to end program.
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package project3;

import java.util.Scanner;/**
*
*
*/
public class Program7 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

double fahrenheit = 0; // degrees in Fahrenheit
double celsius = 0; // degrees in Celsius
int choice; // 1 for F to C and 2 for C to F
double[] inputFahr = new double[10];
double[] inputCel = new double[10];
double max;
double max2;
max = maxF(inputFahr);
max2 = maxC(inputCel);

do {
System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
choice = input.nextInt();

if (choice == 1)
celsius = tempC(celsius);
else if (choice == 2)
fahrenheit = tempF(fahrenheit);
else if (choice == 3)
exit();
else System.out.println("Invalid selection");

}  while (choice != 3);

System.out.println("Maximum temperature is " + max + " fahrenheit");
System.out.println("Maximum temperature is " + max2 + " celsius");
}

public static double tempC (double celsius) {
Scanner input = new Scanner(System.in);
System.out.print("Enter degrees fahrenheit");
double fahrenheit = input.nextDouble();
double[] inputFahr = new double[10];
for (int i = 0; i < inputFahr.length; i++)
inputFahr[i] = fahrenheit;
celsius = (5.0/9)*(fahrenheit - 32);

System.out.println(fahrenheit + " degrees fahrenheit equals " +
celsius + " degrees celsius");
return celsius;
}
public static double maxF(double[] inputFahr) {
double max = 0;
for (int i = 0; i < inputFahr.length; i++) {
if (inputFahr[i] > max)
{
max = inputFahr[i];
}
}
return max;
}

public static double tempF (double fahrenheit) {
Scanner input = new Scanner(System.in);

System.out.print("Enter degrees celsius");
double celsius = input.nextDouble();
double[] inputCel = new double[10];
for (int i = 0; i < inputCel.length; i++)
inputCel[i] = celsius;
fahrenheit = (9.0/5)* celsius + 32;
System.out.println(celsius + " degrees celsius equals " +
fahrenheit + " degrees fahrenheit");
return fahrenheit;
}

public static double maxC(double[] inputCel) {

double max2 = 0;
for (int i = 0; i < inputCel.length; i++){
if (inputCel[i] > max2)
{	max2 = inputCel[i];
}
}
return max2;
}

public static void exit (){
System.out.println("Goodbye");
// TODO code application logic here
}

}

```

### #12 cici2004

Reputation: 0
• Posts: 6
• Joined: 13-January 09

## Re: temperature 2 dimensional array

Posted 31 January 2009 - 07:13 PM

this is my code now. now my problem is to only get 10 inputs. I've tried putting
public static ArrayList <Double> inputFahr = new ArrayList <Double>(10);
but that doesn't make it stop after 10 inputs.
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package project3;

import java.util.ArrayList;
import java.util.Scanner;/**
*
*
*/
public class Program7 {

public static ArrayList <Double> inputFahr = new ArrayList <Double>();
public static ArrayList <Double> inputCel = new ArrayList <Double>();

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

int choice; // 1 for F to C and 2 for C to F

do {
System.out.print("Enter 1 for F to C, 2 for C to F, and 3 to quit");
choice = input.nextInt();

if (choice == 1)
tempC();
else if (choice == 2)
tempF();
else if (choice == 3)
exit();
else System.out.println("Invalid selection");

}  while (choice != 3);

System.out.println("Maximum temperature is " + max(inputFahr) + " fahrenheit");
System.out.println("Maximum temperature is " + max(inputCel) + " celsius");
}

public static double tempC () {
Scanner input = new Scanner(System.in);
System.out.print("Enter degrees fahrenheit");
double fahrenheit = input.nextDouble();

double celsius = (5.0/9)*(fahrenheit - 32);
System.out.println(fahrenheit + " degrees fahrenheit equals " +
celsius + " degrees celsius");
return celsius;
}

public static double tempF () {
Scanner input = new Scanner(System.in);

System.out.print("Enter degrees celsius");
double celsius = input.nextDouble();

double fahrenheit = (9.0/5)* celsius + 32;
System.out.println(celsius + " degrees celsius equals " +
fahrenheit + " degrees fahrenheit");
return fahrenheit;
}

public static double max(ArrayList <Double> list) {
double result = 0;
for (double p : list) {
if (p > result) {
result = p;
}
}
return result;
}

public static void exit (){
System.out.println("Goodbye");
// TODO code application logic here
}

}

```

### #13 cici2004

Reputation: 0
• Posts: 6
• Joined: 13-January 09

## Re: temperature 2 dimensional array

Posted 02 February 2009 - 08:30 AM

got it figured out. thanks everyone.

### #14 johnmalloy

Reputation: 2
• Posts: 40
• Joined: 24-September 08

## Re: temperature 2 dimensional array

Posted 02 February 2009 - 02:25 PM

Put that part of the code in a loop and limit the loop to 10. That will acomplish what you want.

This post has been edited by johnmalloy: 02 February 2009 - 02:25 PM