Computer Science
- Write a program to declare a square matrix A[ ][ ] of order N (N<20).
- Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix.
- Output the original matrix.
- Find the SADDLE POINT for the matrix. A saddle point is an element of the matrix such that it is the minimum element for the row and the maximum element for the column to which it belongs. Saddle point for a given matrix is always unique. If the matrix has no saddle point, output the message "NO SADDLE POINT".
- If the matrix has a saddle point element then sort the elements of the left diagonal in ascending order using insertion sort technique. All other elements should remain unchanged.
Test your program for the following data and some random data:
Sample data:
Input:
n = 4
Matrix A[ ][ ] | |||
2 | 5 | 6 | 9 |
8 | 4 | 12 | 3 |
6 | 7 | 3 | 1 |
12 | 24 | 2 | 11 |
Output:
Matrix A[ ][ ] | |||
2 | 5 | 6 | 9 |
8 | 4 | 12 | 3 |
6 | 7 | 3 | 1 |
12 | 24 | 2 | 11 |
No Saddle Point
Matrix after sorting the Principal diagonal:
2 | 5 | 6 | 9 |
8 | 3 | 12 | 3 |
6 | 7 | 4 | 1 |
12 | 24 | 2 | 11 |
Input:
n = 3
Matrix A[ ][ ] | |||
4 | 6 | 12 | |
2 | 8 | 14 | |
1 | 3 | 6 |
Output:
Matrix A[ ][ ] | |||
4 | 6 | 12 | |
2 | 8 | 14 | |
1 | 3 | 6 |
Saddle Point = 4
Matrix after sorting the Principal diagonal:
4 | 6 | 12 |
2 | 6 | 14 |
1 | 3 | 8 |
Answer
import java.util.Scanner;
public class KboatDDASaddlePoint
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of the matrix: ");
int n = in.nextInt();
if (n >= 20) {
System.out.print("Size is out of range");
return;
}
int a[][] = new int[n][n];
System.out.println("Enter elements of the matrix: ");
for (int i = 0; i < n; i++) {
System.out.println("Enter Row "+ (i+1) + " :");
for (int j = 0; j < n; j++) {
a[i][j] = in.nextInt();
if (a[i][j] < 0) {
System.out.print("Invalid Input");
return;
}
}
}
System.out.println("Matrix A[ ][ ]");
printMatrix(a, n, n);
//Find the Saddle Point
boolean found = false;
for (int i = 0; i < n; i++) {
int rMin = a[i][0];
int cIdx = 0;
for (int j = 1; j < n; j++) {
if (rMin > a[i][j]) {
rMin = a[i][j];
cIdx = j;
}
}
int k = 0;
for (k = 0; k < n; k++) {
if (rMin < a[k][cIdx]) {
break;
}
}
if (k == n) {
found = true;
System.out.println("Saddle Point = " + rMin);
break;
}
}
if (!found) {
System.out.println("No Saddle Point");
}
//Sort Left Diagonal with Insertion Sort
for (int i = 1; i < n; i++) {
int key = a[i][i];
int j = i - 1;
while (j >= 0 && a[j][j] > key) {
a[j + 1][j + 1] = a[j][j];
j--;
}
a[j + 1][j + 1] = key;
}
System.out.println("Matrix after sorting the Principal diagonal:");
printMatrix(a, n, n);
}
public static void printMatrix(int arr[][], int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
Output
Related Questions
Write a program in Java to create a 4 x 4 matrix. Now, swap the elements of 0th row with 3rd row correspondingly. Display the result after swapping.
Sample Input
55 33 26 14 81 86 31 10 58 64 17 12 22 14 23 25 Sample Output
22 14 23 25 81 86 31 10 58 64 17 12 55 33 26 14 Write a program to input N and M number of names in two different single dimensional arrays A and B respectively, such that none of them have duplicate names. Merge the arrays A and B into a single array C, such that the resulting array is sorted alphabetically. Display all the three arrays.
Test your program for the following data and some random data:
Sample data:
Input:
Enter the names in array A, N = 2
Enter the names in array B, M = 3
First array: A
Suman
Anil
Second array: B
Usha
Sachin
JohnOutput:
Sorted Merged array: C
Anil
John
Sachin
Suman
Usha
Sorted First array: A
Anil
Suman
Sorted Second array: B
John
Sachin
UshaNumbers have different representations depending on the bases on which they are expressed. For example, in base 3, the number 12 is written as 110 (1 x 32 + 1 x 31 + 0 x 30) but base 8 it is written as 14 (1 x 81 + 4 x 80).
Consider for example, the integers 12 and 5. Certainly these are not equal if base 10 is used for each. But suppose, 12 was a base 3 number and 5 was a base 6 number then, 12 base 3 = 1 x 31 + 2 x 30, or 5 base 6 or base 10 (5 in any base is equal to 5 base 10). So, 12 and 5 can be equal if you select the right bases for each of them.
Write a program to input two integers x and y and calculate the smallest base for x and smallest base for y (likely different from x) so that x and y represent the same value. The base associated with x and y will be between 1 and 20 (both inclusive). In representing these numbers, the digits 0 to 9 have their usual decimal interpretations. The upper case letters from A to J represent digits 10 to 19 respectively.
Test your program for the following data and some random data.
Sample Data
Input:
x=12, y=5Output:
12 (base 3)=5 (base 6)Input:
x=10, y=AOutput:
10 (base 10)=A (base 11)Input:
x=12, y=34Output:
~~12 (base 8) = 34 (base 2)~~
12 (base 17) = 34 (base 5)
[∵ 34 (base 2) is not valid as only 0 & 1 are allowed in base 2]Input:
x=123, y=456Output:
123 is not equal to 456 in any base between 2 to 20Input:
x=42, y=36Output:
42 (base 7) = 36 (base 8)Write a program in Java to store the elements in two different double dimensional arrays (in matrix form) A and B each of order 4 x 4. Find the product of both the matrices and store the result in matrix C. Display the elements of matrix C.
Note:
Two matrixes can be multiplied only if the number of columns of the first matrix must be equal to the number of rows of the second matrix.Sample Input: Matrix A
3 2 1 2 6 4 5 0 7 -1 0 2 4 3 1 1 Sample Input: Matrix B
-2 -4 -1 0 3 6 -5 2 5 3 4 6 0 -2 2 5 Sample Output: Matrix C
5 -1 -5 20 25 15 -6 38 -17 -38 2 -8 6 3 -13 17