diff --git a/MatrixMultiplication.java b/MatrixMultiplication.java new file mode 100644 index 0000000..359f083 --- /dev/null +++ b/MatrixMultiplication.java @@ -0,0 +1,73 @@ +import java.io.*; + +class MatrixMultiplication { + + // Function to print Matrix + static void printMatrix(int M[][], + int rowSize, + int colSize) + { + for (int i = 0; i < rowSize; i++) { + for (int j = 0; j < colSize; j++) + System.out.print(M[i][j] + " "); + + System.out.println(); + } + } + + // Function to multiply + // two matrices A[][] and B[][] + static void multiplyMatrix( int row1, int col1, int A[][], int row2, int col2, int B[][]) + { + int i, j, k; + + // Print the matrices A and B + System.out.println("\nMatrix A:"); + printMatrix(A, row1, col1); + System.out.println("\nMatrix B:"); + printMatrix(B, row2, col2); + + // Check if multiplication is Possible + if (row2 != col1) { + + System.out.println( + "\nMultiplication Not Possible"); + return; + } + + // Matrix to store the result + // The product matrix will + // be of size row1 x col2 + int C[][] = new int[row1][col2]; + + // Multiply the two marices + for (i = 0; i < row1; i++) { + for (j = 0; j < col2; j++) { + for (k = 0; k < row2; k++) + C[i][j] += A[i][k] * B[k][j]; + } + } + + // Print the result + System.out.println("\nResultant Matrix:"); + printMatrix(C, row1, col2); + } + + // Driver code + public static void main(String[] args) { + + int row1 = 4, col1 = 3, row2 = 3, col2 = 4; + + int A[][] = { { 1, 1, 1 }, + { 2, 2, 2 }, + { 3, 3, 3 }, + { 4, 4, 4 } }; + + int B[][] = { { 1, 1, 1, 1 }, + { 2, 2, 2, 2 }, + { 3, 3, 3, 3 } }; + + multiplyMatrix(row1, col1, A, + row2, col2, B); + } +} \ No newline at end of file diff --git a/ScanSchedulingAlgo.java b/ScanSchedulingAlgo.java new file mode 100644 index 0000000..9fcaa86 --- /dev/null +++ b/ScanSchedulingAlgo.java @@ -0,0 +1,107 @@ +// SCAN Disk Scheduling algorithm +import java.util.*; + +class ScanSchedulingAlgo{ + +static int size = 8; +static int disk_size = 200; + +static void SCAN(int arr[], int head, String direction) +{ + int seek_count = 0; + int distance, cur_track; + Vector left = new Vector(), + right = new Vector(); + Vector seek_sequence = new Vector(); + + // appending end values + // which has to be visited + // before reversing the direction + if (direction == "left") + left.add(0); + else if (direction == "right") + right.add(disk_size - 1); + + for (int i = 0; i < size; i++) + { + if (arr[i] < head) + left.add(arr[i]); + if (arr[i] > head) + right.add(arr[i]); + } + + // sorting left and right vectors + Collections.sort(left); + Collections.sort(right); + + // run the while loop two times. + // one by one scanning right + // and left of the head + int run = 2; + while (run-- >0) + { + if (direction == "left") + { + for (int i = left.size() - 1; i >= 0; i--) + { + cur_track = left.get(i); + + // appending current track to seek sequence + seek_sequence.add(cur_track); + + // calculate absolute distance + distance = Math.abs(cur_track - head); + + // increase the total count + seek_count += distance; + + // accessed track is now the new head + head = cur_track; + } + direction = "right"; + } + else if (direction == "right") + { + for (int i = 0; i < right.size(); i++) + { + cur_track = right.get(i); + + // appending current track to seek sequence + seek_sequence.add(cur_track); + + // calculate absolute distance + distance = Math.abs(cur_track - head); + + // increase the total count + seek_count += distance; + + // accessed track is now new head + head = cur_track; + } + direction = "left"; + } + } + + System.out.print("Total number of seek operations = " + + seek_count + "\n"); + + System.out.print("Seek Sequence is" + "\n"); + + for (int i = 0; i < seek_sequence.size(); i++) + { + System.out.print(seek_sequence.get(i) + "\n"); + } +} + +// Driver code +public static void main(String[] args){ + + // request array + int arr[] = { 176, 79, 34, 60, + 92, 11, 41, 114 }; + int head = 50; + String direction = "left"; + + SCAN(arr, head, direction); +} +} \ No newline at end of file