Results 1 to 20 of 23
Thread: Comparing output of two arrays
- 03-28-2012, 08:26 PM #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[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;
//Prints out the temperature readings
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 #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Comparing output of two arrays
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-28-2012, 08:35 PM #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?
- 03-28-2012, 08:39 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Comparing output of two arrays
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-28-2012, 08:58 PM #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[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 #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Comparing output of two arrays
Nope, and if you'd tried to compile it you would've known; better try:
a bit of advice: please read some tutorials that explain the basics and the syntax of the Java language.Java Code:double[] array = ( 45, 65, 22, 34, 56, 25, 67, 24, 55, 25, 33, 35, 43, 45 );
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-28-2012, 09:49 PM #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 );
^
- 03-29-2012, 06:16 AM #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[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;
//Prints out the temperature readings
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 #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 03-29-2012, 07:50 AM #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
- 03-29-2012, 07:56 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Comparing output of two arrays
That should be easy; now you have this:
Change that first line in your loop to this:Java Code:for (index = 0; index < 14; index++) { temperature[index] = keyboard.nextFloat(); ...
i.e. instead of reading a next number from an input stream, you obtain it from your second array.Java Code:for (index = 0; index < 14; index++) { temperature[index] = temperature2[index]; ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-29-2012, 08:01 AM #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[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;
//Prints out the temperature readings
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 #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: Comparing output of two arrays
Have you read my previous reply #11? (I named the array 'temperature2', you named it 'array')
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-29-2012, 06:18 PM #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[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;
//Prints out the temperature readings
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 #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-29-2012, 07:00 PM #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.
- 03-30-2012, 05:08 PM #17
Member
- Join Date
- Mar 2012
- Posts
- 51
- Rep Power
- 0
Re: Comparing output of two arrays
Did you see what I mean?
- 03-30-2012, 05:19 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-30-2012, 05:30 PM #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
- 03-30-2012, 06:24 PM #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[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];
//Prints out the temperature readings
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)
Similar Threads
-
comparing 2 arrays to see if they're equal
By Get_tanked in forum New To JavaReplies: 2Last Post: 02-17-2011, 06:59 AM -
Comparing Arrays in Java
By Daykeem in forum New To JavaReplies: 6Last Post: 01-15-2011, 04:25 AM -
Comparing arrays
By mitty in forum New To JavaReplies: 8Last Post: 04-14-2010, 11:55 AM -
Comparing two Char arrays
By viperlasson in forum New To JavaReplies: 3Last Post: 01-30-2010, 08:05 AM -
comparing arrays..
By circuspeanuts in forum New To JavaReplies: 5Last Post: 05-25-2009, 07:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks