Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. import java.io.PrintStream;
  2. import java.util.Scanner;
  3.  
  4. public class VarDiffExtended {
  5. public static void main(String[] args) {
  6. // Initialization and Declaration
  7. Scanner sc = new Scanner(System.in);
  8. PrintStream ps = System.out;
  9. int[] a = new int[499]; // array a with maximum 499 number of elements
  10. int[] b = new int[250]; // array b with maximum 250 number of odd or even elements from array a
  11. int size_of_a = 0; // number of inserted elements - X
  12. int size_of_b = 0; // number of sample elements - Y
  13. int j = 0; // index of b array
  14.  
  15. // Inserting the values to an array
  16. for (int i = 0; sc.hasNextInt(); size_of_a++, i++)
  17. a[i] = sc.nextInt();
  18. ps.printf("%d elements are added.%n", size_of_a);
  19.  
  20. if (a[0] % 2 != 0) {
  21. ps.printf("First element of an array is odd.%n");
  22. for(int i = 0 ; i < size_of_a; i++) {
  23. if(i % 2 != 0) {
  24. b[j++] = a[i];
  25. size_of_b ++;
  26. }
  27. }
  28. } else if (a[0] % 2 == 0) {
  29. ps.printf("First element of an array is even.%n");
  30. for(int i = 0 ; i < size_of_a; i++) {
  31. if(i % 2 == 0) {
  32. b[j++] = a[i];
  33. size_of_b ++;
  34. }
  35. }
  36. }
  37. ps.printf("Size of b is %d%n", size_of_b);
  38.  
  39. // Calculating total of sample
  40. double total_of_sample = 0;
  41. for (int i = 0; i < b.length; i++)
  42. total_of_sample += b[i];
  43. ps.printf("Total of sample is %.3f%n", total_of_sample);
  44.  
  45. // Calculating mean of sample
  46. double mean_of_sample = 0;
  47. mean_of_sample = total_of_sample / size_of_b;
  48. ps.printf("Mean of sample is %.3f%n", mean_of_sample);
  49.  
  50. // Calculating sample variance
  51. double sample_variance = 0;
  52. double sample_variance_before_division = 0;
  53. for (int i = 0; i < size_of_b; i++)
  54. sample_variance_before_division += (b[i] - mean_of_sample) * (b[i] - mean_of_sample);
  55. sample_variance = sample_variance_before_division / (size_of_b - 1);
  56. ps.printf("Sample variance is %.3f%n", sample_variance);
  57.  
  58. // Calculating total of population
  59. double total_of_population = 0;
  60. for (int i = 0; i < a.length; i++)
  61. total_of_population += a[i];
  62. ps.printf("Total of population is %.3f%n", total_of_population);
  63.  
  64. // Calculating mean of population
  65. double mean_of_population = 0;
  66. mean_of_population = total_of_population / size_of_a;
  67. ps.printf("Mean of population is %.3f%n", mean_of_population);
  68.  
  69. // Calculating population variance
  70. double population_variance = 0;
  71. double population_variance_before_division = 0;
  72. for (int i = 0; i < size_of_a; i++)
  73. population_variance_before_division += (a[i] - mean_of_population) * (a[i] - mean_of_population);
  74. // Note, we do not need to subtract one from the size of a
  75. population_variance = population_variance_before_division / (size_of_a);
  76. ps.printf("Population variance is %.3f%n", population_variance);
  77.  
  78. // Calculating the difference between sample variance and population variance
  79. double diff = population_variance - sample_variance;
  80. ps.printf("Difference of variances is %.3f%n", diff);
  81.  
  82. // Printing the difference with 3 digit numbers
  83. ps.printf("%.3f%n", Math.abs(diff));
  84. sc.close();
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement