Showing posts with label 2 D Numeric Array. Show all posts
Showing posts with label 2 D Numeric Array. Show all posts

Monday, December 16, 2024

BlueJ Program Upper Triangular Matrix Checking

What is upper triangular matrix?

A matrix where all elements below the left diagonal are zero is called upper triangular matrix.

 import java.util.*;
class UTMatrix
{   
    int arr[][];
    int i,j,c,n;
    Scanner sc=new Scanner(System.in);
    public void show() 
    {
       System.out.print("\nEnter the size of array:");
       n=sc.nextInt();
       arr=new int[n][n];
     for(i=0;i<n;i++)
     {
         for(j=0;j<n;j++)
     {
              System.out.print("\nValue:");
              arr[i][j]=sc.nextInt();
     }
    }
     
     System.out.print("\nMatrix Values\n");
     for(i=0;i<n;i++)
     {
         for(j=0;j<n;j++)
     {
        System.out.print(" "+arr[i][j]);
    }
    System.out.println();
}
          for(i=0;i<n;i++)
     {
         for(j=0;j<n;j++)
     {
         if(arr[i][j]!=0 && j<i)
        {
         c=1;
         break;
        }
    } 
    } 
        if(c==0)
        System.out.print("\nThis is an upper traingular Matrix");
        else
        System.out.print("\nThis is not an upper traingular Matrix");
       } 
    }
         

Tuesday, December 10, 2024

BlueJ Program To Check For 2-D Array With Binary Values

What is array with binary values? If an array contains only 0 and 1, it is called binary array.

 import java.util.*;
class Arr
{   
    int arr[][]=new int [3][4];
    int i,j,c;
    Scanner sc=new Scanner(System.in);
    public void show() 
    {
        c=0;
     for(i=0;i<3;i++)
     {
         for(j=0;j<4;j++)
     {
              System.out.print("\nValue:");
              arr[i][j]=sc.nextInt();
     }
    }
     
     System.out.print("\nValues=");
     for(i=0;i<3;i++)
     {
         for(j=0;j<4;j++)
     {
        System.out.print(" "+arr[i][j]);
    }
    System.out.println();
}
          for(i=0;i<3;i++)
     {
         for(j=0;j<4;j++)
     {
         if(arr[i][j]!=0 || arr[i][j]!=1)
        {
         c=1;
         break;
        }
    } 
    } 
        if(c==0)
        System.out.print("\nArray with binary values");
        else
        System.out.print("\nArray with not binary values");
       } 
    }
         

Tuesday, November 5, 2024

BlueJ Program On 2 D Array and Sorting


Details of The Program: CLICK HERE

import java.io.*; 
public class P 
{ 
void sort(int arr[])          //Taking single dimension array as parameter 
{

int i, j, size = arr.length, temp,flag;

/*'size' holds the number of values in the array */ 
for(i=0;i 
{

flag=0; 
/*Sorting using bubble sort technique, so if there is no exchange of values in one pass there is no need to run the loop */ 
for(j=0;j 
{ 
if(arr[j]>arr[j+1])

{
flag=1; temp = arr[j]; 
arr[j] = arr[j+1]; 
arr[j+1] = temp; 
}

} 
if(flag==0)

break;
}

}

void show(int arr[][]) /*Taking 2D array as parameter*/ 
{

int i, j; for(i=0;i

{

for(j=0;j 
/* arr[i] gives one row at a time. arr[i].length gives the number of values in each row */ 
{ 
System.out.print(arr[i][j]+"\t"); 
}

System.out.println();

} 
}
 void sort2D(int arr[][]) 
{ 

/* In java, array is an object, so here the function calling technique is call be reference means any changes made to the formal parameter in this function will be reflected in the original parameter.*/


int i, j; 
for(i=0;i 
{ 
sort(arr[i]); 
} 
}
public static void main(String args[])throws Exception 
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

P obj = new P(); int arr[][], m, n, i, j; 
System.out.println("Enter the number of rows: "); 
m = 
Integer.parseInt(br.readLine()); 
System.out.println("Enter the number of columns: ");
n = Integer.parseInt(br.readLine()); 
if(m< 2 && m >9||n >9 && n< 2) 
{

System.out.println("Matrix out of range"); System.exit(0);

} 
arr = new int[m][n]; 
for(i=0;i< 
{
System.out.println("Enter values for row no "+(i+1) + ":"); for(j=0;j
{

System.out.print("\nColumn No " + (j+1) + ":"); arr[i][j] = Integer.parseInt(br.readLine()); 
} 
}
System.out.println("Original Matrix: ");
obj.show(arr); 
obj.sort2D(arr);

System.out.println("Matrix after sorting rows: "); obj.show(arr);
}
}

Variable Description

Type
Name
Use
BufferedReader
br
Intakes limit from user
int
arr
2 D array to hold the values
Int
m
Holds no of rows in the array
int
n
Holds no of columns in the


array

int
I,j
Loop Control Variable
int
size
Holds the no of elements in


each row the array
Algorithm




Step 1: Create BufferedReader object ‘br’ and int type variables ‘n’ and 'm' to store the column and row size of the 2 D array.

Step 2: Values of ‘n’ and 'm' are checked for validity and if it is invalid, terminate the program otherwise create the 2 D array 'arr'.

Step 3: Stores the values in the two dimensional array 'arr'.

Step 4: Display the original array


Step 5: Pass the two dimensional array to function 'sort2D()' which sends each rows of the 2 d array to 'sort()' function which sorts the elements bubble sort technique.


Back To 2018 Computer Practical Paper: CLICK HERE

Sunday, December 10, 2023

2 D Array To Store Only Composite Numbers - BlueJ Program

 A class Composite contains a two-dimensional array of order [m x n]. The maximum values possible for both ‘m’ and ‘n’ is 20. Design a class Composite to fill the array with the first (m x n) composite numbers in column wise. [Composite numbers are those which have more than two factors.]The details of the members of the class are given below:

Class name: Composite

Data members/instance variables :arr[ ] [ ] :integer array to store the composite numbers column wise

 m        :integer to store the number of rows 

n:integer to store the number of columns 

Member functions/methods:

Composite(int mm, int nn ):to initialize the size of the matrix, m=mm and n=nn

 int isComposite( int p ):to return 1 if the number is composite otherwise returns 0

void fill ( ):to fill the elements of the array with the first (m × n) composite numbers in column wise

void display( ):to display the array in a matrix form Specify     the     class Composite giving     details of     the constructor(int,int), int isComposite(int), void fill( ) and void display( ). Define a main( ) function to create an object and call all the functions accordingly to enable the task.


import java.util.*;
class Composite
{
int m,n,arr[][];
Scanner sc=new Scanner(System.in);
Composite(int mm, int nn)
{
    m=mm;
    n=nn;
    arr=new int [m][n];
}
private int isComposite( int p )
{
   int i;
   for(i=2;i<p;i++)
   {
        if(p%i==0)
        break;
   }
   if(i==p)
   return 0;
   else
   return 1;
}
private void display()
{
   int i,j;
   for(i=0;i<m;i++)
   {
        for(j=0;j<n;j++)
        {
            System.out.print(" "+arr[i][j]);
        }
        System.out.println();
   }
}
void fill ( )
{
    int i,j;
   for(i=0;i<m;i++)
   {
        for(j=0;j<n;j++)
        {
            System.out.print("\nEnter Number:");
            int d=sc.nextInt();
            if(isComposite(d)==1)
            arr[i][j]=d;
            else
            {
                System.out.print("\nEnter Composite Number "+d + "  is not composite");
                j--;
            }   
        }
    }
}
public static void main(String args[])
{
    int m,n;
    Scanner sc=new Scanner(System.in);
    while(true)
    {
        System.out.print("\nEnter Row Number:");
        m=sc.nextInt();
        if(m>0 && m<=20)
        break;
    }
    while(true)
    {
        System.out.print("\nEnter Column Number:");
        n=sc.nextInt();
        if(n>0 && n<=20)
        break;
    }
Composite ob=new Composite(m,n);
ob.fill();
System.out.print("\nFinal Matrix\n");
ob.display();
}
}   
    
         
    
      

Monday, January 16, 2023

Sorting boundary elements of a 2 d array display the boundary elements along with sum

 


Using Scanner Class

import java.util.*;
public class Main
{

int x,t,r,c,i,j,n,m;
int a[][],b[];
void show()
{
t=1;
Scanner br=new Scanner(System.in);
c=0;
while(true)
{

System.out.println("enter the number of rows:");
n=br.nextInt();
if(n<2 || n>20)
continue;
System.out.println("enter the number of columns:");
m=br.nextInt();
if(m<2 || m>20)
continue;
else
break;
}

a=new int[n][m];
b=new int[2*(m+n)];
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.println("enter value:");
a[i][j]=br.nextInt();
}
}
System.out.println("\nEntered values are\n");
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println("\n\n");
for(i=0;i< m;i++)
{
b[c++]=a[0][i];
}
for(i=1;i<=n-1;i++)
{
b[c++]=a[i][m-1];
}
for(i=m-2;i >=0;i--)
{
b[c++]=a[n-1][i];
}
for(i=n-2;i >0;i--)
{
b[c++]=a[i][0];
}

bsort();
r=0;
c=-1;
t=0;
for(i=1;i<=m;i++)
{
a[r][++c]=b[t++];
}
for(i=1;i<=n-1;i++)
{
a[++r][c]=b[t++];
}
for(i=1;i<=m-1;i++)
{
a[r][--c]=b[t++];
}
for(i=1;i<=n-2;i++)
{
a[--r][c]=b[t++];
}
System.out.println("\nAfter sorting the matrix\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  System.out.print(" "+a[i][j]);
  }
  System.out.println();
 }
 x=0;
 System.out.println("\nAfter sorting the boundary values\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  if(i==0 || i==n-1 ||  j==0 ||  j==m-1)
{
  System.out.print(" "+a[i][j]);
  x=x+a[i][j];
}
  else
  System.out.print(" ");
  }
  System.out.println();
 }
System.out.println("\nSum of the boundary elements "+x);
}
private void bsort()
{
 int flag;
 for(i=0;i< c;i++)
 {
  flag=0;
  for(j=0;j< c-i-1;j++)
  {
   if(b[j] >b[j+1])
   {
    flag=1;
    t=b[j];
    b[j]=b[j+1];
    b[j+1]=t;
    }
   }
   if(flag==0)
   break;
  }
  }
public static void main(String args[])throws Exception
{
Main ob=new Main();
ob.show();
}
}


Using BufferedReader Class

import java.io.*;
public class Boundary
{
int x,t,r,c,i,j,n,m;
int a[][],b[];
void show()throws IOException
{
t=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
c=0;
while(true)
{
System.out.println("enter the number of rows:");
n=Integer.parseInt(br.readLine());
if(n<2 || n>20)
continue;
System.out.println("enter the number of columns:");
m=Integer.parseInt(br.readLine());
if(m<2 || m>20)
continue;
else
break;
}
a=new int[n][m];
b=new int[2*(m+n)];
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.println("enter value:");
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("\nEntered values are\n");
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println("\n\n");
for(i=0;i< m;i++)
{
b[c++]=a[0][i];
}
for(i=1;i<=n-1;i++)
{
b[c++]=a[i][m-1];
}
for(i=m-2;i >=0;i--)
{
b[c++]=a[n-1][i];
}
for(i=n-2;i >0;i--)
{
b[c++]=a[i][0];
}
bsort();
r=0;
c=-1;
t=0;
for(i=1;i<=m;i++)
{
a[r][++c]=b[t++];
}
for(i=1;i<=n-1;i++)
{
a[++r][c]=b[t++];
}
for(i=1;i<=m-1;i++)
{
a[r][--c]=b[t++];
}
for(i=1;i<=n-2;i++)
{
a[--r][c]=b[t++];
}
System.out.println("\nAfter sorting the matrix\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  System.out.print(" "+a[i][j]);
  }
  System.out.println();
 }
 x=0;
 System.out.println("\nAfter sorting the boundary values\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  if(i==0 || i==n-1 ||  j==0 ||  j==m-1)
{
  System.out.print(" "+a[i][j]);
  x=x+a[i][j];
}
  else
  System.out.print(" ");
  }
  System.out.println();
 }
System.out.println("\nSum of the boundary elements "+x);
}
private void bsort()
{
 int flag;
 for(i=0;i< c;i++)
 {
  flag=0;
  for(j=0;j< c-i-1;j++)
  {
   if(b[j] >b[j+1])
   {
    flag=1;
    t=b[j];
    b[j]=b[j+1];
    b[j+1]=t;
    }
   }
   if(flag==0)
   break;
  }
  }
public static void main(String args[])throws Exception
{
Boundary ob=new Boundary();
ob.show();
}
}



Sample Input & Output

enter the number of rows:
3
enter the number of columns:
4
enter value:
5
enter value:
6
enter value:
7
enter value:
55
enter value:
4
enter value:
22
enter value:
1
enter value:
34
enter value:
5
enter value:
4
enter value:
34
enter value:
3

Entered values are

 5 6 7 55
 4 22 1 34
 5 4 34 3




After sorting the matrix

 3 4 4 5
 55 22 1 5
 34 34 7 6

After sorting the boundary values

 3 4 4 5
 55   5
 34 34 7 6

Sum of the boundary elements 157

Tuesday, October 18, 2022

Fill 2-D Array with All Prime Numbers

 In this program a two dimensional array will be filled up with only prime numbers and the numbers will be generated using a function.

public class Main
{
int arr[][]=new int[4][3];
int i,j,x;
void take()
{
   x=0;
   for(i=0;i<4;i++)
    {
       for(j=0;j<3;j++)
       {
          
          x=prime(x) ;
       arr[i][j]=x;
        }
    }
        System.out.print("\nThe matrix with all prime numbers \n ");
       
        for(i=0;i<4;i++)
    {
       for(j=0;j<3;j++)
       {
          
         System.out.print(arr[i][j]+ " ");
      
        }
        System.out.println();
    }
}
int prime(int x)
{
   
    int i;
    x++;
    while(true)
    {
        for(i=2;i<x;i++)
        {
             if(x%i==0)
             break;
        }
        if(i<x)
        x++;
        else
        break;
    }
    return x;
}
       
        
        public static void main(String[] args)
 {
Main ob=new Main();
ob.take();
}
}

Saturday, September 11, 2021

Addition of each row and each column in 2d array

 
import java.util.*;
class Even_series
{
   int arr[][]=new int [5][5];
   int i,j, sum_row, sum_col;
    Scanner sc=new Scanner(System.in);       
   public void take()
            {
                for( i=0;i<5;i++)
                {
                     for( j=0;j<5;j++)
                     {            
                 System.out.print("\nValue: ");
                 arr[i][j]=sc.nextInt();
                }          
             }
              System.out.print("\nArray as follows\n"); 
              for( i=0;i<5;i++)
                {
                     for( j=0;j<5;j++)
                     {            
                 System.out.print(" "+arr[i][j]);                 
                } 
                System.out.println();
             }
             display();
            }
            private void display()
            {
                for(i=0;i<5;i++)
                {
                     sum_row=0;
                     sum_col=0;
                   for(j=0;j<5;j++)
                   {
                       sum_row+=arr[i][j];
                       sum_col+=arr[j][i];
                    }
                    System.out.println("\nSum of row no "+ (i+1) + ": "+sum_row);
                    System.out.println("\nSum of column no "+ (i+1) + ": "+sum_col);
                }
            }
        }
            
      Sample Input Output

Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
Value: 9
Value: 9
Value: 7
Value: 6
Value: 5
Value: 4
Value: 3
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 6
Value: 5
Value: 4
Value: 3
Array as follows
 1 2 3 4 5
 6 7 8 9 9
 7 6 5 4 3
 2 3 4 5 6
 7 6 5 4 3
Sum of row no 1: 15
Sum of column no 1: 23
Sum of row no 2: 39
Sum of column no 2: 24
Sum of row no 3: 25
Sum of column no 3: 25
Sum of row no 4: 20
Sum of column no 4: 26
Sum of row no 5: 25
Sum of column no 5: 26

Tuesday, July 20, 2021

Matrix Program With Different Characters At Different Positions


 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: 
A C C C A
C X X X C
C X X X C
C X X X C
A C C C A


import java.util.*;
class ab
{
     char a[][];     
     Scanner sc=new Scanner(System.in);
     int i,j,n;
     char ch1,ch2,ch3;
     void display()
     {
        while(true)
        {
            System.out.print("\nEnter Value of 'N': ");
            n=sc.nextInt();
            if(n>3 && n<10)
            break;
        }
        a=new char[n][n];
        System.out.print("\nEnter 1st Character: ");
         ch1=sc.next().charAt(0);
         System.out.print("\nEnter 2nd Character: ");
         ch2=sc.next().charAt(0);
         System.out.print("\nEnter 3rd Character: ");
         ch3=sc.next().charAt(0);
         for(i=0;i<n;i++)
         {
              for(j=0;j<n;j++)
              {
                 if(i>0 && i<n-1 && j>0 && j<n-1)
                  a[i][j]=ch3;                 
                  else if((i+j)%3==0)
                  a[i][j]=ch1;
                  else
                  a[i][j]=ch2;
                }
            }
                         
                    
            System.out.println("\nMatrix as follows ");
          for(i=0;i<n;i++)
        {
           for(j=0;j<n;j++)
           {
         System.out.print(" "+a[i][j]);
        
        }
        System.out.println();
        
        }
             
    }
    public static void main(String args[])
    {
         ab obj=new ab();
         obj.display();
        }
    }
         
  

Tuesday, June 22, 2021

2 D Array and Interchanging Values In Cyclic Order

 
Take values in 4 x 4 array and interchange the values in cyclic order means 1st row will come down to 2nd row, 2nd row to 3rd,  3rd row to 4th and 4th row to 1st

import java.io.*;
class Matrix
{
     
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int arr[][]=new int[4][4];
    int i,j,x;
    void take() throws Exception
     {
        for(i=0;i<4;i++)
        {
             for(j=0;j<4;j++)
             {
                 System.out.print("\nValue: ");
                 arr[i][j]=Integer.parseInt(br.readLine());
                }
            }
      }
      void arrange()
      {
        for(i=3;i>0;i--)
        {
            for(j=0;j<4;j++)
            {
                x=arr[i][j];
                arr[i][j]=arr[i-1][j];
                arr[i-1][j]=x;
            }
        }
    }
     void show()
     {
        
         for(i=0;i<4;i++)
        {
             for(j=0;j<4;j++)
             {
                 System.out.print(" "+arr[i][j]);
                
                }
                System.out.println();
            }
        }
         
         public static void main(String args[]) throws Exception
         {
             Matrix ob=new Matrix();
             ob.take();
             System.out.println("\nArray Before Interchange");
             ob.show();
             ob.arrange();
             System.out.println("\nArray After Interchange");
             ob.show();
            }   
}  


Sample output after entering 16 values in a 4 X 4 matrix


Array Before Interchange
 12 4 55 6
 5 4 3 22
 3 4 5 7
 8 9 90 9

Array After Interchange
 8 9 90 9
 12 4 55 6
 5 4 3 22
 3 4 5 7

Sunday, June 6, 2021

2 D Array C Program

 Details of the program: Write a C program that takes a 2-dimensional array arr as input (of size 4 x 4),  multiply all the even elements in arr and store in a variable mult  check if multiplying any two elements in arr equals mult. Check this for all possible pairs.

 If any pair equals mult, print those pairs.

Otherwise, print “No pair found”

#include<stdio.h>
void main()
{

int arr[4][4];
int i,j,n,m,mult=1,p=0;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
printf("\nValues:");
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(arr[i][j]%2==0)
mult=arr[i][j]*mult;
}
}
printf("\nMatrix as follows\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d ",arr[i][j]);
printf("\n");
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(arr[i][j]%2==0)
{

for(n=i;n<4;n++)
{
for(m=j;m<4;m++)
{
if(arr[n][m]%2==0 && arr[n][m]*arr[i][j]==mult)
{
printf("\n Pair %d  %d",arr[i][j], arr[n][m]);
p=1;
}
}
}
}
}
}
if(p==0)
printf(" Pair Not Found");
getch();
}

Tuesday, March 24, 2020

Program On 1 D and 2 D Array In Combination

For Details of The Program: CLICK HERE

import java.io.*;
class Mat
{
int a[], N,b[][];
public void takeValues()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i;
System.out.print("\nENTER N=");
N=Integer.parseInt (br.readLine());
if (N<=1 || N>=12)
{
System.out.print("\nMATRIX SIZE OUT OF RANGE");
return;
}
a=new int[N];
b=new int[N][N];
for(i=0;i<=N-1;i++)
{
System.out.print("\nINPUT:");
a[i]=Integer.parseInt (br.readLine());
}
arrange();
}
private void arrange() 
{
int i,j,k,x,temp;
for(i=0;i<=N-2;i++)
{
for(j=i+1;j<=N-1;j++)
{
if(a[i]>a[j])
{
 temp=a[i];
 a[i]=a[j];
 a[j]=temp;
}
}
}
System.out.print("\nSORTED ARRAY\n");
for(i=0;i<=N-1;i++)
System.out.print(a[i]+" ");
for(i=0;i<=N-1;i++)
{
 for(j=0;j<=N-1-i;j++)
 {
      b[i][j]=a[j];
 }
 x=j;
 for(k=0;k<=i-1;k++)
 {
      b[i][x++]=a[k];
    }
}
 System.out.print("\nFILLED MATRIX\n");
 for(i=0;i<=N-1;i++)
 {
      for(j=0;j<=N-1;j++)
      {
         System.out.print(b[i][j]+" "); 
        }
   System.out.println();     
}
}
public static void main(String args[]) throws IOException
{
Mat ob=new Mat();
ob.takeValues();
}
}

