ISC computer practical 2019 solved Question 2
JAVA videos
Write a program to declare a single dimensional array al ] and a square matrix b[ ] of
size N. where >2 and N<10. Allow the user to input positive integers into the single dimensional array Perform the following tasks on the matrix:
(a) Sort the elements of the single dimensional array in ascending order using any standard sorting technique and display the sorted elements.
(b) Fill the square matrix b[ 1 in the following format.
If the array ( = {1, 2, 8, 1 ) then, after sorting a[ ] = {1,2,5,8)
Then, the matrix bu would fill as below:
1 2 5 8
1 2 5 1
1 2 1 2
1 1 2 5
Display the filled matrix in the above format.
Test your program for the following data and some random data:
isc computer practical 2019 solved
INPUT:
N=3
ENTER ELEMENTS OF SINGLE DIMENSIONAL ARRAY: 3 1 7
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
import java.util.*;
public class SortPattern
{
static int a[];
static int b[][];
static int len;
static void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array");
len=sc.nextInt();
a=new int[len];
b=new int [len][len];
System.out.println("Enter the numbers of the array");
for(int i=0;i<len;i++)
{
a[i]=sc.nextInt();
}
}
static void Sort()
{
int temp=0;
for(int i=0;i<len;i++)//This is just to sort
{
for(int j=1;j<(len-i);j++)
{
if(a[j-1]>a[j])
{
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
}
}
for(int i=0;i<len;i++)//giving values to matrix b
{
int temp2=0;
for(int j=0;j<len;j++)// #logic
{
if((i+j)>=len)
{
b[i][j]=a[temp2];
temp2++;
}
else
{
b[i][j]=a[j];
}
}
}
}
static void display()//display
{
System.out.println("The Sorted array is ");
for(int i=0;i<len;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
System.out.println("The array ");
for(int i=0;i<len;i++)
{
for(int j=0;j<len;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}
}
public static void main()
{
input();
Sort();
display();
}
}
