'3.16 LAB: Simple statistics Java

Part 1: Given 4 integers, output their product and their average, using integer arithmetic.

Ex: If the input is:

8, 10, 5, 4 the output is:

1600 6

Note: Integer division discards the fraction. Hence the average of 8, 10, 5, 4 is output as 6, not 6.75.

Note: The test cases include four very large input values whose product results in overflow. You do not need to do anything special, but just observe that the output does not represent the correct product (in fact, four positive numbers yield a negative output; wow).

Submit the above for grading. Your program will fail the last test cases (which is expected), until you complete part 2 below.

Part 2: Also output the product and average, using floating-point arithmetic.

Output each floating-point value with three digits after the decimal point, which can be achieved as follows: System.out.printf("%.3f", yourValue);

Ex: If the input is 8, 10, 5, 4, the output is:

1600. 6

1600.000 6.750

Note that fractions aren't discarded, and that overflow does not occur for the test case with large values.

What I have,

import java.util.Scanner;

public class LabProgram {

  public static void main(String[] args) {

      Scanner scnr = new Scanner(System.in);
      int num1;
      int num2;
      int num3;
      int num4;

        num1 = scnr.nextInt();
        num2 = scnr.nextInt();
        num3 = scnr.nextInt();
        num4 = scnr.nextInt();

        double average_arith = (num1+num2+num3+num4)/4.0;
        double product_arith = num1*num2*num3*num4;

        int result1 = (int) average_arith;
        int result2 = (int) product_arith;

        System.out.printf("%d %d\n",result2,result1);
        System.out.printf("%.3f %.3f\n",product_arith,average_arith);

   }
}

So When I input example numbers everything checks out except overflow,

input: 100000 200000 300000 500000

Expected output:

-1679818752 275000

 3000000000000000000000.000 275000.000

My output:

-1679818752 275000

-1679818752.000 275000.000

Any help please?



Solution 1:[1]

The result of multiplying two ints is an int. So here, you multiply the inputted numbers as ints (which causes them to overflow), and only then promote the result to a double. In order to get a non-overlowing result, you need to have at least one of the operands in each multiplication as a double. Since the * is left-associative, casting num1 to a double should do the trick:

double product_arith = ((double) num1) * num2 * num3 * num4;
-- Here ----------------^

Solution 2:[2]

I just did this lab too!

Mureinik has it right BUT I made a second product variable just for the overflow, and used the first one for the int. Does that make sense?

Solution 3:[3]

try using this code

  int average;
  average = (num1 * num2 * num3 * num4);
  int product;
  product = (num1 + num2 + num3 + num4) / 4;
  double product2;
  double average2;
  average2 = ((double)num1 + num2 + num3 + num4 / 4.0);
  product2 = ((double)num1 * num2 * num3 * num4);
 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mureinik
Solution 2 Trevor anderson
Solution 3 Nour Ibrahim