Probleme de boucle :'(

cs_paulina82 Messages postés 19 Date d'inscription vendredi 3 juin 2005 Statut Membre Dernière intervention 27 avril 2008 - 27 avril 2008 à 18:10
cs_paulina82 Messages postés 19 Date d'inscription vendredi 3 juin 2005 Statut Membre Dernière intervention 27 avril 2008 - 27 avril 2008 à 18:30
Bonjour a tous.
j'ecri un programme pour coder des caracteres. comme j'arivais pas avec strcat j'ai esayé une autre methode. c'est deja mieu mais c'est pa encor ca :( je pense qu'il y a un probleme avec ma boucle. mais je sais pas comment le coriger.
en gros mon programme consiste a coder des caracteres. le but c'est de faire la fonction 'construction_binaire' .elle doi remplire un tableau a 2 dimension. pour cela on utilise une fonction 'codage' qui rempli des colonnes. elle met des '0' du "debut" jusqu'a "k" et des "1" de "k+1" jusqu'a "fin". donc qund on execute 'construction_binaire' dans la 1er colonne je devrais avoir des '0' jusqu'a "k" et des '1' ensuite. ensuite dans la 2eme colonne on doit redecoupe la plage des '0' et des '1' et ainsi de suite dans chaque colonne tan que on puise plus decoupé.
dans mon exemple je devrait obtenir :

00000
00001
0001
0010
0011
010
011
100
101
11

mais voila ce que j'obtient :
000000000
000000001
00011111
001
001
01
01
1
1
1

si quelqu'un sais que ce qui cloche ca seré super sympa car je n'arive vraiment pas.
je vous remercie d'avance pour votre reponse

3 réponses

cs_paulina82 Messages postés 19 Date d'inscription vendredi 3 juin 2005 Statut Membre Dernière intervention 27 avril 2008
27 avril 2008 à 18:15
Oups j'ai oublié de joindre le prog :/



#include <stdio.h>
#include <math.h>
#include <string.h>
#include
#include
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include

#define L 10
#define C 10


int cpt=0;


void trier_croissant(int tab[],int taille);
int indice_coupure(int tableau[], int g, int d);
void codage(char tab[L][C],int tab_freq[],int debut,int fin,int colonne);
void initialisation(char tab[L][C]);
void construction_binaire(char tab[L][C], int tab_freq[],int debut,int fin);


void trier_croissant(int tab[],int taille){
int i,j;
int min;
int temp;



for(i=0; i<taille i="" for="" j="" min="tab[i];" if="" temp="tab[i];" tab="" int="" indice_coupure="" tableau="" g="" d="" sommeg="0;" sommed="0;" indice="" while=""><= d)
{

if (SommeG < SommeD)
{
SommeG += tableau[g];
g++;
}
else
{
SommeD += tableau[d];
d--;
}
}

indice=g-1;

return indice ;


}






void codage(char tab[L][C],int tab_freq[],int debut,int fin,int colonne){

int k,i,j;


k=indice_coupure(tab_freq, debut,fin);



for(i=debut;i<=k;i++){
tab[i][colonne]='0';
}

for(j=k+1;j<=fin;j++){
tab[j][colonne]='1';
}
/*
for (i=0;i<10;i++){
for(j=0;j<10;j++){
printf("%c",tab[i][j]);
}
printf ("\n");
}
*/




}

