forked from atestulumen/Assignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework.java
More file actions
91 lines (75 loc) · 2.86 KB
/
Copy pathHomework.java
File metadata and controls
91 lines (75 loc) · 2.86 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
import java.util.*;
public class Homework {
private static int arraySize = 100;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] array = createRandomIntArray();
boolean isOver = false;
while (!isOver) {
// Displaying the menu
System.out.println ("Menu Options:");
System.out.println ("1. Find the minimum and maximum of the array");
System.out.println ("2. Find the average of the array and the differences from the average");
System.out.println ("3. Find the sum of elements with odd and even indexes");
System.out.println ("4. Exit");
System.out.print ("Choose Operation: ");
int operation = scanner.nextInt();
if (operation == 1) {
System.out.println ("The minimum of the array is " + findMin(array));
System.out.println ("The maximum of the array is " + findMax(array));
}
else if (operation == 2) {
System.out.println ("The average of the array is " + findAverage(array));
// Diff part here
}
else if (operation == 3) {
System.out.println ("The sum of the elements with odd indexes is " + findOddSum(array));
System.out.println ("The sum of the elements with even indexes is " + findEvenSum(array));
}
else if (operation == 4) {
isOver = true;
System.out.println ("Exiting...");
}
else {
System.out.println ("Invalid operation. Please try again.");
}
}
// Closing Scanner
scanner.close();
}
// Method that creates an int array of a given number of int's randomly selected from the [0,100] range.
public static int[] createRandomIntArray()
{
int[] arr = new int[arraySize];
Random random = new Random();
for (int i = 0; i < arraySize; i++)
{
arr[i] = random.nextInt(101);
}
return arr;
}
//Method for finding the minimum of the array
public static int findMin (int array[])
{
int minimum = array[0];
//Changing the minimum if there is a smaller element than the current minimum
for (int i = 1 ; i < arraySize ; i++)
{
if (array[i] < minimum)
minimum = array[i];
}
return minimum;
}
//Method for finding the maximum of the array
public static int findMax (int array[])
{
int maximum = array[0];
//Changing the maximum if there is a larger element than the current maximum
for (int i = 1 ; i < arraySize ; i++)
{
if (array[i] > maximum)
maximum = array[i];
}
return maximum;
}
}