# [Two dimension array] C program to read matrix and print its diagonal

[Two dimension array] C program to read matrix and print its diagonal

This is the example of c-programming. You can edit and compile code in your own way on clicking button. If you have better solution or you found any error on code, then give us suggestion.

## Code Explanation :

This is the example of C program. you must have following topics to understand this example.

Here, at first we have define fixed row and column using `#define` keyword.

Suppose we have a example of 4 X 4 matrix which can be represent as

```A00 A01 A02 A03
A10 A11 A12 A13
A20 A21 A22 A23
A30 A31 A32 A33```

Here, `A00, A11, A22, A33` are known ad primary diagonal and to print this diagonal, following condition must match:

` row = column`

We can also print seconday diagonal (`A03, A12, A21, A30`).

To print this following condition must match:

` row = numberOfRows - column -1`

In this process we use two loops, i.e. a loop for columns and a loop for rows, and we test for the above condition in the inner loop.

We are going to represent this code into algorithm and flowchart.

## Algorithm

```STEP 1 : Start

STEP 2 : Declare MAXROW, MAXCOL, matrix[MAXROW][MAXCOL], i, j, r and c

STEP 2 : Initialize MAXROW = 10 and MAXCOL = 10

STEP 4 : Repeat this step until i < r
STEP 4.1 : Repeat this step until j < c
STEP 4.1.1 : Read element matrix[i][j]
STEP 4.1.2 : j++
STEP 4.2 : i++

STEP 5 : IF r == c THEN
STEP 5.1 : Repeat this step until i < c
STEP 5.1.1 : Repeat this step until j < c
STEP 5.1.1.1 : IF i == j THEN
Print element matrix[j][i]
ELSE
print tab using "t"
STEP 5.1.1.2 : j++
STEP 5.1.2 : print line break using "t"
STEP 5.1.3 : i++

ELSE
print "Matrix is not a Square Matrix"

STEP 6 :End```

## Flowchart If you got any issue on understanding this example, you can comment below. We will reply your problem with better solution.

Our example mainly covers following topics in any programing

• Basic Programs
• Number Programs
• Array Programs
• Matrix Programs
• Pattern Programs
• String Programs
• Tree Programs

Here we share programs on various topics such as array strings, series area & volume of geometric figures, mathematical calculation algorithms for sorting & searching and much more.Our goal is to provide you with the perfect solution to all the programming problems you may have encountered either during interviews or in class assignments. If you don’t find what you are looking for then please drop a line in the comment section of example page or you can request from account page so that we can get it added to the below collection of programs. Happy Learning!!

If you are confident with the above programs and are able to successfully understand and run them without any problems then it is time for you to take a step further and learn comprehensive programming principles using examples and flow diagrams. Here is the link: Programming tutorial lists .

If you find any code error or grammetical error then you can suggest for example improvement on clicking below button.

## // SEARCH

What are you searching for....?