Définir une Matrice en C

Résolu
Ashretor Messages postés 6 Date d'inscription vendredi 13 novembre 2009 Statut Membre Dernière intervention 13 novembre 2009 - 13 nov. 2009 à 10:25
Ashretor Messages postés 6 Date d'inscription vendredi 13 novembre 2009 Statut Membre Dernière intervention 13 novembre 2009 - 13 nov. 2009 à 15:29
Bonjour, je travaille sur DevC++, en C:
Je cherche à créer une matrice contenant et affichant:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

J'ai déjà créé le type de matrice, la seule chose qui me manque c'est comment définir les nombres ci-dessus dans ma matrice.
J'ai fait:
Matrice mat={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};

(Matrice est mon type, comme int, char ou float)

Cette solution fonctionne-t-elle ? J'en ai pas l'impression ^^

13 réponses

cptpingu Messages postés 3837 Date d'inscription dimanche 12 décembre 2004 Statut Modérateur Dernière intervention 28 mars 2023 123
13 nov. 2009 à 10:55
Ceci fonctionne chez moi:
#include <stdio.h>

#define maxM 4
typedef float Matrice[maxM][maxM];

void Affichermat(Matrice val)
{
  int x;
  int y;
  for(x = 0; x < maxM; ++x)
  {
    for(y = 0; y < maxM; ++y)
      printf("%f ", val[x][y]);
    printf("\n");
  }
}

int main(void)
{
  Matrice mat =
    {
      {1,2,3,4},
      {5,6,7,8},
      {9,10,11,12},
      {13,14,15,16}
    };

  Affichermat(mat);
  return 0;
}
2
Rejoignez-nous