Posted on by Kalkicode
Code Matrix

# Diamond shape in odd square matrix

The problem you're addressing involves printing a diamond shape pattern within an odd-sized square matrix. The diamond pattern starts from the center of the matrix and extends outwards, forming a symmetrical pattern.

## Problem Statement and Example

Given an odd-sized square matrix, your task is to print a diamond shape pattern within it. For example, consider the following 7x7 matrix:

`````` 1   2   3   4   5   6   7
8   9  10  11  12  13  14
15  16  17  18  19  20  21
22  23  24  25  26  27  28
29  30  31  32  33  34  35
36  37  38  39  40  41  42
43  44  45  46  47  48  49``````

The output pattern should be a diamond shape formed using the numbers from the matrix.

## Idea to Solve

To solve this problem, you need to iterate through the matrix and print the elements in a way that forms the diamond pattern. You can divide the process into two main parts: printing spaces and printing the actual matrix elements.

## Pseudocode

``````function diamond_view(matrix)
if size of matrix is not odd or matrix is not square
print "Not a valid perfect Odd square matrix"
else
col = size of matrix / 2
counter = col * 3
distance = 3
for i = 0 to size of matrix - 1
print spaces to the left (counter spaces)
print elements of the matrix row i from col to (SIZE - col)
if i < size of matrix / 2
decrease counter by distance
decrease col by 1
else
increase col by 1
increase counter by distance
print newline``````

## Algorithm Explanation

1. Check if the size of the matrix is odd and if the matrix is square. If not, print an error message.
2. Initialize `col` to the middle column index, `counter` to `col * 3`, and `distance` to `3`.
3. Loop through each row of the matrix:
• Print spaces to the left of the elements (using the value of `counter`).
• Print the elements of the current row within the range of `col` to `(SIZE - col)`.
• Adjust `col` and `counter` based on the current row index.
4. After printing each row, move to the next line.

## Resultant Output Explanation

It calculates the necessary spacing and prints the elements of the matrix to create the diamond pattern. The output shows the diamond shape pattern formed by the numbers from the given matrix.

## Time Complexity

The time complexity of this algorithm is O(n^2), where 'n' is the number of rows (or columns) in the matrix. This is because you are iterating through each element of the matrix once and performing a constant amount of work for each element.

Your code is clear and correctly produces the desired diamond pattern within an odd-sized square matrix. The output explanation demonstrates that it works as expected for the given example matrix.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment

Saheli ghosh     548 Day ago
``Want code``
Categories
Relative Post