Skip to content

Commit

Permalink
Some basic Array Operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypatel208 committed Sep 17, 2023
1 parent 410131e commit 761e185
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import java.util.Arrays;

public class Array {

public static void findMaxOfArray() {
int[] a = {15, 29, 38, 14, 56, 6, 7};
double max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
System.out.println("Maximum element in array is: " + max);
}

public static void averageOfArray() {
int[] a = {15, 29, 38, 14, 56, 6, 7};
int N = a.length;
double sum = 0.0;
for (int i = 0; i < N; i++) {
sum += a[i];
}
double average = sum / N;

System.out.println("Average of given array is: " + average);
}

public static void copyToAnotherArray() {
int[] a = {15, 29, 38, 14, 56, 6, 7};
int N = a.length;
int[] b = new int[N];

for (int i = 0; i < N; i++) {
b[i] = a[i];
}

System.out.println(Arrays.toString(b));
}

public static void reverseTheArray() {
int[] a = {15, 29, 38, 14, 56, 6, 7};
int N = a.length;

for (int i = 0; i < N / 2; i++) {
int temp = a[i];
a[i] = a[N - 1 - i];
a[N - 1 - i] = temp;
}

System.out.println("Reversed elements of an array is: " + Arrays.toString(a));
}

public static void matrixMatrixMultiplication() {
int[][] a = {{15, 29, 38}, {14, 56, 6}};
int[][] b = {{7, 6, 56}, {14, 38, 29}};

int N = a.length;

int[][] c = new int[N][N];

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
print2DArray(c);
}

public static void print2DArray(int[][] array) {
for (int i=0;i<array.length;i++){
for (int j=0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}

public static void main(String[] args) {
findMaxOfArray();
averageOfArray();
copyToAnotherArray();
reverseTheArray();
matrixMatrixMultiplication();
}
}

0 comments on commit 761e185

Please sign in to comment.