KEMBAR78
CompSc11 Matrix Patterns | PDF | Integer (Computer Science) | Constructor (Object Oriented Programming)
0% found this document useful (0 votes)
6 views8 pages

CompSc11 Matrix Patterns

Uploaded by

csegunjan03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

CompSc11 Matrix Patterns

Uploaded by

csegunjan03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Q1 .

Design a class having following specifications :-


Class Name :- Matrix_ZigZag_RowWise
Data Members : int a[][],r,c // integer type
Member Methods :-
Matrix_ZigZag_RowWise(int , int) // parameterized Constructor
void store( ) // To store values in the following format ( if r = 4 and c =5)

1 2 3 4 5
10 9 8 7 6
void show() // To display the 11 12 13 14 15 elements of the
matrix. 20 19 18 17 16
Write the main() function to create an object of the class and call the respective member methods.

Q2 . Design a class having following specifications :-


Class Name :- Matrix_ZigZag_ColumnWise
Data Members : int a[][],r,c // integer type
Member Methods :-
Matrix_ZigZag_ColumnWise(int , int) // parameterized Constructor
void store( ) // To store values in the following format ( if r = 4 and c =5)

1 8 9 16 17
2 7 10 15 18
void show() // To display the 3 6 11 14 19 elements of the
matrix. 4 5 12 13 20
Write the main() function to create an object of the class and call the respective member methods.

Q3 . Design a class having following specifications :-


Class Name :- Matrix_Upper_Triangular
Data Members : int a[][], n // integer type (Square Matrix)
Member Methods :-
Matrix_ Upper_Triangular ( int ) // parameterized Constructor
void store( ) // To store values in the following format ( if n= 4 )

1 2 3 4
0 5 6 7
void show() // To display the 0 0 8 9 elements of the matrix.
0 0 0 10
Write the main() function to create an object of the class and call the respective member methods.
Q4 . Design a class having following specifications :-
Class Name :- Matrix_Lower_Triangular
Data Members : int a[][], n // integer type (Square Matrix)
Member Methods :-
Matrix_ Lower_Triangular ( int ) // parameterized Constructor
void store( ) // To store values in the following format ( if n= 4 )

1 2 0 0
2 3 0 0
void show() // To display the 4 5 6 0 elements of the matrix.
7 8 9 10
Write the main() function to create an object of the class and call the respective member methods.

Q5. Design a class having following specifications :-


Class Name :- Matrix_Diagonal
Data Members : int a[][], n // integer type (Square Matrix)
Member Methods :-
Matrix_ Lower_Diagonal ( int ) // parameterized Constructor
void store( ) // To store values in the following format ( if n= 4 )

1 0 0 1
0 1 1 0
void show() // To display the 0 1 1 0 elements of the matrix.
1 0 0 1
Write the main() function to create an object of the class and call the respective member methods.

Q5. Design a class having following specifications :-


Class Name :- Matrix_Pattern
Data Members : int a[][], n // integer type (Square Matrix)
Member Methods :-
Matrix_Pattern ( int ) // parameterized Constructor
void store( ) // To store values in the following format ( if n= 4 )

1 2 3 4
16 5 6 7
void show() // To display the 15 14 8 9 elements of the
matrix. 13 12 11 10

Write the main() function to create an object of the class and call the respective member methods.
Answer 1 :
class Matrix_ZigZag_RowWise
{
int a[][];
int r,c;
Matrix_ZigZag_RowWise(int rr,int cc)
{
r = rr;
c = cc;
a = new int[r][c];
}
void store()
{
int i,j,k=1;
for(i=0;i<r;i++)
{
if(i%2==0)
{
for(j=0;j<c;j++)
{
a[i][j]=k++;
}
}
else
{
for(j=c-1;j>=0;j--)
{
a[i][j]=k++;
}
}
}
}
void show()
{
int i,j;
System.out.println("Original Matrix :- ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
public static void main(int x,int y)
{
Matrix_ZigZag_RowWise ob = new Matrix_ZigZag_RowWise(x,y);
ob.store();
ob.show();
}
}
Answer 2 :
class Matrix_ZigZag_ColumnWise
{
int a[][];
int r,c;
Matrix_ZigZag_ColumnWise(int rr,int cc)
{
r = rr;
c = cc;
a = new int[r][c];
}
void store()
{
int i,j,k=1;
for(i=0;i<c;i++)
{
if(i%2==0)
{
for(j=0;j<r;j++)
{
a[j][i]=k++;
}
}
else
{
for(j=r-1;j>=0;j--)
{
a[j][i]=k++;
}
}
}
}
void show()
{
int i,j;
System.out.println("Original Matrix :- ");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}
public static void main(int x,int y)
{
Matrix_ZigZag_ColumnWise ob = new Matrix_ZigZag_ColumnWise(x,y);
ob.store();
ob.show();
}
}
Answer 3 :
class Matrix_Upper_Triangular
{
int a[][];
int n;
Matrix_Upper_Triangular(int nn)
{
n = nn;
a = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
}
}
}

void store()
{
int i,j,k=1;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
a[i][j]=k++;
}
}
}

void show()
{
int i,j;
System.out.println("Original Matrix :- ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

public static void main(int x)


{
Matrix_Upper_Triangular ob = new Matrix_Upper_Triangular(x);
ob.store();
ob.show();
}
}
Answer 4 :

class Matrix_Lower_Triangular
{
int a[][];
int n;
Matrix_Lower_Triangular(int nn)
{
n = nn;
a = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
}
}
}

void store()
{
int i,j,k=1;
for(i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
a[i][j]=k++;
}
}
}

void show()
{
int i,j;
System.out.println("Original Matrix :- ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

public static void main(int x)


{
Matrix_Lower_Triangular ob = new Matrix_Lower_Triangular(x);
ob.store();
ob.show();
}
}
Answer 5 :

class Matrix_Diagonal
{
int a[][];
int n;
Matrix_Diagonal(int nn)
{
n = nn;
a = new int[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
}
}
}
void store()
{
int i,j,k=1;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j || (i+j)==(n-1))
a[i][j]=1;
}
}
}
void show()
{
int i,j;
System.out.println("Original Matrix :- ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

public static void main(int x)


{
Matrix_Diagonal ob = new Matrix_Diagonal(x);
ob.store();
ob.show();
}
}
Answer 6 :

class Matrix_Pattern
{
int a[][];
int n;
Matrix_Pattern(int nn)
{
n = nn;
a = new int[n][n];
}

void store()
{
int i,j,k=1;
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
a[i][j]=k++;
}
}
for(i=n-1;i>0;i--)
{
for(j=i-1;j>=0;j--)
{
a[i][j]=k++;
}
}
}

void show()
{
int i,j;
System.out.println("Original Matrix :- ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
}

public static void main(int x)


{
Matrix_Pattern ob = new Matrix_Pattern(x);
ob.store();
ob.show();
}
}

You might also like