You are on page 1of 30

Question 1:

Write a program to declare a square matrix M [ ] [ ] of order ‘N’


where ‘N’ must be greater than 3 and less than 10. Allow the
user to accept three different characters from the keyboard and
fill the array according to the instruction given below:
(i) Fill the four corners of the square matrix by character 1.
(ii) Fill the boundary elements of the matrix (except the four
corners) by character 2.
(iii) Fill the non-boundary elements of the matrix by character 3.
Test your program with the following data and some random
data:
Example 1:
INPUT: N = 4
FIRST CHARACTER: @
SECOND CHARACTER: ?
THIRD CHARACTER: #
OUTPUT: @ ? ? @
? # # ?
? # # ?
@ ? ? @
Example 2:
INPUT: N = 5
FIRST CHARACTER: A
SECOND CHARACTER: C
THIRD CHARACTER: X
OUTPUT: ACCCA
CXXXC
CXXXC
CXXXC
ACCCA
Example 3:
INPUT: N = 12
OUTPUT: SIZE OUT OF RANGE

Algorithm:
1. Start algorithm.
2. Input the number of lines.
3. Check if the number of lines are in the range then goto step 4
else print “INVALID INPUT” and goto step 10.
4. Input the first, second and third characters.
5. Create a double dimensional array.
6. Now place the first character at the four corners.
7. Now fill the second character at the four boundary lines leaving
the corners.
8. Now fill the third character at the non-boundary positions.
9. Print the double dimensional array.
10. End algorithm.
Program:
import java.util.*;
class CharacterMatrix
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter value of n");
int n=sc.nextInt();
if(n<3 || n>10)
{
System.out.println("Invalid input");
return;
}
char ar[][]=new char[n][n];
System.out.println("Enter first character");
char c1=sc.next().charAt(0);
System.out.println("Enter second character");
char c2=sc.next().charAt(0);
System.out.println("Enter third character");
char c3=sc.next().charAt(0);
for(int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
if(x==0 || y==0 || x==n-1 || y==n-1)
{
if(x==y || x+y==n-1)
{
ar[x][y]=c1;
}
else
{
ar[x][y]=c2;
}
}
else
{
ar[x][y]=c3;
}
}
}

for(int x=0;x<n;x++)
{
for(int y=0;y<n;y++)
{
System.out.print(ar[x][y]+" ");
}
System.out.println();
}
}
}
Question 2:
Write a program to input and store n integers (n > 0) in a
single subscripted variable and print each number with its
frequency. The output should contain number and its
frequency in two different columns.

Sample Input:
12 20 14 12 12 20 16 16 14 14 12 20 18 18

Sample Output:
Number Frequency
12 4
14 3
16 2
18 2
20 3

Algorithm:
1. Start algorithm.
2. Declare and initialize an array arr.
3. Declare another array ar with the same size of array arr. It is used
to store the frequencies of elements present in the array.
4. Variable visited will be initialized with the value -1. It is required
to mark an element visited that is, it helps us to avoid counting
the same element again.
5. The frequency of an element can be counted using two loops. One
loop will be used to select an element from an array, and another
loop will be used to compare the selected element with the rest of
the array.
6. Initialize count to 1 in the first loop to maintain a count of each
element. Increment its value by 1 if a duplicate element is found
in the second loop. Since we have counted this element and didn't
want to count it again. Mark this element as visited by setting fr[j]
= visited. Store count of each element to fr.
7. Finally, print out the element along with its frequency
8. End algorithm.

Program:
import java.util.Scanner;
public class Frequency
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();
if (n <= 0) {
System.out.println("Invalid Input! n should be greater than
0.");
return;
}
int arr[] = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
//Sort the array
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}
System.out.println("Number\tFrequency");
int count = 0;
for (int i = 0; i < n - 1; i++) {
count++;
if (arr[i] != arr[i + 1]) {
System.out.println(arr[i] + "\t" + count);
count = 0;
}
}
//Print frequency of last element
count++;
System.out.println(arr[n - 1] + "\t" + count);
}}
Question 3:
Write a program to accept a set of n integers (where n > 0) in a
single dimensional array. Arrange the elements of the array such
that the lowest number appears in the centre of the array, next
lower number in the right cell of the centre, next lower in the left
cell of the centre and so on... . The process will stop when the
highest number will set in its appropriate cell. Finally, display
the array elements.

Assume that the memory space is less. Hence, you don't need to
create extra array for the aforesaid task.

Example:

Input: 1 2 3 4 5
Output: 5 3 1 2 4

Input: 11 12 31 14 5
Output: 31 12 5 11 14
Algorithm:
1. Start algorithm.
2. Sort the given array.
3. Move all the odd position element in the right side of the array.
4. Reverse the element from 0 to (n-1)/2 position of the array.
5. End algorithm.
Program:
import java.util.Scanner;

public class Arrange


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter n: ");
int n = in.nextInt();

if (n <= 0) {
System.out.println("Invalid Input! n should be greater than
0.");
return;
}

int arr[] = new int[n];


System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}

//Step 1: Sort the array


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = t;
}
}
}

//Step 2: Get elements at odd indexes to the right


int endIdx = n - 1;
int startIdx;

if (n % 2 == 0) {
startIdx = n - 1;
}
else {
startIdx = n - 2;
}

while (startIdx > 0) {


int t = arr[startIdx];
int idx = startIdx;

while (idx != endIdx) {


arr[idx] = arr[idx + 1];
idx++;
}

arr[idx] = t;
startIdx -= 2;
endIdx -= 1;
}

//Step 3: Reverse the sub-array from 0 to (n-1) / 2


for (int i = 0, j = (n - 1) / 2; i < j; i++, j--) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}

//Print the final array


for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}}
Question 4:
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:

i. Output the original matrix.

ii. Find the SADDLE POINT for the matrix.

A saddle point is an element of the matrix such that it is minimum


element for the row to which it belongs 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”.

iii. Sort the element along principal diagonal in ascending order


using insertion sort technique.

All other elements should remain unchanged. Test your program


for the following data:

Entered Matrix:
2 5 6 9
8 4 12 3
5 7 3 1
12 24 2 11
NO SADDLE POINT
Matrix after sorting principal diagonal:

2 5 6 9
8 3 12 3
5 7 4 1
12 24 2 11

Entered Matrix:
4 16 12
2 8 14
1 3 6
Saddle Point=4
Matrix after sorting principal diagonal:
4 16 12
2 6 14
1 3 8

Algorithm:
Algorithm for main(String[]args) method:
1. Start algorithm.
2. Call the function inputElements() to input the array.
3. Then display the matrix using the function display().
4. Now call the function findSaddlePoint() to calculate the
saddle point of the matrix.
5. Now Sort the matrix using the function
principalDiagonalSort().
6. End algorithm.
Algorithm for PrincipalDiagonalSort() method:
1. Start algorithm.
2. Sort the matrix by the principal diagonal.
3. Now display the matrix using the function display().
4. End algorithm.
Algorithm for findSaddlePoint() method:
1. Start algorithm.
2. Traverse each row of the matrix one by one and find the
minimum elements from each row.
3. Traverse the same column for finding the maximum element
in which the minimum element is found.
4. If the minimum element in the row and maximum element
in the column are the same, the element is the saddle point
of the matrix.
5. If the minimum element in the row and maximum element
in the column are not the same, there does not exist a saddle
point.
6. Repeat the above steps until we do not get the saddle point.
7. End algorithm.
Algorithm for display() method:
1. Start algorithm.
2. Print the matrix.
3. End algorithm.
Algorithm for inputElements() method:
1. Start algorithm.
2. Enter the numbers of the matrix.
3. Store the numbers in an array.
4. End algorithm.

Program:
import java.util.*;
class SaddlePoint
{
private boolean flag;
private int MAX_MATRIX;
private int[][] mat;
private int order;
private int max;
private int min;
private int col;
public static void main(String[] args)
{
int N;
Scanner in = new Scanner(System.in);
do
{
System.out.println("Enter the order of the square matrix:");
N = in.nextInt();
if(N>=20)
System.out.println("INVALID INPUT,TRY AGAIN");
}while(N>=20);
SaddlePoint obj = new SaddlePoint(N);
obj.inputElements();
System.out.println("Entered Matrix:");
obj.display();
obj.findSaddlePoint();
obj.principalDiagonalSort();
in.close();
}

public void principalDiagonalSort()


{
for (int i = 1; i < order; ++i)
{ // using insertion sort
int key = mat[i][i];
int j = i - 1;
while (j >= 0 && mat[j][j] > key)
{
mat[j + 1][j+1] = mat[j][j];
j = j - 1;
}
mat[j + 1][j+1] = key;
}
System.out.println("Matrix after sorting principal diagonal:");
display();
}

public void findSaddlePoint()


{
max=0;
for(int i=0;i<order;i++)
{
min=mat[i][0];
for(int j=0;j<order;j++)
{
if(mat[i][j]<min)
{
min=mat[i][j];
col = j;
}
} // end of for loop-j
for(int k=0;k<order;k++)
if(mat[k][col]>max)
{
max=mat[k][col];
} // end of for loop-k
if(max==min)
{
System.out.println("Saddle Point="+max);
flag=true;
}
} //end of for loop-i
if(flag==false)
System.out.println("NO SADDLE POINT");
}

public void display()


{
for(int p=0;p<order;p++)
for(int q=0;q<order;q++)
if(MAX_MATRIX < mat[p][q])
MAX_MATRIX = mat[p][q];
String s,element;
s=Integer.toString(MAX_MATRIX);
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
element=Integer.toString(mat[i][j]); // for formatted output
int tmp=element.length();
while(tmp !=s.length())
{
element +=" ";
tmp++;
}
System.out.print(element+" ");
}
System.out.println();
}
}

