Transposition d’une matrice en C

Dans ce tutoriel nous allons découvrir comment transposer une matrice. Il est obtenu en inter-changeant des lignes et des colonnes d’une matrice. Par exemple, considérons la matrice 3 X 2 suivante:
 


 
 

Programme C pour transposer une matrice
#include <stdio.h>

void main()
{
    int matrice[10][10], transpose[10][10];
    int i,j,n,m;
	
    printf("Entrez le nombre de lignes et de colonnes:\n");
    scanf("%d%d",&n,&m);
	
    printf("Entrez les éléments de la matrice\n");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d", &matrice[i][j]);
        }
    }

    printf("La matrice:\n");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            printf("%d\t", matrice[i][j]);
        }
        printf("\n");
    }
    //Transposition
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            transpose[j][i] = matrice[i][j];
        }
    }

    printf("La transposition de la matrice est:\n");
    for(i = 0; i < m; i++)
    {
        for(j = 0; j < n; j++)
        {
            printf("%d\t", transpose[i][j]);
        }
        printf("\n");
    }
}

La sortie :
 

 
 

2 réflexions sur “Transposition d’une matrice en C

  • janvier 18, 2021 à 10:08 am
    Permalien

    Bonjour,
    La transposé de votre matrice en image en introduction est fausse. On devrait plutôt avoir (1 3 5; 2 4 6)

    Cordialement

    Répondre
    • janvier 27, 2021 à 12:14 pm
      Permalien

      Merci pour cette remarque, c’est corrigé 🙂

      Répondre

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *