Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1051 connectés 

  FORUM HardWare.fr
  Programmation
  C

  programme C

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

programme C

n°2159434
sabeurios8​9
Posté le 07-10-2012 à 12:02:35  profilanswer
 

Bonjour,
pouvez vous m'aider à écrire un programme c qui permet d'accepter une suite de valeurs,applique un tri MIN_MAX et affiche le resultat
merci

mood
Publicité
Posté le 07-10-2012 à 12:02:35  profilanswer
 

n°2159462
Profil sup​primé
Posté le 07-10-2012 à 16:58:19  answer
 

Tu connais l'instruction "goto" ?
C'est quoi un tri Min_Max ?

n°2159483
sabeurios8​9
Posté le 07-10-2012 à 21:20:16  profilanswer
 

non je ne conais pas GOTO je suis encors débutant...tri min max: son principe consiste à chercher à chaque itération l'indice du min et l'indice du max et permuter avec les éléments  
i et n-i-1.

n°2159485
Profil sup​primé
Posté le 07-10-2012 à 22:56:11  answer
 

C'es quoi i, c'est sensé représenter quoi ?

n°2159491
Profil sup​primé
Posté le 07-10-2012 à 23:57:23  answer
 

J'avais fait ça : c'est plus intéressant.
 

Code :
  1. #include <stdio.h>
  2.  
  3. int main (void) {
  4.  
  5.  float tab_elements[10] = { 2.0, 4.0, 6.0, 8.0, 5.5, 1.0, 3.0, 5.0, 7.0, 9.0};
  6.  
  7.  float min, max;
  8.  
  9.  min = 100.0;
  10.  max = 0.0;
  11.  
  12.  int b_inf, b_sup;
  13.  
  14.  b_inf = 0;
  15.  b_sup = 9;
  16.  
  17.  int index;
  18.  
  19.  int pos_min, pos_max;
  20.  
  21.  float buffer;
  22.  
  23.  while (b_inf < b_sup) {
  24.    
  25.    for (index = b_inf; index <= 9; index++) {
  26.      if (tab_elements[index] < min) {
  27.     min = tab_elements[index];
  28.     pos_min = index;
  29.      }
  30.    }
  31.    
  32.    for (index = b_sup; index >= 0; index--) {
  33.      if (tab_elements[index] > max) {
  34.     max = tab_elements[index];
  35.     pos_max = index;
  36.      }
  37.    }
  38.  
  39.    buffer = tab_elements[b_inf];
  40.    tab_elements[b_inf] = tab_elements[pos_min];
  41.    tab_elements[pos_min] = buffer;
  42.    
  43.    buffer = tab_elements[b_sup];
  44.    tab_elements[b_sup] = tab_elements[pos_max];
  45.    tab_elements[pos_max] = buffer;
  46.    
  47.    b_inf++;
  48.    b_sup--;
  49.  
  50.  }
  51.  
  52.  for (index = 0; index <= 9; index++) {
  53.    printf("element %d = %f\n", index, tab_elements[index]);
  54.  }
  55. }


 
Ca donne ça :  
 

element 0 = 1.000000
element 1 = 2.000000
element 2 = 4.000000
element 3 = 6.000000
element 4 = 8.000000
element 5 = 3.000000
element 6 = 5.000000
element 7 = 7.000000
element 8 = 9.000000
element 9 = 5.500000


 
Ca marche po quoa. !   :lol:

n°2159492
Profil sup​primé
Posté le 08-10-2012 à 00:19:29  answer
 

je viens avec mes correction, ça a l'air de fonctionner.
 

Code :
  1. #include <stdio.h>
  2.  
  3. int main (void) {
  4.  
  5.  float tab_elements[10] = { 2.0, 4.0, 6.0, 8.0, 5.5, 1.0, 3.0, 5.0, 7.0, 9.0};
  6.  
  7.  int b_inf, b_sup;
  8.  
  9.  b_inf = 0;
  10.  b_sup = 9;
  11.  
  12.  
  13.  int index;
  14.  
  15.  float min, max;
  16.    
  17.    min = 100.0;
  18.    max = 0.0;
  19.  
  20.  
  21.  while (b_inf < 10/2+1) {
  22.      
  23.    int pos_min, pos_max;
  24.  
  25.    for (index = b_inf; index <= 9; index++) {
  26.      if (tab_elements[index] < min) {
  27.     min = tab_elements[index];
  28.     pos_min = index;
  29.  
  30.      }
  31.    }
  32.    
  33.    for (index = b_sup; index >= 0; index--) {
  34.      if (tab_elements[index] > max) {
  35.     max = tab_elements[index];
  36.     pos_max = index;
  37.  
  38.      }
  39.    }
  40.    
  41.    float buffer;
  42.  
  43.    buffer = tab_elements[b_inf];
  44.    tab_elements[b_inf] = tab_elements[pos_min];
  45.    tab_elements[pos_min] = buffer;
  46.    
  47.    buffer = tab_elements[10-b_inf-1];
  48.    tab_elements[10-b_inf-1] = tab_elements[pos_max];
  49.    tab_elements[pos_max] = buffer;
  50.    
  51.    b_inf++;
  52.    b_sup--;
  53.    min = tab_elements[b_inf];
  54.    max = tab_elements[10-b_inf-1];
  55.  
  56.  }
  57.  
  58.  for (index = 0; index <= 9; index++) {
  59.    printf("element %d = %f\n", index, tab_elements[index]);
  60.  }
  61. }


