-2

So I have searched for different methods, and they all seem WAY to complicated. So exactly what do I need to do, I would prefer not use the array util, in order to print the array? So far I have this:

public static void main(String[] args) 
{
    int a[] = {2, 3, 5, 7, 11, 13};

    int sum = 0;

    System.out.printf("The average of  \n" + a );

    for(int i = 0; i < a.length; i++)
        sum = sum + a[i];

    double avg = sum / a.length;

    System.out.println("\nis\n  " + avg);
}

And it prints this:

The average of
[I@1bd0dd4 is 6.0

Which I am assuming (not sure I am correct) means that it is printing the location of the array not what is contained. (Please do correct me if I am wrong) As well, we were given the answer which is 6.83 and mine is reading out at 6.0 which means I am doing something wrong. Any ideas as to what how to print the array without using any special libraries and where the math issue is coming from?

NaughtyBear
  • 113
  • 2
  • 8
  • 4
    See http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array and http://stackoverflow.com/questions/19620225/why-double-width-50-110000-the-output-is-0-000000000000000/19620230 – Alexis C. Oct 16 '15 at 21:49
  • 1
    System.out.println(Arrays.toString(a)); – Siddhartha Oct 16 '15 at 21:49
  • 1
    In all actuality though, you shouldn't resist `Arrays#toString`. It's *amazing* and far more readable than anything else. – Makoto Oct 16 '15 at 21:51
  • 1
    You're going to get a nasty surprise when you see the printed average... beware the integer division... – TayTay Oct 16 '15 at 21:52
  • 1
    Also, it's giving you 6.0 because you're using an ```int``` to track sum, and when you do sum/size, it truncates to an integer and then puts it into ```avg```. You need to use ```double```. – Siddhartha Oct 16 '15 at 21:52
  • @Makoto we are not supposed to be using any libraries or utilities. I will create a 2nd main() function momentarily as per the instructions, just trying to get the format down first. – NaughtyBear Oct 16 '15 at 21:57
  • @Siddhartha You just saved me a ton of heartache. Thank you sir/ma'am! – NaughtyBear Oct 16 '15 at 21:57
  • You should tell your professor not to obscure those methods from you. If you get into the industry and you start writing code that loops over an array manually just to print it, you're going to get quite a few looks your way. I entirely understand the rationale as to why you're not allowed to use it, I just wish that you wouldn't get penalized for doing it the conventional way. – Makoto Oct 16 '15 at 21:58
  • @Makoto I understand that. Thus I like to write it this way (and get it right) before doing it the way he wants it done. The class asked him why and he said that there will not always be a special library or method for you to use. So if you do not learn to write it this way, you will be out of luck when it comes to the wire. I attempt it my way first, and then covert to his. May not be the best practice, but in all fairness it gives me a lot of practice. :P – NaughtyBear Oct 16 '15 at 22:02
  • I'm a sir, and you're welcome. – Siddhartha Oct 16 '15 at 22:06

1 Answers1

0
System.out.printf("The average of  \n" + a );

You are print the array object, You should iterate over it and print the content individually. OR you can use the Arrays.toString to print the content of the array. Also cast one of the value (either sum or array length) to double got getting the avg. preciously. Use the System.out.format() for printing the decimal values.

int a[] = {2, 3, 5, 7, 11, 13};
System.out.print("The average of \n" + Arrays.toString(a));  // print the array

for(int i = 0; i < a.length; i++)
  sum = sum + a[i];

double avg = (double)sum / a.length;  // cast one of the value to double

System.out.format(" is %.2f", avg);   // print the output upto two decimal.

Output:

The average of 
[2, 3, 5, 7, 11, 13] is 6.83
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73