# Comparing output of two arrays

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-28-2012, 08:26 PM
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[14];
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 ");
}
• 03-28-2012, 08:33 PM
JosAH
Re: Comparing output of two arrays
Quote:

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
• 03-28-2012, 08:35 PM
Re: Comparing output of two arrays
Should i declare it as a float instead?
• 03-28-2012, 08:39 PM
JosAH
Re: Comparing output of two arrays
Quote:

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
• 03-28-2012, 08:58 PM
Re: Comparing output of two arrays
import java.util.*;

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

//Declaring Variables
float[] temperature = new float[14];
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?
• 03-28-2012, 09:26 PM
JosAH
Re: Comparing output of two arrays
Quote:

Is this how it should be written?

Nope, and if you'd tried to compile it you would've known; better try:

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
• 03-28-2012, 09:49 PM
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 );
^
• 03-29-2012, 06:16 AM
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[14];
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.
• 03-29-2012, 07:44 AM
JosAH
Re: Comparing output of two arrays
Quote:

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.

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
• 03-29-2012, 07:50 AM
Re: Comparing output of two arrays
Blanked my mind.... Just changed it.

Now I have to figure out how to compare the two
• 03-29-2012, 07:56 AM
JosAH
Re: Comparing output of two arrays
That should be easy; now you have this:

Code:

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

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
• 03-29-2012, 08:01 AM
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[14];
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.
• 03-29-2012, 08:40 AM
JosAH
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
• 03-29-2012, 06:18 PM
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[14];
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
• 03-29-2012, 06:37 PM
JosAH
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
• 03-29-2012, 07:00 PM
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.
• 03-30-2012, 05:08 PM
Re: Comparing output of two arrays
Did you see what I mean?
• 03-30-2012, 05:19 PM
JosAH
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
• 03-30-2012, 05:30 PM
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
• 03-30-2012, 06:24 PM
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[14];
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)
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last