This repository was archived by the owner on Oct 4, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathAssignment_2.java
More file actions
94 lines (72 loc) · 2.32 KB
/
Copy pathAssignment_2.java
File metadata and controls
94 lines (72 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
import java.util.Random;
public class Assignment_2 {
public static int findSum (int[] arr){
int sum = 0;
for (int num : arr) {
sum += num;
}
return sum;
}
public static double findAverage (int[] arr){
return (double)findSum(arr) / arr.length;
}
public static double[] diffFromAverage (int[] arr){
double[] diffArray = new double[arr.length];
double average = findAverage(arr);
for (int i = 0; i < arr.length; i++) {
diffArray[i] = arr[i] - average;
}
return diffArray;
}
public static int findMin(int[] arr){
int min = 101;
for(int i = 0; i < arr.length; i++){
if(arr[i] < min ){
min = arr[i];
}
}
return min ;
}
public static int findMax(int[] arr){
int max = -1;
for(int i = 0; i < arr.length; i++){
if(arr[i] > max ){
max = arr[i];
}
}
return max ;
}
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
Random random = new Random ();
boolean valid = false;
int userInput = 0;
System.out.println ("Please enter a positive integer for the size of the array: ");
//Checking for valid inputs for the size of the array
do {
if (!in.hasNextInt ()) {
System.out.println ("Invalid input, please enter a positive integer.");
}
else {
userInput = in.nextInt ();
if (userInput <= 0) {
System.out.println ("Invalid input, please enter a positive integer.");
}
else {
valid = true;
}
}
} while (!valid);
//Creating and displaying the array
int [] randomizedArray = new int [userInput];
for (int i = 0; i < userInput; i++) {
randomizedArray [i] = random.nextInt (101);
}
System.out.println ("The array containing random integers at first: ");
for (int i = 0; i < randomizedArray.length; i++) {
System.out.print (randomizedArray[i] + " ");
}
System.out.println ();
}
}