0

Can someone help me please find the ploblem with this program ? I know it's a question of a misssing pointer but i don't know where to put it ..

#include<stdio.h>
void remplissage_matrix(char M[50][50],int l , int c);
void remplissage_elements(int l, int c ,char M[50][50],char elements[100],int *n);
void remplissage_histo(int histo[100],char elements[100],int *n);
int main()
{
    int l,c,n,m ;
    char M[50][50],elements[100];
    int histo[100];

     printf("Saisir le nombre des lignes de la matrice \n");
    scanf("%d",&l);

    printf("Saisir le nombre des colonnes de la matrice \n");
    scanf("%d",&c);

remplissage_matrix( M, l , c);
remplissage_elements(l,c,M,elements,&m);
remplissage_histo( histo,elements,&m);
return 0 ;
}
void remplissage_matrix(char M[50][50],int l , int c)
{
    int i,j,k,x=0,n;

    for (i=0;i<l;i++)
{
    for (j=0;j<c;j++)
{
    printf("Saisir M[%d][%d]",i,j);
    fflush(stdin);
    scanf("%c",&M[i][j]);

  }}
   for (i=0;i<l;i++)
     {
     for (j=0;j<c;j++)

     printf("M[%d,%d]= %c \n" ,i,j,M[i][j]);
  }

}
void remplissage_elements(int l, int c ,char M[50][50] ,char elements[100],int *n)
{
int i,j,k,x;

    for (i=0;i<l;i++)
    {
        for(j=0;j<c;j++)
        {
elements[x++]= M[i][j];
        }
    }
    *n= l * c;

    for(i=0; i < *n; i++)
   {
      for(j=i+1; j < *n; )
      {
         if(elements[j] == elements[i])
         {
            for(k=j; k < *n;k++)
            {
               elements[k] = elements[k+1];
            }
            *n--;
         }
         else {
            j++;
         }
      }
   }

   for(i=0; i < *n; i++)
   {
      printf("Elements[%d] est %c \n",i, elements[i]);
   }
   }

void remplissage_histo(int histo[100],char elements[100], int *n)
{
    int  i,j,x,count;
    for(i = 0; i < *n; i++) {
        count = 1;
        for(j = i+1; j < *n; j++) {
            if(elements[i]==elements[j]) {
               histo[j] = 0;
                count++;
            }
        }
        if(histo[i]!=0) {
            histo[i] = count;
        }
    }

    for(i = 0; i<*n; i++) {
        if(histo[i] != 0) {
            printf("Element %c : Count %d\n", elements[i], histo[i]);
        }
    }

    for(x = 0; x < *n; x++)
    {
         if( histo[x] > 0){
            printf("%c",&elements[x]);
            for(i = 1; i <= histo[x]; ++i){
                printf("*");
         }
         printf("\n");
         }
    }
    }
3
Contributors
2
Replies
22
Views
4 Months
Discussion Span
Last Post by StuXYZ
Featured Replies
  • 1

    I agree a bit with rproffitt, but without actually running the code I noticed the following: line 51 : `elements[x++]= M[i][j];` x is not initialized. Might and might not start as 0. line 64 `elements[k] = elements[k+1];` In the first parse if elements[i] == elements [j] then this line sets … Read More

1

I agree a bit with rproffitt, but without actually running the code I noticed the following:

line 51 : elements[x++]= M[i][j]; x is not initialized. Might and might not start as 0.

line 64 elements[k] = elements[k+1]; In the first parse if elements[i] == elements [j] then this line sets elements[k] to be element[k+1] BUT the loop is for(k=j; k < *n;k++) which means that you have stepped passed the end of the array.

Beyond that I am not sure what you want the code to do -- so fix this simple stuff and if you can add an example of what the code should do and what it does do, e.g. for a 3x3 matrix or something small, that would help.

Edited by StuXYZ

Comments
Nice find. I have a story about uninitialized variables. Big money, adventures were had.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.