The following program generates matrices in a clockwise spiral pattern based on user input for the size and corner values. It consists of three main components: the caracolMatrix
function, the checkRange
function, and the main
function. This project is a homework realized for Compu I
The caracolMatrix
function takes two parameters, n
and corner
, and generates a matrix in a clockwise spiral pattern.
n
: An integer representing the size of the matrix.corner
: An integer representing the starting corner of the matrix (values 1-4).
- Initialize the
matrix
array with zeros. - Set the initial values of
e
,x
, andy
based on thecorner
parameter. - Set the initial
step
value based on the current direction. - Iterate
i
from 1 ton * n
:- Set the value of
matrix[e]
toi
. - If the next element (
matrix[e + step]
) is zero and the current positionx
is less thann
, move to the next element in the current direction. - Otherwise, update the direction (
y
) and the step value (step
) based on the next direction. - Move to the next element in the updated direction.
- Set the value of
- Print the elements of the
matrix
array in a tabular form, with each row separated by a newline.
The checkRange
function checks if the given values n
and a
are within specified ranges.
n
: An integer representing the size of the matrix.a
: An integer representing the corner value.
1
if bothn
anda
are within the specified ranges.0
otherwise.
The main
function is the entry point of the program. It handles user input, validation, and calling the necessary functions to generate and print the matrices.
- Read the value of
size
from the user. - Initialize the
nMatrix
andcornerMatrix
arrays to store the sizes and corner values, respectively. - Iterate
i
from 0 tosize - 1
:- Read the values of
n
andcorner
from the user and validate them using thecheckRange
function. - If the values are not valid, prompt the user to enter correct values.
- Store the valid values in the
nMatrix
andcornerMatrix
arrays.
- Read the values of
- Iterate
i
from 0 tosize - 1
:- Call the
caracolMatrix
function with the corresponding size and corner values from the arrays. - Print three newline characters to separate the matrices.
- Call the
This program allows users to generate matrices in a clockwise spiral pattern by providing the size and corner values. The caracolMatrix
function implements the spiral pattern algorithm, while the checkRange
function validates the input ranges. The main
function handles user input, validation, and calls the necessary functions to generate and print the matrices.