Jemal Amshalal
Jemal Amshalal

Reputation: 27

C - Matrix Main and Anti Diagonals (Replace)

I have a task and i need help from pros.

My task is: User inputs number of rows and columns for matrix[8][8], with some prohibitions using IF. Later user enters all the elements for this matrix, using loops and outputting this matrix.

Here is the code :

#include <stdio.h>

int main () 
{
    int matrix[10][10];
    int nrows, ncols, i, j;

    printf("Enter number of rows: "); //user enters number of rows
    scanf("%d", &nrows);
    if(nrows < 4){
        printf("\n Out of range, please try again");
        exit(5);
    }
    printf("Enter number of columns: "); //user enters number of columns
    scanf("%d", &ncols);
    if(ncols < 4){
        printf("\n Out of range, please try again");
        exit(5);
    }
    printf("Enter matrix elements: "); //user enters all the elements for Matrix

    for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
            scanf("%d",&matrix[i][j]);
    }

    printf("\n");
}
    printf("This is your matrix: \n");
    for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
            printf("%d \t",matrix[i][j]);}

    printf("\n");}



    return 0;   
}

Now i need to add one thing, i want to take diagonal and anti-diagonal of this matrix and replace all the elements there with capital X. As i don't know, what kind of matrix it will be, like it can be matrix[4][6] or matrix[7][4], so i need to do something that will work with any kind of matrix that user can enter in this program.

Example of this task (In this program i can't enter less than [4][4], but for the example i'll do it):

User enters rows - 3
User enters columns - 3
User enters elements : 1 2 3 4 5 6 7 8 9
Output :

    1 2 3
    4 5 6
    7 8 9

Here i need to do something like this :

X 2 X
4 X 6
X 8 X

If user enters not square matrix, for example:

User enters rows: 3
User enters rows: 4
User enters elements: 1 2 3 4 5 6 7 8 9

Out matrix: 

1 2 3 4
5 6 7 8
9 0 1 2

After replacing with X:

X 2 3 X
5 X X 8
9 X X 2

Replaced diagonal and anti-diagonal with capital Xs.

Please any help ?

Upvotes: 1

Views: 2313

Answers (2)

klaus
klaus

Reputation: 1227

You can do something like this:

    printf("This is your matrix: \n");
for(i = 0; i < nrows; i++){
    for(j = 0; j < ncols; j++){
        if (i == j)
            printf("X \t");
        else if (i == ncols - j - 1)
            printf("X \t");
        else
            printf("%d \t", matrix[i][j]);
    }

    printf("\n");}

Example:

Enter number of rows: 4
Enter number of columns: 5
Enter matrix elements: 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4




This is your matrix: 
X   1   1   1   X   
2   X   2   X   2   
3   3   X   3   3   
4   X   4   X   4 

Upvotes: 2

Lalit Verma
Lalit Verma

Reputation: 802

Complete code and its working on both of the examples you provided in your quesetion, just tell me if anything missing here

#include <stdio.h>

int main ()
{
    int matrix[10][10];
    int nrows, ncols, i, j;

    printf("Enter number of rows: "); //user enters number of rows
    scanf("%d", &nrows);
    if(nrows < 4){
        printf("\n Out of range, please try again");
        exit(5);
    }
    printf("Enter number of columns: "); //user enters number of columns
    scanf("%d", &ncols);
    if(ncols < 4){
        printf("\n Out of range, please try again");
        exit(5);
    }
    printf("Enter matrix elements: "); //user enters all the elements for Matrix

    for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
            scanf("%d",&matrix[i][j]);
    }

    printf("\n");
}
    printf("This is your matrix: \n");
    for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
            printf("%d \t",matrix[i][j]);}

    printf("\n");}
//When square matrix
    if(nrows==ncols)
    {
        for(i = 0; i < nrows; i++){
           matrix[i][i]='X';
            }
            int count=0;
            for(i=nrows-1;i>=0;i--)
            {
                matrix[count][i]='X';
                count++;

            }
 for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
                if(i==j||(i+j==nrows-1))
            printf("%c \t",matrix[i][j]);
        else
                        printf("%d \t",matrix[i][j]);

            }

    printf("\n");}
    }
else
{
    if(nrows<ncols)
    {
        int count=ncols-1;
        for(i = 0; i < nrows; i++){
           matrix[i][i]='X';
            }
            for(i = 0; i < nrows; i++){
           matrix[i][count]='X';
           count--;
            }
          for(i = 0; i < nrows; i++){
        for(j = 0; j < ncols; j++){
                if(i==j||(i+j==nrows))
                printf("%c \t",matrix[i][j]);
        else
                                    printf("%d \t",matrix[i][j]);



            }

    printf("\n");}
    }

}

    return 0;
}

Upvotes: 0

Related Questions