Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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 ");
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Comparing output of two arrays

    Quote Originally Posted by bad biddy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Comparing output of two arrays

    Should i declare it as a float instead?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Comparing output of two arrays

    Quote Originally Posted by bad biddy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Comparing output of two arrays

    Quote Originally Posted by bad biddy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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 );
    ^

  8. #8
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Comparing output of two arrays

    Quote Originally Posted by bad biddy View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Comparing output of two arrays

    Blanked my mind.... Just changed it.

    Now I have to figure out how to compare the two

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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.

  17. #17
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default Re: Comparing output of two arrays

    Did you see what I mean?

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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

  20. #20
    bad biddy is offline Member
    Join Date
    Mar 2012
    Posts
    51
    Rep Power
    0

    Default 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)

Page 1 of 2 12 LastLast

Similar Threads

  1. comparing 2 arrays to see if they're equal
    By Get_tanked in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 07:59 AM
  2. Comparing Arrays in Java
    By Daykeem in forum New To Java
    Replies: 6
    Last Post: 01-15-2011, 05:25 AM
  3. Comparing arrays
    By mitty in forum New To Java
    Replies: 8
    Last Post: 04-14-2010, 12:55 PM
  4. Comparing two Char arrays
    By viperlasson in forum New To Java
    Replies: 3
    Last Post: 01-30-2010, 09:05 AM
  5. comparing arrays..
    By circuspeanuts in forum New To Java
    Replies: 5
    Last Post: 05-25-2009, 08:05 AM

Posting Permissions

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