public void inputElements()


{
Scanner in = new Scanner(System.in);
System.out.println("Enter the values of the matrix:");
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
mat[i][j]=in.nextInt();
if(mat[i][j]<=0)
{
System.out.println("Enter only positive values,try
again:");
j--;
}
}
}
in.close();
}

SaddlePoint(int N)
{
order = N;
mat = new int[order][order];
MAX_MATRIX =0;
flag=false;
}

}
Question 5:
Write a program to declare a single-dimensional array a[] and a
square matrix b[][] of size N, where N > 2 and N < 10. Allow the
user to input positive integers into the single dimensional array.
Perform the following tasks on the matrix:
1. Sort the elements of the single-dimensional array in
ascending order using any standard sorting technique
and display the sorted elements.
2. Fill the square matrix b[][] in the following format:
If the array a[] = {5, 2, 8, 1} then, after sorting a[] = {1,
2, 5, 8}
Then, the matrix b[][] would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
3. Display the filled matrix in the above format.
Test your program for the following data and some random
data:
Example 1
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3
17
OUTPUT:
SORTED ARRAY: 1 3 7
FILLED MATRIX
1 3 7
1 3 1
1 1 3
Example 2
INPUT:
N = 13
OUTPUT:
MATRIX SIZE OUT OF RANGE
Example 3
INPUT:
N=5
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY:
10 2 5 23 6
OUTPUT:
SORTED ARRAY: 2 5 6 10 23
FILLED MATRIX
2 5 6 10 23
2 5 6 10 2
2 5 6 2 5
2 5 2 5 6
2 2 5 6 10
Algorithm:
 Start algorithm.
 Input capacity of array in a variable
 Define two array (1D and 2D) with the entered capacity
 Store elements in 1D array
 Display the unsorted array
 Now using any sorting technique sort the array
 Now using pattern program logic store the 1st pattern in 2D
array
 After storing 1st pattern using logic of 2nd pattern, store the
2nd pattern in 2D array
 Display the sorted 1D array
 Display required 2D array.
 Compile and run the program.
 End algorithm.

Program:
import java.util.*;
class DDArray
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int n,i,j,x,l,m,temp;
System.out.println("Enter Array Capacity");
n= in.nextInt(); // Input Array Capacity
int a[]= new int[n]; // Single Dimensional Array
int b[][]= new int[n][n]; // Double Dimension Array
System.out.println("Enter Array Elements");
for(i=0;i<n;i++)
{
a[i]= in.nextInt(); // Storing numbers in array
}
System.out.println("Original 1D Array");
for(i=0;i<n;i++)
{
System.out.print(a[i]+"\t"); // Displaying unsorted array
}
System.out.println();
for(i=0;i<n-1;i++)
{
for(j=0;j<((n-1)-i);j++)
{
if(a[j]>a[j+1]) // Sorting array
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
x=0; // variable used for storing elements in array.
for(i=n;i>=1;i--) // 1st pattern condition
{
for(j=1;j<=i;j++)
{
b[x][j-1]= a[j-1]; // storing first pattern elements in 2D
array
}
x++;
}
m=1;
l=n;
for(i=1;i<n;i++) // 2nd pattern condition
{
for(j=1;j<=i;j++)
{
b[m][l-1]= a[j-1]; // storing 2nd pattern in 2D array
l++;
}
m++;
l=l-m;
}
System.out.println("Sorted 1D Array");
for(i=0;i<n;i++)
{
System.out.print(a[i]+"\t"); // displaying sorted array
}
System.out.println();
System.out.println("2D Array");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(b[i][j]+"\t"); // display double
dimensional matrix
}
System.out.println("\t");
}
} // end of main method
} // end of class

You might also like