Bonjour tout le monde ,
Voila ma question : je prépare un TP en c qui consiste a crée un fichier input contient la taille en premier ligne et 2 matrice d'entier dans les autres lignes
ex :
4

Matrice1
1235
5456
5428
2415

matrice2
8795
2514
6524
8562

le projet consiste à récupérer ces entier entier et faire le produit des deux matrice mais en utilisant les listes chaînées et sans tableau
le résultat doit être placée dans une autre fichier output aidez moi svp

Recommended Answers

All 8 Replies

Please post your question in English.

commented: ok :) +0

Hello everyone,
Here is my question: I prepare a TP in c which is to create an input file contains the size in the first line and 2 integer matrix in other lines
example:
4

array1
1235
5456
5428
2415

array2
8795
2514
6524
8562

the project is to recover the full and make the product of two matrix but using linked lists and not array
the result should be placed in another output file please help me

no one can help me ! :/

What code do you have so far? We don't just do people's homeworks, you have to show that your are trying to solve the problem, and ask about specific problems that you are having with your code.

commented: the code I have is too long :/ +0
Member Avatar for Mouche

You should attack this problem in multiple steps.

Here's an example of how I might do it:

1) Design a data structure to hold the matrices
2) Write code to read in the file and store data in the data structure
3) Write the matrix multiplication function
4) Write code to output the resulting matrix

For step 1, you have a constraint. You need to use a linked list.

P.S. Si tu as des questions que tu veux poser en français, tu peux m'envoyer un message.

commented: mon problème est comment je pourrai récupérer les valeurs depuis un fichier et les affecter à ma cellule :/ +0

thank you Mouche I will do as you told me :)

here is my code I want to optimize it

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



typedef struct matrice    //chaque pointeur pointe sur une liste
{
    int valeur;
    struct matrice * colonne;
    //struct matrice * ligne;
}mat;


void Insertion_colonne(mat **tete,int val)
{
    mat *fin , *p;
    p=(mat *)malloc(sizeof(mat ));     // création d'un nouveau maillon pour les colonnes
    p->valeur=val;
    p->colonne=NULL;
    p->ligne=NULL;


    if(*tete==NULL)
        *tete=p;
    else
    {
        fin=*tete;
        while(fin->colonne!=NULL){
            fin=fin->colonne;
        }
        fin->colonne=p;
    }
}
int valeur(mat *p,int postion)
{
    int i;
    i=0;
    while(i <position)
    {
        i++;
        p=p->colonne;
    }
    return p->valeur;
}

void recuperation_des_donnes(mat **l1 , mat **l2 , int *x)
{
    FILE* pFile;
    int k,i,j;
    char c;
    char * ch
    ch=malloc(255*sizeof(char));   // Allocation d'un block de 255 octets

           pFile=fopen("test.txt","r");

     if (pFile == NULL)
        perror ("Error opening file");
    else {
            fgets(ch,255,pFile);   //ch :Pointeur vers un tableau de caractères où la lecture de chaîne est copiée.
                                   //255: Le nombre maximum de caractères à copier dans ch (y compris le nul caractère de fin).
                                    //pFile:Pointeur vers un objet de fichier qui identifie un flux d'entrée

            i=0;
            j=0;
            *x=atoi(ch);    //convertir une chaine à un entier
            k=(*x)*(*x);
            mat *p=NULL;
            while((c=getc(pFile))!=EOF){
                ch[0]=c;
                ch[1]='\n';
                if((c>='0')&& (c<='9')){
                    i++;

                if(i<=k){
                    Insertion_colonne(&(*l1),atoi(ch));

                    j++;

                if (j%(*x)==0)
                {
                    j=0;

                }
            }
            else{
                    Insertion_colonne(&(*l2),atoi(ch));
                j++;
                if (j%(*x)==0)
                {
                    j=0;
                }
            }
        }
    }
  }
}


//calcule du produit des deux matrices
void produit_mat(mat *mm , mat *mt , mat ** prod, int n )
{
    int i,k,dep,produit;
    k=0;
    dep=0;
    produit=0;
    mat *mat1=mm;
    mat *mat2=mt;
    while(k<n*n){

        for (dep = 0; dep < n; ++dep)
        {produit=0;
            for ( i =k ; i < n+k; ++i)
            {
            produit=produit + valeur(m1,i)*valeur(m2,dep+(i-k)*n);

            }
        Insert_colonne(&*prod,produit);
        }

        k+=n;
}

}

//l'affichage liste chainé
void afficher_mat(mat *l,int n ){
    int i=0;
    while(l)
        {
            i++;
            printf("  %d  ",l->valeur );
            l=l->colonne;
            if(i==n)
            {
                    i=0;
                printf("\n");
            }

        }
}


 int main(int argc, char const *argv[])
{   int n;
    mat *mat_prod=NULL;
    mat *mat2=NULL;
    mat *mat1=NULL;
    mat *mataux=NULL;


    saisie_des_donnes(&mat1,&mat2,&n );
    printf("mat 1  ***************\n");
    afficher_mat(mat1,n);
    printf("mat 2  ****************\n");
    afficher_mat(mat2,n);
    produit(mat1,mat2,&mat_prod, n );
    printf("mat 3  ****************\n");
    afficher_mat(mat_prod,n);


    return 0;
}
Member Avatar for Mouche

Before you optimize it, it needs to compile and run. Have you looked at the compiler errors?

A couple hints from the compiler errors:
- You're using 'ligne' but it's commented out in the struct
- You misspelled position in valeur()
- In produit_mat() you use m1 and m2, and they aren't declared
- In produit_mat() you call Insert_colonne() which doesn't exist

Please test your code and try to fix your errors before posting. If there is a particular error you're having trouble fixing, then ask about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.