-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh06_30_Practice11_Imp.java
More file actions
162 lines (138 loc) · 5.4 KB
/
Ch06_30_Practice11_Imp.java
File metadata and controls
162 lines (138 loc) · 5.4 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
155
156
157
158
159
160
161
public class Ch06_30_Practice11_Imp {
public static void main(String[] args) {
System.out.println(" ");
// Sum of array
float [] sumarr = {9.8f , 282.23f , 92.2f , 39.23f , 100.23f};
System.out.println("Sum of array values is: " + (sumarr[0]+sumarr[1]+sumarr[2]+sumarr[3]+sumarr[4]));
// Smart method
float sum = 0;
for (float i : sumarr){
sum = sum + i;
}
System.out.println("Sum of array values is: " + sum);
System.out.println(" ");
// Value checker in array
boolean isinarray = false;
for (float j : sumarr){
if (j == 91.38f) {
isinarray = true;
break;
}
}
if (isinarray){
System.out.println("Your array has the given value");
}
else {
System.out.println("Your array doesn't contain the given value");
}
System.out.println(" ");
// Average calculator of an array
int [] marks = {98 , 100 ,78 , 89 , 63};
int total = 0;
for (int i : marks){
total = total + i;
}
float average = (float) ((total)/(marks.length));
System.out.println("Average of your marks are: " + average);
System.out.println(" ");
// Addition of matrices
// Multidimensional arrays can be formed using multiple {}
int [][] matrice1 = {{20 , 50 , 60},
{58 , 29 , 70}};
int [][] matrice2 = {{80 ,40 ,16},
{20 , 50 ,85}};
int [][] result = {{0,0,0},
{0,0,0}};
System.out.println("Matrice 1 is");
for (int a = 0; a<matrice1.length; a++){
for (int b =0; b<matrice1[a].length; b++){
System.out.print(matrice1[a][b]);
System.out.print(" ");
}
System.out.println(" ");
}
System.out.println(" ");
System.out.println("Matrice 2 is");
for (int c = 0; c<matrice2.length; c++){
for (int d =0; d<matrice2[c].length; d++){
System.out.print(matrice2[c][d]);
System.out.print(" ");
}
System.out.println(" ");
}
for (int e = 0; e<matrice1.length; e++){
for (int f =0; f<matrice1[e].length; f++){
result[e][f] = matrice1[e][f] + matrice2[e][f];
}
}
System.out.println(" ");
System.out.println("Result is");
for (int g = 0; g<result.length; g++){
for (int h =0; h<result[g].length; h++){
System.out.print(result[g][h]);
System.out.print(" ");
}
System.out.println(" ");
}
System.out.println(" ");
// Printing array in reverse order
String [] grocery_list = { "Cold drink" , "Milk" , "Veggies" , "Ketchup" , "Bread"};
System.out.print("Array in reverse order is: {");
for (int p = grocery_list.length-1; p>=0 ; p--) {
System.out.print(grocery_list[p]);
if (p == 0){
break;
}
System.out.print(" , ");
}
System.out.print("}\n");
System.out.println(" ");
/* Doing the above problem with memory swapping
that is if an array has length(l) = 5 then 0 index is swapped by **(l-1-index) = 5-1-0 = 4th index */
// no of swapping to be done can be estimated by length/2. Example. for l = 5 swapping = 5/2 = 2.5 = 2
// can be achieved by using math module specifically , math.floordiv() (divides given number)
int [] array = {37, 48 , 78 , 23 ,99};
int xyz = Math.floorDiv(array.length , 2); // (numerator , denominator) //Determines number of swaps
int l = array.length;
int swap;
// now swapping grocery.length[i] with grocery.length[l-1-i] using swapping logic
for (int index = 0; index<xyz ; index++){
swap = array[index]; // swap = array[0] // Initialization step
array[index] = array[l-1-index]; // array[0] = array[4] //First swap
array[l-1-index] = swap; // array[4] = array[0] //Second swap
}
for (int e : array){
System.out.print(e + " , ");
}
System.out.println(" \n");
//Finding maximum value in an array
int max = Integer.MIN_VALUE;
for (int num : array){
if (num>max){
max = num;
}
}
System.out.println("Greatest number in array is: " + max);
System.out.println(" ");
// Finding minimum number in an array
// max is defined above
for (int num1 : array ){
if (num1 < max){
max = num1;
}
}
System.out.println("Smallest number in array is: " + max);
// Above questions can be solved by .MAX_VALUE and .MIN_VALUE by replacing int max = 0
// (denotes the least supported integers)
System.out.println(" ");
// Telling whether an array is sorted or not
int [] array1 = {1,2,1,4};
boolean sorted = true;
for (int sort = 0; sort<array1.length-1; sort++){
if (array1[sort] > array1[sort+1]){
sorted = false;
}
}
System.out.println("Array is sorted(except last value): " + sorted);
}
}