Icse Project - 1: Double Dimensional Arrays
Icse Project - 1: Double Dimensional Arrays
Icse Project - 1: Double Dimensional Arrays
DOUBLE DIMENSIONAL
ARRAYS
SECTION - B
ROLL NO - 12
SCHOOL: A.G. CHURCH ASANSOL
PROGRAM - 1
PROGRAM 1:
1. Accept a Double Dimensional Array and perform the
following:
a. print the matrix in tabular format.
b. print the transposed matrix.
c. if it is a Square matrix print:
i.
Check if it is a Symmetric matrix or not.
ii.
Print the main of left diagonal diagonally.
iii.
It is a unit matrix or not
iv.
Upper main half of the matrix
v.
Intersection point of 2 diagonals.
vi.
Elements above, under, right and left to the
intersection point .
d. if the DDA is not a square matrix print:
i.
Transposed matrix
ii.
Cumulative products of the elements on 2nd row.
iii.
Sum of every row should appear at the end of
each respective row
iv.
Sum of every column should appear at the
bottom of the respective column
v.
Sort the elements of the DDA in ascending order
and print it in 4 *y format where y should be
calculated and might generate a row with
unequal number of elements as compared to
other rows.
VARIABLE LISTING
Serial no
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Variable name
r
sum
f
c
k
pr
mid1
mid2
t
s1
s2
M[][]
i
j
a[]
M2[][]
T[][]
y
Variable type
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
int
Variable description
number of row
counter variable
flag variable
number of column
flag variable
to find product
mid value
mid value
temporary variable
counter variable
counter variable
DDA variable
loop variable
loop variable
SDA variable
DDA variable
DDA variable
calculated column
PROGRAM
import java.io.*;
class matrix
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int sum=0,f=0;
int r,y,c,k=0,pr=1,mid1,mid2,t,s1=0,s2=0;
System.out.println("Enter number of rows and columns");
r=Integer.parseInt(br.readLine());
c=Integer.parseInt(br.readLine());
int M[][]=new int[r][c];
int T[][]=new int[c][r];
int a[]=new int[r*c];
if((r*c)%4==0)
y=(r*c)/4;
else y=(r*c)/4 + 1;
int M2[][]=new int [4][y];
for(int i=0;i<r;i++)
{ System.out.println("Enter element for row "+(i+1));
for(int j=0;j<c;j++)
{
M[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("Printing the matrix IN TABULAR FORMAT");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
System.out.print(M[i][j]+" ");
System.out.println();
}//PRINTING IT IN TABULAR FORMAT
System.out.println("PRINTING THE TRANSPOSED MATRIX");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
T[j][i]=M[i][j];
}
for(int i=0;i<c;i++)
{
for(int j=0;j<r;j++)
System.out.print(T[i][j]+" ");
System.out.println();
}
if(r==c)
{
for( int i=0;i<r;i++)
{
for( int j=0;j<c;j++)
{
if(M[i][j]!=M[j][i])
k=1;
}
}
if(k==0)
System.out.println("It is a symmetric matrix");
else
System.out.println("it is not a symmetric matrix");
}
else
System.out.println("Matrix is not a square matrix");
if(r==c)
{
System.out.println("Printing the left diagonal");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
if(i==j)
System.out.print(M[i][j]);
else System.out.print(" ");
}
System.out.println();
}
}
else System.out.println("Left diagonal cannot be printed");
if(r==c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{if((i+j)==(r-1))
sum=sum+M[i][j];
}
}
System.out.println("SUM of the elements of the right diagonal is
="+sum);
}
else System.out.println("It is not Square Matrix");
sum=0;
System.out.println();
System.out.println("Printing Sum of every row and column of matrix");
for(int i=0;i<r;i++)
{
s1=0;
for(int j=0;j<c;j++)
{
s1+=M[i][j];
System.out.print(M[i][j]+" ");
}
System.out.println();
}
if(r==c)
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{if(i==j&&M[i][j]==1)
{ if(i!=j&&M[i][j]==0)
f=1;
else
{f=0;break;}
}
}
}
if(f==1)
System.out.println(" It is a unit matrix");
else
System.out.println(" It is not a unit matrix");
}
else
{
for(int j=0;j<c;j++)
{
System.out.print(M[i][j]+" ");
sum=sum+M[i][j];
}
System.out.print(" "+sum);
sum=0;
System.out.println();
}
System.out.println("\nPRINTING THE MATRIX IN SUCH A WAY THAT
SUM OF EVERY COLUMN APPEARS AT THE BOTTOM OF RESPECTIVE
COLUMN");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[j]+=T[j][i];
System.out.print(M[i][j]+" ");
}
System.out.println();
}
System.out.println();
for(int j=0;j<c;j++)
System.out.print(a[j]+" ");
System.out.println("\nPRINTING THE UPPER MAIN HALF OF
MATRIX");
if(r%2==0)
t=r/2;
else t=(r+1)/2;
for(int i=0;i<t;i++)
{
for(int j=0;j<c;j++)
System.out.print(M[i][j]+" ");
System.out.println();
}
System.out.println("PRINTING SORTED ARRAY IN DESIRED 4*Y
FORMAT ");
s1=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
a[s1]=M[i][j];
s1++;
}
}
for(int i=0;i<s1;i++)
{
for(int j=1;j<(s1-i);j++)
{
if(a[j-1]>a[j])
{
t=a[j-1];
a[j-1]=a[j];
a[j]=t;
}
}
}
s2=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<y;j++)
{
if(s2<s1)
{ M2[i][j]=a[s2];
System.out.print(M2[i][j]+"\t");
s2++; }
else break;
}
System.out.println();
}
}
}
}
OUTPUT
Printing the matrix IN TABULAR FORMAT
1234
5 6 7 87
8432
PRINTING THE TRANSPOSED MATRIX
1 5 8
2 6 4
3 7 3
4 87 2
Matrix is not a square matrix
Left diagonal cannot be printed
It is not Square Matrix
10
105
17
14 12 13 93
PRINTING THE UPPER MAIN HALF OF MATRIX
1234
5 6 7 87
PRINTING SORTED ARRAY IN DESIRED 4*Y FORMAT
1
87
PROGRAM - 4
PROGRAM 4:
Assign a 5*5 matrix and print the four triangles created at the
four corners of the matrix.
MATRIX
TRIANGLE 4
06942
5
12439
96
57308
065
96125
06542
TRIANGLE 1
069
12
5
TRIANGLE 2
TRIANGLE 3
942
39
8
8
25
542
VARIABLE LISTING
Serial no
1
2
3
4
5
6
7
8
9
10
Variable name
i
j
p
a
b
k
e
c
d
ar[][]
Variable type
int
int
int
int
int
int
int
int
int
int
Variable description
loop variable
loop variable
counter
counter variable
counter variable
counter variable
flag variable
flag variable
flag variable
array variable
PROGRAM
import java.io.*;
class triangle
{int i,j,p=2,a=2,b=0,k,e=0,c=4,d=4;
void method()
{ int ar[][]={{0,6,9,4,2},{1,2,4,3,9},{5,7,3,0,8},{9,6,1,2,5},
{0,6,5,4,2}};
System.out.println("Printing Original Matrix");
for(int i=0;i<5;i++)
{for(int j=0;j<5;j++)
{System.out.print(ar[i][j]+" ");
}
System.out.println();
}//PRINTING DONE
System.out.println("TRIANGLE 1");
System.out.println();
int f=3;
for(int i=0;i<3;i++)
{
for(int j=0;j<f;j++)
System.out.print(ar[i][j]+" ");
f--;
System.out.println();
}
System.out.println("TRIANGLE 2");
System.out.println();
int a=2;int b=0;
for(int i=0;i<3;i++)
{ for(int j=0;j<b;j++)
{
System.out.print(" ");
}//j loop closed
b+=2;
for(int k=i+2;k<=4;k++)
{
System.out.print(ar[i][k]+" ");
}//k loop closed
System.out.println();
a++;
}
System.out.println("TRIANGLE 3");
System.out.println();
int d=4;c=4;
for(int i=2;i<5;i++)
{
for(int j=0;j<c;j++)
{ System.out.print(" ");
}//j loop closed
c-=2;
for(int k=6-i;k<=4;k++)
{System.out.print(ar[i][k]+" ");
}//k loop closed
System.out.println();
d--;
}
System.out.println("Triangle 4");
System.out.println();
e=0;
for(int i=2;i<5;i++)
{ for(int j=0;j<=e;j++)
{
System.out.print(ar[i][j]+" ");
}//j loop closed
System.out.println();
e++;
}
}
}
OUTPUT
Printing Original Matrix
06942
12439
57308
96125
06542
TRIANGLE 1
069
12
5
TRIANGLE 2
942
39
8
TRIANGLE 3
8
25
542
Triangle 4
5
96
065