void initialisation(char tab[L][C]){

int i,j ;

for(i=0;i<c;i++){ for(j="0;j<L;j++){" tab[i][j]=" " ;="" }="" void="" construction_binaire(char="" tab[l][c],="" int="" tab_freq[],int="" debut,int="" fin){="" k,i,j;="" tab_k[c];="" nb_element="1;" tab_k[0]="debut;" tab_k[1]="fin;" for="" (i="1;i<10;i++){" if((tab_k="" [j+1]="" -="" tab_k[j])="">= 1){
codage( tab,tab_freq, tab_k[j],tab_k[j+1],i);
k=indice_coupure(tab_freq, tab_k[j],tab_k[j+1]);
tab_k[nb_element+1]=k;

}

}
nb_element++; trier_croissant(tab_k,nb_element+2);

}

}




int main (int argc, char ** argv){
int tab_frequence[10]={1,2,3,4,5,6,7,8,9,10};
char tab[L][C];
int i,j;

initialisation(tab);


construction_binaire( tab, tab_frequence,0,9);

for(i=0;i<10;i++){

for(j=0;j<10;j++){
printf("%c", tab[i][j]);
}
printf("\n");
}
printf("\n");

return (EXIT_SUCCESS);

}



</c;i++){></taille></stdlib.h></errno.h></fcntl.h></string.h></math.h></stdio.h>
0
cs_paulina82 Messages postés 19 Date d'inscription vendredi 3 juin 2005 Statut Membre Dernière intervention 27 avril 2008
27 avril 2008 à 18:24
je m'excuse pour le format. c'est a cause de knopix qui a enlevé tout les passages a la ligne. je redone le code :

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include<sys/stat.h>

#define L 10
#define C 10

int cpt=0;

void trier_croissant(int tab[],int taille);
int indice_coupure(int tableau[], int g, int d);
void codage(char tab[L][C],int tab_freq[],int debut,int fin,int colonne);
void initialisation(char tab[L][C]);
void construction_binaire(char tab[L][C], int tab_freq[],int debut,int fin);

void trier_croissant(int tab[],int taille){
  int i,j;
  int min;
  int temp;
 

  for(i=0; i<taille ; i++){
 
    for(j=i;j<taille; j++){
      min=tab[i];

      if(tab[j]<min){
    min=tab[j];
           temp=tab[i];
        tab[i]=min;
    tab[j] = temp;

  
      }
    }
   
     
  }
}

int indice_coupure(int tableau[], int g, int d)
{
 
  int  SommeG = 0;
  int SommeD = 0;
  int indice;
 
  while (g <= d)
    {
     
      if (SommeG < SommeD)
    {
      SommeG += tableau[g];
      g++;         
    }     
      else
    {
      SommeD += tableau[d];
      d--;     
    }    
    }      
    
  indice=g-1;
       
  return indice ;
    
 
}

void codage(char tab[L][C],int tab_freq[],int debut,int fin,int colonne){
 
  int k,i,j;

 
  k=indice_coupure(tab_freq, debut,fin);

  
  for(i=debut;i<=k;i++){
    tab[i][colonne]='0';
  }

  for(j=k+1;j<=fin;j++){
    tab[j][colonne]='1';
  }
  /*
    for (i=0;i<10;i++){
    for(j=0;j<10;j++){
    printf("%c",tab[i][j]);
    }
    printf ("\n");
    }
  */
  
 
 
 
}

void initialisation(char tab[L][C]){

  int i,j ;
 
  for(i=0;i<C;i++){
    
    for(j=0;j<L;j++){
      tab[i][j]=' ';
    }
  
  }
   
}

void construction_binaire(char tab[L][C], int tab_freq[],int debut,int fin){

  int k,i,j;
 

  int tab_k[C];
  int nb_element=1;
  
 
  tab_k[0]=debut;
  tab_k[1]=fin;
 
  for (i=1;i<10;i++){
    for(j=0;j<nb_element;j++){
      if((tab_k [j+1] - tab_k[j]) >= 1){
    codage( tab,tab_freq, tab_k[j],tab_k[j+1],i);
        k=indice_coupure(tab_freq, tab_k[j],tab_k[j+1]);
    tab_k[nb_element+1]=k;

      }
         
       }
         nb_element++;    trier_croissant(tab_k,nb_element+2);
   
  }

}

int main (int argc, char ** argv){
  int tab_frequence[10]={1,2,3,4,5,6,7,8,9,10};
  char tab[L][C];
  int i,j;
  
  initialisation(tab);
 
 construction_binaire( tab, tab_frequence,0,9);
 
     for(i=0;i<10;i++){
    
  for(j=0;j<10;j++){
  printf("%c", tab[i][j]);
  }
  printf("\n");
  }
  printf("\n");
 
  return (EXIT_SUCCESS);

}
0
cs_paulina82 Messages postés 19 Date d'inscription vendredi 3 juin 2005 Statut Membre Dernière intervention 27 avril 2008
27 avril 2008 à 18:30
 je cvous redone le resultat :
ce que je devrais avoir :
00000
00001
0001
0010
0011
010
011
100
101
11

mais voila ce que
j'obtient :
000000000
000000001
00011111
001
001
01
01
1
1
1
0
Rejoignez-nous