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

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;

}
    
asked by anonymous 06.07.2018 / 05:39

2 answers

0

Basically, the copy / convert code between the arrays 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 the solution to the problem:

#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 at Ideone.com

    
06.07.2018 / 11:15
-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;

}

    
06.07.2018 / 15:48