-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path7 HARD practice Problems in ARRAYS
117 lines (100 loc) · 3.35 KB
/
7 HARD practice Problems in ARRAYS
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
package com.company;
public class cwh_29_Practice_Set_6 {
public static void main(String[] args) {
// Practice Problem 1
/* float [] marks = {45.7f, 67.8f, 63.4f, 99.2f, 100.0f};
float sum = 0;
for(float element:marks){
sum = sum + element;
}
System.out.println("The value of sum is " + sum);
// Practice Problem 2
float [] marks = {45.7f, 67.8f, 63.4f, 99.2f, 100.0f};
float num = 45.57f;
boolean isInArray = false;
for(float element:marks){
if(num==element){
isInArray = true;
break;
}
}
if(isInArray){
System.out.println("The value is present in the array");
}
else{
System.out.println("The value is not present in the array");
}
// Practice Problem 3
float [] marks = {45.7f, 67.8f, 63.4f, 99.2f, 100.0f};
float sum = 0;
for(float element:marks){
sum = sum + element;
}
System.out.println("The value of average marks is " + sum/marks.length);
// Practice Problem 4
int [][] mat1 = {{1, 2, 3},
{4, 5, 6}};
int [][] mat2 = {{2, 6, 13},
{3, 7, 1}};
int [][] result = {{0, 0, 0},
{0, 0, 0}};
for (int i=0;i<mat1.length;i++){ // row number of times
for (int j=0;j<mat1[i].length;j++) { // column number of time
System.out.format(" Setting value for i=%d and j=%d\n", i, j);
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
// Printing the elements of a 2-D Array
for (int i=0;i<mat1.length;i++){ // row number of times
for (int j=0;j<mat1[i].length;j++) { // column number of time
System.out.print(result[i][j] + " ");
result[i][j] = mat1[i][j] + mat2[i][j];
}
System.out.println(""); // Prints a new line
}
// Practice Problem 5
int [] arr = {1, 21, 3, 4, 5, 34, 67};
int l = arr.length;
int n = Math.floorDiv(l, 2);
int temp;
for(int i=0; i<n; i++){
// Swap a[i] and a[l-1-i]
// a b temp
// |4| |3| ||
temp = arr[i];
arr[i] = arr[l-i-1];
arr[l-i-1] = temp;
}
for(int element: arr){
System.out.print(element + " ");
}
// Practice Problem 6
int [] arr = {1, 2100, 3, 455, 5, 34, 67};
int max = Integer.MIN_VALUE;
for(int e: arr){
if(e>max){
max = e;
}
}
System.out.println("the value of the maximum element in this array is: "+ max);
// Practice Problem 6
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
*/
// Practice Problem 7
boolean isSorted = true;
int [] arr = {1, 12, 3, 4, 5, 34, 67};
for(int i=0;i<arr.length-1;i++){
if(arr[i] > arr[i+1]){
isSorted = false;
break;
}
}
if(isSorted){
System.out.println("The Array is sorted");
}
else{
System.out.println("The Array is not sorted");
}
}
}