element 0 = 1.000000
element 1 = 2.000000
element 2 = 3.000000
element 3 = 4.000000
element 4 = 5.000000
element 5 = 5.500000
element 6 = 6.000000
element 7 = 7.000000
element 8 = 8.000000
element 9 = 9.000000


Message édité par Profil supprimé le 08-10-2012 à 00:25:46
n°2159493
WiiDS
20 titres en GC, 0 abandon, 0 DQ
Posté le 08-10-2012 à 01:30:56  profilanswer
 


 
http://imgs.xkcd.com/comics/goto.png


---------------
"I can cry like Roger. It's just a shame I can't play like him" - Andy Murray, 2010
n°2159494
Profil sup​primé
Posté le 08-10-2012 à 01:41:34  answer
 

Bien, bon, bah, voilà, la même chose avec Ada : ça m'a permis de voir que l'ordre des opération n'était pas la même avec Ada et C.
 

Code :
  1. with Ada.Text_Io;
  2. use Ada;
  3. procedure Main is
  4.  
  5.  
  6.  
  7.   Tab_Elements : array (1..10) of Float := (2.0, 4.0, 6.0, 8.0, 5.5, 1.0, 3.0, 5.0, 7.0, 9.0);
  8.  
  9.   B_Inf : Positive := 1;
  10.   B_Sup : Positive := 10;
  11.  
  12.   Min : Float := 10.0;
  13.   Max : Float := 0.0;
  14.  
  15.  
  16.   Pos_Min, Pos_Max : Positive;  
  17.  
  18. begin
  19.  
  20.  
  21.  
  22.   while B_Inf <= 10/2+1 loop      
  23.  
  24.      begin
  25.      for Index in b_Inf..Tab_Elements'last loop
  26.         if Tab_Elements(Index) < Min then
  27.            Pos_Min := Index;
  28.            Min := Tab_Elements(Index);
  29.         end if;
  30.      end loop;
  31.     
  32.      for Index in reverse Tab_Elements'first..B_Sup loop
  33.         if Tab_Elements(Index) > Max then
  34.            Pos_Max := Index;
  35.            Max := Tab_Elements(Index);
  36.         end if;
  37.      end loop;
  38.     
  39.      declare
  40.         Buffer : Float := 0.0;
  41.      begin
  42.         Buffer := Tab_Elements(B_Inf);
  43.         Tab_Elements(B_Inf) := Tab_Elements(Pos_Min);
  44.         Tab_Elements(Pos_Min) := Buffer;
  45.         
  46.         Buffer := Tab_Elements(10-(B_Inf-1));
  47.         Tab_Elements(10-(B_Inf-1)) := Tab_Elements(Pos_Max);
  48.         Tab_Elements(Pos_Max) := Buffer;
  49.      end;
  50.      end;
  51.      
  52.      B_Inf := B_Inf + 1;
  53.      B_Sup := B_Sup - 1;      
  54.      Min := Tab_Elements(B_Inf);
  55.      Max := Tab_Elements(10-(B_Inf-1));                
  56.   end loop;
  57.  
  58.  
  59.   for Index in Tab_Elements'Range loop
  60.      Text_Io.Put_Line("Element " & Positive'Image(Index) & " = " & Float'Image(Tab_Elements(Index)));
  61.   end loop;
  62.  
  63.  
  64. end Main;


 
Même résultat.

n°2159555
Profil sup​primé
Posté le 08-10-2012 à 17:20:22  answer
 

Bon voici un truc un peut plus propre parait-il avec C.
 

