Javatpoint Logo
Javatpoint Logo

Create a matrix with alternating rectangles of O and X

Introduction

Basic data structures called matrices are used in mathematics and computer science to arrange and work with data. In this post, we'll look at how to use the C programming language to make a matrix with 'O' and 'X' rectangles that alternate. The logic is to divide the matrix into sections and fill each section with either 'O' or 'X' according to a pattern in order to create a matrix with alternating rectangles. The desired arrangement of 'O' and 'X' and the matrix's dimensions typically define the pattern.

Implementation

1. Recognizing the Dimensions: The alternating pattern must be determined by taking into account the dimensions of the matrix. Let's use a 5x5 matrix as an example to keep things simple.

2. Iterating Through Rows and Columns: To iterate through each row and column of the matrix, nested loops will be used.

3. 'O' and 'X' alternate: The secret to accomplishing the alternating pattern is to use the row and column indices to decide which character to put in the current position: 'O' or 'X'.

The 'O' or 'X' is inserted into the matrix based on the condition that determines whether the row index is in an odd or even section.

4. Printing the Matrix: To see the alternating pattern, print the matrix after it is filled.

Code

Output:

Create a matrix with alternating rectangles of O and X

Code Explanation

Matrix Declaration

  • It is declared that a 2D character array matrix has ROWS and COLS dimensions.

Using Nested Loops to Complete the Matrix

  • Each matrix element is iterated over using two nested loops.
  • The row index is represented by the outer loop (i), and the column index is represented by the inner loop (j).

Conditional Filling

  • An if-else statement is present inside the nested loops.
  • 'O' or 'X' should be filled in the current matrix position based on the condition (i / 2) % 2 == 0.
  • 'O' is assigned if the condition is true; 'X' is assigned if it is not.

Printing the Matrix

  • The matrix is filled, and then its contents are printed using an additional set of nested loops.
  • Every character in the matrix is printed after space by the printf("%c ", matrix[i][j]) statement.
  • Printf("\n") ends the inner loop and advances to the subsequent line upon printing each row.

Return Statement

  • The program was successfully executed, as evidenced by the return 0 statement.

Time Complexity

The code to create a matrix with alternating rectangles of 'O' and 'X' has a time complexity of O(R * C), where R is the matrix's row count and C is its column count. The two nested loops that are used to iterate over each matrix element are the source of this complexity. The time complexity of both loops is proportional to the product of the number of rows and columns because they run from 0 to the corresponding dimensions of the matrix.

Space Complexity

Because it depends on the size of the 2D character array matrix, the code's space complexity is also O(R * C). The space needed is determined by multiplying the number of rows by the number of columns. There are R * C characters ('O' or 'X') that make up each element in the matrix. Since there are no other data structures or recursive calls that make a substantial difference in the space complexity, the matrix size is the primary factor.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA