-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHomework.java
More file actions
154 lines (127 loc) · 4.39 KB
/
Copy pathHomework.java
File metadata and controls
154 lines (127 loc) · 4.39 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.*;
public class Homework {
private static int arraySize;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the array length: ");
arraySize = scanner.nextInt();
int[] array = createRandomIntArray();
boolean isOver = false;
while (!isOver) {
// Displaying the menu
System.out.println ("\nMenu 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 ("\nChoose 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));
}
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;
}
//Method for finding the sum of elements with odd numbered indexes.
public static int findOddSum (int array[])
{
int sumOdd = 0;
if (arraySize > 1)
{
for (int i = 1; i < arraySize; i++ )
{
sumOdd += array[i];
i++;
}
}
return sumOdd;
}
//Method for finding the sum of elements with even numbered indexes.
public static int findEvenSum (int array[])
{
int sumEven = 0;
if (arraySize > 0)
{
for (int i = 0; i < arraySize; i++ )
{
sumEven += array[i];
i++;
}
}
return sumEven;
}
//Method for finding the average of the array and the difference of elements from the average.
public static double findAverage(int array[])
{
int result = 0;
for (int element : array)
{
result += element;
}
double average = (double)result / arraySize;
double [] diff = new double[arraySize];
System.out.print("Difference of each element from the average: ");
for (int i = 0; i < arraySize; i++)
{
diff[i] = array[i] - average;
if(i > 0)
{
System.out.print(", ");
}
System.out.printf("%.1f", diff[i]);
}
System.out.println();
return average;
}
}