Back To 2019 Computer Practical Paper: CLICK HERE

Saturday, March 21, 2020

BlueJ Program On 2 D Array And Non Boundary Elements Sorting


For Details of the program: CLICK HERE

Programming Code:


import java.io.*;
class CSort
{
int m,arr[][],dummy[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
void takeNumber()throws Exception
{
while(true)
{

System.out.println("Enter the Number of rows(3 to 10):"); 
m=Integer.parseInt(br.readLine());
 if(m>=3 && m<=10) 
break;
}
arr=new int[m][m];
dummy=new int[(m-2)*(m-2)];
for(int i=0;i< m;i++)
{
for(int j=0;j< m;j++)
{

System.out.println("Enter Value:");
arr[i][j]=Integer.parseInt(br.readLine());
}
} 
}
void show()
{
for(int i=0;i< m;i++)
{
for(int j=0;j< m;j++)
{

System.out.print(" "+arr[i][j]);
}
System.out.println();
}
}
void takeToDummy()
{
int x=0;
for(int i=0;i< m;i++)
{
for(int j=0;j< m;j++)
{
if(i!=0 && i!=m-1 && j!=0 && j!=m-1)
dummy[x++]=arr[i][j];
}

} 
} 
void takeToArray()
{
int x=0;
for(int i=0;i< m;i++)
{
for(int j=0;j< m;j++)
{
if(i!=0 && i!=m-1 && j!=0 && j!=m-1)
arr[i][j]=dummy[x++];
}
}
}
void showDiagonals()
{
int left,right;
left=right=0;
System.out.println("Diagonal values are as follows.\n");
for(int i=0;i< m;i++)
{
for(int j=0;j< m;j++)

{
if (i==j ||i+j==m-1)
System.out.print(" "+arr[i][j]);
else
System.out.print(" ");
if(i==j)
left+=arr[i][j];
else if(i+j==m-1)
right+=arr[i][j];
}
System.out.println();
}

System.out.println("Sum of Left Diagonal values="+left);

System.out.println("Sum of Right Diagonal values="+right);
}
void sort()
{
int i,j,t;
for(i=0;i< (m-2)*(m-2);i++)
{
for(j=i+1;j< (m-2)*(m-2);j++)
{
if(dummy[i]>dummy[j])
{
t=dummy[i];
dummy[i]=dummy[j];
dummy[j]=t;
}

}
}
}
public static void main(String args[]) throws Exception
{
CSort ob=new CSort();
ob.takeNumber();
System.out.println("Original Array\n");
ob.show();
ob.takeToDummy();
ob.sort();
ob.takeToArray();
System.out.println("Arranged Array\n");
ob.show();
ob.showDiagonals();
}
}


Variable Description


Type
Name
Purpose
Scope


int
m
Holds the size of
Within all functions




array



BufferedReader
br
Used for user input
Within void





takeNumber ()





function


int
arr [][]
2-d array to hold the
Within all functions




numbers



int
dummy []
Used to sort the non
Within voi sort (),




boundary elements
void takeToArray ()





functions


Algorithm












Step 1: int type variable ‘m’ and BufferedReader object ‘br’ are created. Array arr[][] and dummy [] are declared

Step 2: Take the size of the square array from user and store it in ‘m’

Step 3: Check the validity of the value of ‘m’ and if it passes, create arrays arr[][] and dummy [].

Step 4: Take values from user and store them in array ‘arr [][]’.

Step 5: Display the original array.

Step 6: Take the non boundary values from the array ‘arr [][]’ and store them in array dummy [].

Step 7: Sort the elements in the 1-d array dummy and finally set those values in the original array arr [][] in non boundary locations so that the elements are sorted now.

Step 8: Now display the array arr [][]

Step 9: Display the diagonal values and sum of the diagonals

Back To 2016 Computer Practical paper: CLICK HERE

Subscribe via email

Enter your email address:

Delivered by FeedBurner