# Thread: Comparing output of two arrays

1. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Comparing output of two arrays

I'm pretty sure I have this code setup way wrong. First I was to create a array that was dynamic and allowed for input of temperature inputs. Then it would display if the average of the 14 temperatures was above, below, or is the average.

Now I'm trying to create a hard coded array within the code. I will take the average of the hard coded array against the average of the dynamic array.

Can some one please point me in the right direction?

Thanks

import java.util.*;

public class ArrayOfTemperatures
{
public static void main(String[] args)
{

//Declaring Variables
float[] temperature = new float;
double temperature2 = {45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45};
int index2;
int index;
double sum, average, sum2, average2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 14 temperatures: ");
sum = 0;
sum2 = 0;

sum2 = temperature2;

average2 = sum2/14;

for (index = 0; index < 14; index++)
{
temperature[index] = keyboard.nextFloat();
sum = sum + temperature[index];
}
//Creates the average for the first array, It is also dynamic
average = sum/temperature.length;

System.out.println("The average temperature is " + average);
System.out.println("The temperatures are ");
for (index = 0; index < 14; index++)
{
if (temperature[index] < average)
System.out.println(temperature[index] + " colder than previous average ");
else if (temperature[index] > average)
System.out.println(temperature[index] + " hotter than previous average ");
else if (temperature[index] == average)
System.out.println(temperature[index] + " is the avg ");
}  Reply With Quote

2. ## Re: Comparing output of two arrays Originally Posted by bad biddy double temperature2 = {45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45};
This line doesn't make sense: you declare a single double but you want to initialize it to (presumably) an array of doubles. Your compiler doesn't like it.

kind regards,

Jos  Reply With Quote

3. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

Should i declare it as a float instead?  Reply With Quote

4. ## Re: Comparing output of two arrays Originally Posted by bad biddy Should i declare it as a float instead?
Nope, declare the variable as a double array (double[]) or initialize it with a single double variable. What you have now is simply wrong and it won't pass the compiler.

kind regards,

Jos  Reply With Quote

5. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

import java.util.*;

public class ArrayOfTemperatures
{
public static void main(String[] args)
{

//Declaring Variables
float[] temperature = new float;
double array = (double[45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45]);
int index2;
int index;
double sum, average, sum2, average2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 14 temperatures: ");
sum = 0;
sum2 = 0;

Is this how it should be written?  Reply With Quote

6. ## Re: Comparing output of two arrays Originally Posted by bad biddy Is this how it should be written?
Nope, and if you'd tried to compile it you would've known; better try:

Java Code:
`double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );`
a bit of advice: please read some tutorials that explain the basics and the syntax of the Java language.

kind regards,

Jos  Reply With Quote

7. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

I tried to compile it and came across some errors.

Sorry if it feels like your spoon feeding me. But sometimes I just can't figure it out.

[bdewall@hermes ~]\$ javac ArrayOfTemperatures.java
ArrayOfTemperatures.java:11: ')' expected
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^
ArrayOfTemperatures.java:11: <identifier> expected
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^
ArrayOfTemperatures.java:11: illegal start of expression
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^
ArrayOfTemperatures.java:11: ';' expected
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^
ArrayOfTemperatures.java:11: illegal start of expression
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^
ArrayOfTemperatures.java:11: ';' expected
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^
ArrayOfTemperatures.java:11: illegal start of expression
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^
ArrayOfTemperatures.java:11: ';' expected
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^  Reply With Quote

8. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

So i basically started over. He is the code i created to make the first array work

import java.util.*;

public class ArrayOfTemperatures
{
public static void main(String[] args)
{

//Declaring Variables
float[] temperature = new float;
int index;
double sum, average;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 14 temperatures: ");
sum = 0;

for (index = 0; index < 14; index++)
{
temperature[index] = keyboard.nextFloat();
sum = sum + temperature[index];
}
//Creates the average for the first array, It is also dynamic
average = sum/temperature.length;

System.out.println("The average temperature is " + average);
System.out.println("The temperatures are ");
for (index = 0; index < 14; index++)
{
if (temperature[index] < average)
System.out.println(temperature[index] + " below average ");
else if (temperature[index] > average)
System.out.println(temperature[index] + " above average ");
else if (temperature[index] == average)
System.out.println(temperature[index] + " is the avg ");
}

//Prints out a simple sentence
System.out.println("An array a day can help increase your pay! ");

}

}

This is what it looks like compiled and ran

[bdewall@hermes ~]\$ java ArrayOfTemperatures
Enter 14 temperatures:
10
80
74
28
12
45
67
54
34
58
93
25
87
12
The average temperature is 48.5
The temperatures are
10.0 below average
80.0 above average
74.0 above average
28.0 below average
12.0 below average
45.0 below average
67.0 above average
54.0 above average
34.0 below average
58.0 above average
93.0 above average
25.0 below average
87.0 above average
12.0 below average
An array a day can help increase your pay!

Now this is what I'm trying to do:

Create a second array to store a hard-coded 14 days worth a temperatures which are: 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45

Then i have to compare the average of the current period to the previous period and print out a statement describing their relationship. Such as the previous average was 7 degrees higher.

I'm just having problems figuring this part out. I can see it in my head i just can't figure out how to put it into code.  Reply With Quote

9. ## Re: Comparing output of two arrays Originally Posted by bad biddy I tried to compile it and came across some errors.

Sorry if it feels like your spoon feeding me. But sometimes I just can't figure it out.

Java Code:
```[bdewall@hermes ~]\$ javac ArrayOfTemperatures.java
ArrayOfTemperatures.java:11: ')' expected
double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
^```
Why did you change the curly brackets to parentheses? (Also see previous posts).

kind regards,

Jos  Reply With Quote

10. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

Blanked my mind.... Just changed it.

Now I have to figure out how to compare the two  Reply With Quote

11. ## Re: Comparing output of two arrays

That should be easy; now you have this:

Java Code:
```for (index = 0; index < 14; index++)
{
temperature[index] = keyboard.nextFloat();
...```
Change that first line in your loop to this:

Java Code:
```for (index = 0; index < 14; index++)
{
temperature[index] = temperature2[index];
...```
i.e. instead of reading a next number from an input stream, you obtain it from your second array.

kind regards,

Jos  Reply With Quote

12. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

Before I saw you reply I went ahead and changed a lot of my code

import java.util.*;

public class ArrayOfTemperatures
{
public static void main(String[] args)
{

//Declaring Variables
float[] temperature = new float;
double[] array = { 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 };
int index;
double sum, sum2, average, average2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 14 temperatures: ");
sum = 0;
sum2 = 0;

for (index = 0; index < 14; index++)
{
temperature[index] = keyboard.nextFloat();
sum = sum + temperature[index];
sum2 = sum2 + array[index];

}
//Creates the average for the first array, It is also dynamic
average = sum/temperature.length;
average2 = sum2/14;

System.out.println("The average temperature is " + average);
System.out.println("The temperatures are ");
for (index = 0; index < 14; index++)
{
if (temperature[index] < average2)
System.out.println(temperature[index] + " below average ");
else if (temperature[index] > average2)
System.out.println(temperature[index] + " above average ");
else if (temperature[index] == average2)
System.out.println(temperature[index] + " is the avg ");
}

//Prints out a simple sentence
System.out.println("An array a day can help increase your pay! ");

}

}

Here it is. I just can't figure out how allow it describe the relationship as in if the dynamic array was for example 7 or 8 degrees higher than the hard coded array.  Reply With Quote

13. ## Re: Comparing output of two arrays

Have you read my previous reply #11? (I named the array 'temperature2', you named it 'array')

kind regards,

Jos  Reply With Quote

14. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

Just made the changes. here is the code:

import java.util.*;

public class ArrayOfTemperatures
{
public static void main(String[] args)
{

//Declaring Variables
float[] temperature = new float;
double[] temperature2 = { 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 };
int index;
double sum, sum2, average, average2;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 14 temperatures: ");
sum = 0;
sum2 = 0;

for (index = 0; index < 14; index++)
{
temperature[index] = temperature2[index];
sum = sum + temperature[index];

}
//Creates the average for the first array, It is also dynamic
average = sum/temperature.length;

System.out.println("The average temperature is " + average);
System.out.println("The temperatures are ");
for (index = 0; index < 14; index++)
{
if (temperature[index] < temperature2[index])
System.out.println(temperature[index] + " below average ");
else if (temperature[index] > temperature2[index])
System.out.println(temperature[index] + " above average ");
else if (temperature[index] == temperature[index])
System.out.println(temperature[index] + " is the avg ");
}

//Prints out a simple sentence
System.out.println("An array a day can help increase your pay! ");

}

Only problem is when i compile it im getting this error

[bdewall@hermes ~]\$ javac ArrayOfTemperatures.java
ArrayOfTemperatures.java:21: possible loss of precision
found : double
required: float
temperature[index] = temperature2[index];
^
1 error

Im guessing its getting this error because temperature2 is a double and temperature is a float  Reply With Quote

15. ## Re: Comparing output of two arrays

So change the other array to a float[] array ... (or change one array to a double[] array) or cast the values whenever you need them ...

kind regards,

Jos  Reply With Quote

16. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

Duh why didn't i think of that.....

I don't think you get what I am trying to do with this program. The user will input temperature for 14 days and the temperatures he/she has added are compared to the temperatures that were hard coded.

This is the output as of right now

[bdewall@hermes ~]\$ java ArrayOfTemperatures
Enter 14 temperatures:
The average temperature is 41.0
The temperatures are
45.0 is the avg
65.0 is the avg
22.0 is the avg
34.0 is the avg
56.0 is the avg
25.0 is the avg
67.0 is the avg
24.0 is the avg
55.0 is the avg
25.0 is the avg
33.0 is the avg
35.0 is the avg
43.0 is the avg
45.0 is the avg
An array a day can help increase your pay!

Which is saying the temps are just being grabbed from the hard coded array and not allowing the user to enter in 14 more temps.  Reply With Quote

17. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

Did you see what I mean?  Reply With Quote

18. ## Re: Comparing output of two arrays

Yep, I read what you mean: you want to compare the user supplied values against the values given in your array. You should at least make an attempt to solve this little problem.

kind regards,

Jos  Reply With Quote

19. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

I'm thinking I could have both temperature indexes and have the inputted value array subtract the value given by dynamic array.

I'll try experimenting with it in 30 mins. I'll let ya know what I figure out  Reply With Quote

20. Member Join Date
Mar 2012
Posts
51
Rep Power
0

## Re: Comparing output of two arrays

Working on it some more. Here is the newest code

import java.util.*;

public class ArrayOfTemperatures
{
public static void main(String[] args)
{

//Declaring Variables
float[] temperature = new float;
float[] temperature2 = { 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 };
int index;
double sum, sum2, average, average2, tempValue;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter 14 temperatures: ");
sum = 0;
sum2 = 0;

for (index = 0; index < 14; index++)
{
temperature[index] = keyboard.nextFloat();
sum = sum + temperature[index];

}
//Creates the average for the first array, It is also dynamic
average = sum/temperature.length;
average2 = sum2/temperature2[index];

tempValue = temperature[index] - temperature2[index];

System.out.println("The average temperature is " + average);
System.out.println("The temperatures are ");
for (index = 0; index < 14; index++)
{
if (temperature[index] < temperature2[index])
System.out.println(tempValue + " below average ");
else if (temperature[index] > temperature2[index])
System.out.println(tempValue + " above average ");
else if (temperature[index] == temperature[index])
System.out.println(tempValue + " is the avg ");
}

//Prints out a simple sentence
System.out.println("An array a day can help increase your pay! ");

}

It can compile but Im getting this error:

[bdewall@hermes ~]\$ java ArrayOfTemperatures
Enter 14 temperatures:
45
76
34
56
90
12
34
56
73
25
67
34
56
44
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14
at ArrayOfTemperatures.main(ArrayOfTemperatures.java: 28)  Reply With Quote

#### Posting Permissions

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