-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayOperations
111 lines (84 loc) · 2.58 KB
/
ArrayOperations
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
package arrayoperations;
import java.util.Random;
public class ArrayOperations {
public static void main(String[] args) {
// declares array as random
Random rng = new Random();
int[] array = new int[10000];
// For loop for random 0 to 200
for (int i = 0; i < array.length; i++)
{
array[i] = rng.nextInt(200);
}
// sum of the array
int sum1 = 0;
for (int i = 0; i < array.length; i++)
{
sum1 += array[i];
}
System.out.println("The Sum is: " + sum1);
// average sum of the array
int sum2 = 0;
for (int i = 0; i < array.length; i++)
{
sum2 += array[i];
}
double average = sum2/array.length;
System.out.println("The average is: " + average);
// min value
int min1 = array[0];
int indexMin = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] < min1)
{
min1 = array[i];
indexMin = i;
}
}
System.out.println("The minimum value is " + min1 + " and index is " + indexMin);
// max value
int max2 = array[0];
int indexMax = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] > max2)
{
max2 = array[i];
indexMax = i;
}
}
System.out.println("The maximum value is " + max2 + " and index is " + indexMax);
// even and odd numbers for if
int oddnum = 0;
int evennum = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] % 2 == 0)
{
evennum++;
}
else
{
oddnum++;
}
}
System.out.println("The array contains " + evennum + " even numbers and " + oddnum +" odd numbers");
// sum of even and odd numbers
int sumeven = 0;
int sumodd = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] % 2 == 0)
{
sumeven += array[i];
}
else
{
sumodd += array[i];
}
}
System.out.println("The sum of the even numbers: " + sumeven);
System.out.println("The sum of the odd numbers: " + sumodd);
}
}