Page 344 - Cs_withBlue_J_C11_Flipbook
P. 344
4x4 matrix is
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
New elements are
0 2 3 4
5 0 7 8
9 1 0 3
4 5 6 0
Program 14 Create the following class:
class name : spiral
Data members : int sparr[][] – stores the matrix in spiral format
int s : Size of the matrix
Method:
spiral(int n) : constructor to initialise s=n and create the matrix in spiral format.
void display() : Display the matrix
Example of spiral matrix:
Clockwise
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
1 import java.util.*;
2 class spiral
3 {
4 int sparr[ ][ ];
5 int s;
6 spiral(int n)
7 {
8 s=n;
342342 Touchpad Computer Science-XI

