Copy a square matrix of tam n into another complemetar n-1, omitted row 0 and column 0 of matrix n

Asked

Viewed 32 times

0

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<locale.h>

int main(){
    int i,j;    
    int mat[3][3];
    int comp[2][2];     

    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            printf("Entre com o Valor [%d][%d]\n",i+1,j+1);
            scanf("%d",&mat[i][j]);     
        }
    }

    for(i=0;i<3;i++){
        if(i==0){
        i++;
        for(j=0;j<3;j++){   
            if(j==0){
            j++;
            comp[i-1][j-1]=mat[i][j];               
            }               
            else comp[i-1][j]=mat[i][j];
            }
        }           
        else{                       
            for(j=0;j<3;j++){
                if(j==0){
                j++;
                comp[i][j-1]=mat[i][j];
                }
            else comp[i][j]=mat[i][j];
            }
        }
    }   

    printf("\n");
    printf("Matriz 3 X 3\n\n");

    for(i=0;i<3;i++){
        for(j=0;j<3;j++){
            printf("%d ",mat[i][j]);
            if(j==2) printf("\n");          
        }       
    }
    printf("\n");
    printf("Matriz 2 X 2\n\n");

    for(i=0;i<2;i++){
        for(j=0;j<2;j++){
            printf("%d ",comp[i][j]);
            if(j==1) printf("\n");          
        }       
    }

    return 0;

}
  • Cool the code... But, what is your doubt or your difficulty? What is it that you want to ask?

  • I need to make a code that receives a square matrix tam N and copies the data in another complementary tam /n-1. I made a basic case to remove row 0 and column 0, but actually the row and column to be omitted could be anyone to be passed as argument.

2 answers

0

Basically, the copy/conversion code between the matrices would look something like this:

for( i = 0; i < N - 1; i++ )
    for( j = 0; j < N - 1; j++ )
        comp[i][j] = mat[i+1][j+1];

Follow your modified code with troubleshooting:

#include <stdio.h>
#include <stdlib.h>

#define N  (3)

int main( void )
{
    int i,j;
    int mat[N][N];
    int comp[N-1][N-1];

    /* Preenche matriz 3x3 */
    for( i = 0; i < N; i++ )
    {
        for( j = 0; j < N; j++ )
        {
            printf("Entre com o Valor [%d][%d]: ", i + 1, j + 1 );
            scanf("%d", &mat[i][j] );
        }
    }

    /* Copia conteudo das matrizes ignorando a primeira linha e coluna */
    for( i = 0; i < N - 1; i++ )
        for( j = 0; j < N - 1; j++ )
            comp[i][j] = mat[i+1][j+1];

    /* Exibe matriz 3x3 */
    printf("\nMatriz 3 X 3:\n");
    for( i = 0; i < N; i++ )
    {
        for( j = 0; j < N; j++ )
            printf("%d ", mat[i][j] );
        printf("\n");
    }

    /* Exibe matriz 2x2 */
    printf("\nMatriz 2 X 2:\n");
    for( i = 0; i < N - 1; i++ )
    {
        for( j = 0; j < N - 1; j++ )
            printf("%d ",comp[i][j]);
        printf("\n");
    }

    return 0;
}

Testing:

Entre com o Valor [1][1]: 1
Entre com o Valor [1][2]: 2
Entre com o Valor [1][3]: 3
Entre com o Valor [2][1]: 4
Entre com o Valor [2][2]: 5
Entre com o Valor [2][3]: 6
Entre com o Valor [3][1]: 7
Entre com o Valor [3][2]: 8
Entre com o Valor [3][3]: 9

Matriz 3 X 3:
1 2 3 
4 5 6 
7 8 9 

Matriz 2 X 2:
5 6 
8 9 

See working on Ideone.com

  • It worked if row 0 and column 0 were deleted. Would this code work for any row and column? , type to delete row 1 and column 2. Maybe in my code I didn’t make this clear.

  • It wouldn’t work! You need to be clear and objective when designing your question. Help us understand, that we help you solve. Just a code with no explanation doesn’t help much.

  • That’s right. As I’m new here, I’m still getting the hang of it. But I wanted to take a base case first. And your code helped me a lot.

  • Don’t be afraid to ask, knowledge is the son of curiosity and this place is full of curious.

-1

include

include

defines N (3)

int main( void ) { int k,l; int i = 0; int j = 0; int mat[N][N]; int comp[N-1][N-1];

/* Preenche matriz 3x3 */
for( k = 0; k < N; k++ )
{
    for( l = 0; l < N; l++ )
    {
        printf("Entre com o Valor [%d][%d]: ", k + 1, l + 1 );
        scanf("%d", &mat[k][l]);
    }
}

/* Copia conteudo das matrizes ignorando a primeira linha e coluna */
for( k = 0; k < N; k++ )
    for( l = 0; l < N - 1; l++ )
        if(k==i)k++;
        else if(l==j){
        l++;
        comp[k][l-1] = mat[k][l];
        }
        else
        comp[k][l] = mat[k][l];

/* Exibe matriz 3x3 */
printf("\nMatriz 3 X 3:\n");
for( k = 0; k < N; k++ )
{
    for( l = 0; l < N; l++ )
        printf("%d ", mat[k][l] );
    printf("\n");
}

/* Exibe matriz 2x2 */
printf("\nMatriz 2 X 2:\n");
for( k = 0; k < N - 1; k++ )
{
    for( l = 0; l < N - 1; l++ )
        printf("%d ",comp[k][l]);
    printf("\n");
}

return 0;

}

  • I did it step by step and it worked for the specific case, but when I change the i and j it doesn’t run the same way. I couldn’t see the error in the logic employed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.