1 #include <iostream> 2 #include <cstdlib> 3 using namespace std; 4 5 /* Fonction raz: Remet le tableau a zero 6 Parametres: A = tableau, 7 taille = taille du tableau 8 */ 9 void raz(int A[], int taille) { 10 for(int i=0;i<taille;i++) { 11 A[i]=0; 12 } 13 } 14 15 /* Fonction imp: imprime le tableau complet 16 Parametres: A = tableau 17 taille = taille du tableau 18 19 */ 20 void imp(int A[], int taille) { 21 for (int i=0;i<taille;i++) { 22 cout << A[i]; 23 }; 24 cout << "\n"; 25 } 26 27 /* Fonction cella: renvoie une CELLule du tableau choisie Aleatoirement, sous forme de lvalue 28 Parametres: A = tableau 29 taille = taille du tableau 30 renvoie: la case du tableau 31 */ 32 33 int& cella(int A[], int taille) { 34 int z; 35 z=rand() % taille; 36 return A[z]; 37 } 38 39 /* Programme principal */ 40 int main(int argc, char* argv[]) { 41 42 /* Teste les erreurs de maniere exhaustive (j'espere...) */ 43 if (argc==1) 44 { 45 cerr << "ERREUR - Vous n'avez pas donne la taille du tableau !!!\n"; 46 return 1; 47 } 48 if (argc > 2) 49 { 50 cerr << "ERREUR - Les parametres suivants ne servent a rien:\n"; 51 for (int i=2; i<argc; i++) 52 { 53 cerr << argv[i] << '\n'; 54 } 55 return 2; 56 } 57 58 int taille = atoi(argv[1]); 59 if (taille <=0) 60 { 61 cerr << "ERREUR - La taille doit etre un nombre positif (et pas " << taille << ")\n"; 62 return 3; 63 } 64 65 // On utilise malloc 66 int* tableau = (int*) malloc(taille * sizeof(int)); 67 if (tableau == NULL) 68 { 69 cerr << "ERREUR - Allocation memoire impossible\n"; 70 return 4; 71 } 72 73 // Initialise le generateur aleatoire 74 srand(time(NULL)); 75 76 // Initialise le tableau de maniere aleatoire 77 // On choisit une case au hasard et on met 1 dedans 78 // On le fait tourner TAILLE/2 fois, tout ne sera donc pas initialise 79 for(int i=0;i<taille/2;++i) { 80 cella(tableau,taille) = 1; 81 } 82 83 // imprime le resultat 84 imp(tableau,taille); 85 cout << endl; 86 87 free(tableau); 88 89 return 0; 90 } 91