Bonjour !
J'ai un petit souci pour finir cet exo : Le principe est simple, l'utilisateur rentre des chiffres, le programme affiche ces chiffres, les mélange et les reaffiche mélangés.
J'ai donc eu l'idée d'utiliser un tableau et voici ce que ca donne :
<code type="cpp">
#include <iostream>
using namespace std;
int main ()
{
int tableau[100], n, k;
char reponse;
void afficherTab(int Tableau[100], int n);
void melangeTab (int n, int Tableau[100]);
do
{
n=0;
cout<<"Entrez un nombre: ";
cin>>k;
if (k>=0)
{
tableau[n]=k;
n++;
}
while ((k>=0) && (n<=99))
{
cout<<"Entrez un nombre: ";
cin>>k;
if (k>=0)
{
tableau[n]=k;
n++;
}
}
cout<<"Vous avez saisi : ";
afficherTab(tableau,n);
cout<<"\nJe melange..."<<endl;
melangeTab(n,tableau);
afficherTab(tableau, n);
cout<<"Recommencer ? (o/n) ";
cin>>reponse;
}
while (reponse=='o');
}
</code>
procédure afficherTab :
<code type="cpp">
#include<iostream.h>
void afficherTab(int tableau[100], int n)
{
int i;
for (i=0; i<n; i++)
{
cout<<tableau[i]<<" ";
}
}
</code>
procedure melangeTab
<code type="cpp">
#include<iostream>
using namespace std;
void melangeTab (int n, int Tableau[100])
{
int hasard(int min, int max);
int random1, random2, tampon,i;
for (i=0; i<n; i++)
{
random1=((n-1) * hasard(0, n-1));
random2=((n-1) * hasard(0, n-1));
tampon=Tableau[random2];
Tableau[random1]=Tableau[random2];
Tableau[random2]=tampon;
}
}
</code>
fonction random :
<code type="cpp">
#include <iostream>
#include <cstdlib>
#include <time.h>
int hasard(int min, int max)
{
return (int) (min + ((float) rand() / RAND_MAX * (max - min + 1)));
}
</code>
le probleme, c'est que ca me donne ca :
<image>http://img12.imageshack.us/img12/3259/sanstitrecf.png</image>
une' idée ?