Code :
  1. #include <stdio.h>
  2.  
  3. int main (void) {
  4.  
  5.  int maxi(float tab[], int b_inf, int b_sup) {
  6.    
  7.    int max = b_inf;
  8.    int i;
  9.    
  10.    for (i = b_inf+1; i<=b_sup; i++) {
  11.      if (tab[i] > tab[max])
  12.     max = i;
  13.    }
  14.    
  15.    return max;
  16.  }
  17.  
  18.  
  19.  int mini(float tab[], int b_inf, int b_sup) {
  20.    
  21.    int min = b_inf;
  22.    int i;
  23.    
  24.    for (i = b_inf+1; i<=b_sup; i++) {
  25.      if (tab[i] < tab[min])
  26.     min = i;
  27.    }
  28.    
  29.    return min;
  30.  }
  31.  
  32.  
  33.  void permute(float * tab, int i, int j) {
  34.    float buffer;    
  35.    
  36.    buffer = tab[i];
  37.    tab[i] = tab[j];
  38.    tab[j] = buffer;
  39.  }
  40.  
  41.    
  42.  float tab_elements[10] = {1.0, 4.0, 11.0, 8.0, 10.0, 2.0, 3.0, 5.0, 7.0, 9.0};
  43.  
  44.  int i;
  45.  
  46.  for (i = 0; i <= 9; i++) {
  47.        
  48.    permute(tab_elements, mini(tab_elements, i, 10-i-1), i);
  49.    permute(tab_elements, maxi(tab_elements, i, 10-i-1), 10-i-1);    
  50.    
  51.  
  52.  }
  53.  
  54.  for (i = 0; i <= 9; i++) {
  55.    printf("element %d = %f\n", i, tab_elements[i]);
  56.  }
  57.  
  58. }


 
Résultat.....

element 0 = 11.000000
element 1 = 10.000000
element 2 = 9.000000
element 3 = 8.000000
element 4 = 7.000000
element 5 = 5.000000
element 6 = 4.000000
element 7 = 3.000000
element 8 = 2.000000
element 9 = 1.000000


 
C'est à l'envers... bon la y a probablement juste un petite question de logique à appliquer.


Message édité par Profil supprimé le 08-10-2012 à 17:27:26
n°2159567
Profil sup​primé
Posté le 08-10-2012 à 18:44:00  answer
 

Sensiblement le même code avec Ada, marche pas.
 

Code :
  1. with Ada.Text_Io;
  2. use Ada;
  3. procedure Main is
  4.  
  5.   type Tab_Elements_Type is array (Positive range <> ) of Float;
  6.  
  7.   function Mini(Tab : Tab_Elements_Type; B_Inf, B_Sup : positive) return Positive is
  8.      Min : Positive := B_Inf;
  9.   begin
  10.      for I in B_Inf+1..B_Sup loop
  11.      if Tab(B_Inf) > Tab(I) then
  12.         Min := I;
  13.      end if;
  14.      end loop;
  15.      return Min;
  16.   end Mini;
  17.  
  18.   function Maxi(Tab : Tab_Elements_Type; B_Inf, B_Sup : positive) return Positive is
  19.      Max : Positive := B_Inf;
  20.   begin
  21.      for I in B_Inf+1..B_Sup loop
  22.      if Tab(B_Inf) < Tab(I) then
  23.         Max := I;
  24.      end if;
  25.      end loop;
  26.      return Max;
  27.   end Maxi;
  28.      
  29.  
  30.   procedure Permute (Tab : access Tab_Elements_Type; I, J : Positive) is
  31.      Buffer : Float;
  32.   begin
  33.      Buffer := Tab(I);
  34.      Tab(I) := Tab(J);
  35.      Tab(J) := Buffer;
  36.   end Permute;
  37.  
  38.  
  39.        
  40.   Tab_Elements : aliased Tab_Elements_Type := (1.0, 4.0, 11.0, 8.0, 10.0, 2.0, 3.0, 5.0, 7.0, 9.0);
  41.                          
  42. begin
  43.  
  44.  
  45.  
  46.   for I in Tab_Elements'range loop
  47.      
  48.      Permute(Tab_Elements'access, Mini(Tab_Elements, I, Tab_Elements'Length-(I-1)), Tab_ELements'Length-(I-1));
  49.      
  50.      Permute(Tab_Elements'access, Maxi(Tab_Elements, I, Tab_Elements'Length-(I-1)), I);
  51.   end loop;
  52.  
  53.  
  54.   for Index in Tab_Elements'Range loop
  55.      Text_Io.Put_Line("element" & Positive'Image(Index) & " =" & Float'Image(Tab_Elements(Index)));
  56.   end loop;
  57.  
  58.  
  59. end Main;


 
Résultat :

element 1 = 1.00000E+00
element 2 = 3.00000E+00
element 3 = 4.00000E+00
element 4 = 7.00000E+00
element 5 = 2.00000E+00
element 6 = 8.00000E+00
element 7 = 9.00000E+00
element 8 = 1.10000E+01
element 9 = 5.00000E+00
element 10 = 1.00000E+01

mood
Publicité
Posté le 08-10-2012 à 18:44:00  profilanswer
 

n°2159575
theshockwa​ve
I work at a firm named Koslow
Posté le 08-10-2012 à 20:45:58  profilanswer
 

