Page 320 - computer science (868) class 11
P. 320
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;
9 sparr= new int[s][s];
10 }
11 void display()
12 {
318318 Touchpad Computer Science-XI