[:lol wut]
 
Résoudre les problèmes à la place des étudiants, c'est hors charte ... Mais je crois que faire cet éxercice t'a fait le plus grand bien, jovalise [:haha]


---------------
last.fm
n°2159582
sabeurios8​9
Posté le 08-10-2012 à 22:22:03  profilanswer
 

en cours d'essai et merci bien


Message édité par sabeurios89 le 08-10-2012 à 23:04:32
n°2159630
Profil sup​primé
Posté le 09-10-2012 à 12:52:51  answer
 

Correction du code Ada.
 
Dans mini et maxi, on compare tab(min) ou tab(max) avec tab(i) au lieu de tab(b_inf) avec tab(i).
 

Code :
  1. with Ada.Text_Io;
  2. use Ada;
  3. procedure Main is
  4.  
  5.   type Tab_Elements_Type is array (Natural range <> ) of Float;
  6.  
  7.   function Mini(Tab : Tab_Elements_Type; B_Inf, B_Sup : Natural) return Natural is
  8.      Min : Natural := B_Inf;
  9.   begin
  10.      for I in B_Inf+1..B_Sup loop
  11.      if Tab(Min) > Tab(I) then
  12.         Min := I;
  13.      end if;
  14.      end loop;
  15.      return Min;
  16.   end Mini;
  17.  
  18.   function Maxi(Tab : Tab_Elements_Type; B_Inf, B_Sup : Natural) return Natural is
  19.      Max : Natural := B_Inf;
  20.   begin
  21.      for I in B_Inf+1..B_Sup loop
  22.      if Tab(Max) < Tab(I) then
  23.         Max := I;
  24.      end if;
  25.      end loop;
  26.      return Max;
  27.   end Maxi;
  28.      
  29.  
  30.   procedure Permute (Tab : access Tab_Elements_Type; I, J : Natural) is
  31.      Buffer : Float;
  32.   begin
  33.      Buffer := Tab(I);
  34.      Tab(I) := Tab(J);
  35.      Tab(J) := Buffer;
  36.   end Permute;
  37.  
  38.  
  39.        
  40.   Tab_Elements : access Tab_Elements_Type := new Tab_Elements_Type(0..9);
  41.                          
  42. begin
  43.  
  44.   Tab_Elements.all := (1.0, 4.0, 6.0, 8.0, 10.0, 2.0, 3.0, 5.0, 7.0, 9.0);
  45.        
  46.   for I in Tab_Elements'Range loop
  47.      
  48.      Permute(Tab_Elements, Mini(Tab_Elements.all, I, Tab_Elements'Length-I-1), Tab_ELements'Length-I-1);
  49.      Permute(Tab_Elements, Maxi(Tab_Elements.all, I, Tab_Elements'Length-I-1), I);
  50.   end loop;
  51.      
  52.   for Index in Tab_Elements'Range loop
  53.      Text_Io.Put_Line("element" & Natural'Image(Index) & " =" & Float'Image(Tab_Elements(Index)));
  54.   end loop;      
  55. end Main;


Message édité par Profil supprimé le 09-10-2012 à 12:54:30
n°2159632
Profil sup​primé
Posté le 09-10-2012 à 12:57:28  answer
 

theshockwave a écrit :

[:lol wut]
 
Résoudre les problèmes à la place des étudiants, c'est hors charte ... Mais je crois que faire cet éxercice t'a fait le plus grand bien, jovalise [:haha]


 
Non, mais attends, j'ai résolu un problème dans la cat C, alors que j'ai jamais pris de cours de C.
En plus ce avant l'auteur du topic.
Soit je suis surdoué.
Soit je suis surdoué.
 
C'est vrai que j'ai pris mon pied à tourner mes code dans tous les sens pour que finalement, ça marche.
 
Et toi t'as foutu quoi pendant ce temps ?

n°2159643
theshockwa​ve
I work at a firm named Koslow
Posté le 09-10-2012 à 14:35:01  profilanswer
 


 
Je suis arrivé après la tempête, j'ai un travail qui empiète sur mon temps hfr [:petrus75]


---------------
last.fm

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  programme C

 

Sujets relatifs
Programme C++ / Fortran sous Visual 6 - Convention d'appelProgramme pour ouvrir une fenêtre miniature en fonction de la T°C
pouvez vous m'aidez a crée un programme en C[C] programme cherche les racines des polynomes
Dessiner graphe via un programme C++Programme en C qui demande la saisie du JJ/MM/AAAA
Programme C++ / ecriture de fichier Access / 'ConflictTables'un programme en python appelle une fonction C .. ?
Aide pour un programme en C/C++ (compression en rar) 
Plus de sujets relatifs à : programme C


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR