@Yarflam
Les globales c'est le mal, connu comme phrase
@polo29
T'appelles sous programme une fonction?
Les globales c'est le mal, connu comme phrase
@polo29
T'appelles sous programme une fonction?
graphe[0][0] =0;graphe[0][1] =16;graphe[0][2] =10;graphe[0][3] =4;graphe[0][4] =0;graphe[0][5] =0;graphe[0][6] =0;graphe[0][7] =0; graphe[1][0] =0;graphe[1][1] =0;graphe[1][2] =0;graphe[1][3] =0;graphe[1][4] =0;graphe[1][5] =0;graphe[1][6] =4;graphe[1][7] =0; graphe[2][0] =0;graphe[2][1] =0;graphe[2][2] =0;graphe[2][3] =0;graphe[2][4] =12;graphe[2][5] =0;graphe[2][6] =21;graphe[2][7] =0; graphe[3][0] =0;graphe[3][1] =0;graphe[3][2] =0;graphe[3][3] =0;graphe[3][4] =10;graphe[3][5] =0;graphe[3][6] =0;graphe[3][7] =0; graphe[4][0] =0;graphe[4][1] =0;graphe[4][2] =0;graphe[4][3] =0;graphe[4][4] =0;graphe[4][5] =3;graphe[4][6] =0;graphe[4][7] =0; graphe[5][0] =0;graphe[5][1] =0;graphe[5][2] =0;graphe[5][3] =0;graphe[5][4] =0;graphe[5][5] =0;graphe[5][6] =3;graphe[5][7] =5; graphe[6][0] =0;graphe[6][1] =0;graphe[6][2] =0;graphe[6][3] =0;graphe[6][4] =0;graphe[6][5] =0;graphe[6][6] =0;graphe[6][7] =3; graphe[7][0] =0;graphe[7][1] =0;graphe[7][2] =0;graphe[7][3] =0;graphe[7][4] =0;graphe[7][5] =0;graphe[7][6] =0;graphe[7][7] =0;
matrice = [ [0,0,0,0,4,8], [7,4,2,0,0,0] ];
#include <iostream> using namespace std; int main() { struct arrays { int x[3]; int y[3]; int z[3]; }; arrays vec_2 = { {1,2,3}, {4,5,6}, {7,8,9} }; cout << vec_2.x[0]; return 0; }
cout << vec_2.x[0]; // Mauvaise solution cout << vec_2[0][0]; // Bonne solution mais comment faire ?!
int main(void) { int i, j; int matrice[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (i=0; i<3; i++) for (j=0; j<3; j++) printf("%d\n", matrice[i][j]); /* remplacer printf par cout pour le C++ */ return 0; }
#include <iostream> using namespace std; #define TailleMax 100 int PoidsCumul[TailleMax]; int CheminPlusCourt[TailleMax]; int graphe[8][8] = { { 0,16,10, 4, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 4, 0}, { 0, 0, 0, 0,12, 0,21, 0}, { 0, 0, 0, 0,10, 0, 0, 0}, { 0, 0, 0, 0, 0, 3, 0, 0}, { 0, 0, 0, 0, 0, 0, 3, 5}, { 0, 0, 0, 0, 0, 0, 0, 3}, { 0, 0, 0, 0, 0, 0, 0, 0} }; int nbS; int AfficheGraphe(); int Affichetableau(); int CalcPoidsCumul(); int DefineChemin(); int AppartVoisins(int x, int y); //---------------------------------------------------// int main() { int k; nbS=8; AfficheGraphe(); CalcPoidsCumul(); cout<<"le temps le plus court est "<<CalcPoidsCumul()<<" jours \n "; DefineChemin(); return 0; } //---------------------------------------------------// int AppartVoisins(int x, int y) { int voisin; voisin = 0; if(graphe[x][y] != 0){ voisin = 1; } return voisin; } //---------------------------------------------------// int AfficheGraphe() { int i,j; for (i=0; i<nbS; i++) { for (j=0; j<nbS; j++) { cout<<" "<<graphe[i][j]<<"\t"; } cout<<"\n"; } return 0; } //---------------------------------------------------// int CalcPoidsCumul (){ int i; int j; for (i = 0 ;i< nbS ;i=i+1) { PoidsCumul[i] = 0; } for (i = 0 ;i< nbS ;i=i+1) { for (j = i ;j< nbS ;j=j+1) { if (AppartVoisins(i,j) ==1) { if (PoidsCumul[j]==0 || PoidsCumul[j]< PoidsCumul[i]+graphe[i][j]) { PoidsCumul[j] = PoidsCumul[i]+graphe[i][j]; } } } } Affichetableau(); return PoidsCumul[nbS-1]; } //---------------------------------------------------// int Affichetableau () { int i; for (i=0; i<nbS; i++) { cout<<" "<<PoidsCumul[i]<<" "; } cout<<"\n"; return 0; } //---------------------------------------------------// int DefineChemin(){ int i; int j ; int k; for (i = 0 ;i< nbS ;i=i+1) { CheminPlusCourt[i] = 0; } for (i = 0 ;i< nbS ;i=i+1) { for (j = i ;j< nbS ;j=j+1) { if (AppartVoisins(i,j) ==1) { if( PoidsCumul[j]==0 || PoidsCumul[j]< PoidsCumul[i]+graphe[i][j]) { PoidsCumul[j] = PoidsCumul[i]+graphe[i][j]; CheminPlusCourt[i]=i; } } } } cout<<"les étapes du chemin le plus court sont "; for (k = 0 ;k< nbS ;k=k+1){ cout<<CheminPlusCourt[k]; } return 0; }
Compiling the source code.... $g++ main.cpp -o demo -lm -pthread -lgmp 2>&1 Executing the program.... $demo 0 16 10 4 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 12 0 21 0 0 0 0 0 10 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 3 5 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 16 10 4 22 25 31 34 0 16 10 4 22 25 31 34 le temps le plus court est 34 jours les étapes du chemin le plus court sont 00000000
#include <iostream> using namespace std; #define TailleMax 100 void AfficheGraphe(int graph[][8], int nb); void Affichetableau (int *Poids, int nb); int CalcPoidsCumul (int nb, int *Poids, int graph[][8]); void DefineChemin(int nb, int *Chemin, int *Poids, int graph[][8]); int AppartVoisins(int graph[][8], int x, int y); //---------------------------------------------------// int main(void) { int nbS = 8; int graphe[8][8] = { { 0,16,10, 4, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 4, 0}, { 0, 0, 0, 0,12, 0,21, 0}, { 0, 0, 0, 0,10, 0, 0, 0}, { 0, 0, 0, 0, 0, 3, 0, 0}, { 0, 0, 0, 0, 0, 0, 3, 5}, { 0, 0, 0, 0, 0, 0, 0, 3}, { 0, 0, 0, 0, 0, 0, 0, 0} }; int PoidsCumul[TailleMax]; int CheminPlusCourt[TailleMax]; AfficheGraphe(graphe, nbS); CalcPoidsCumul(nbS, PoidsCumul, graphe); cout<<"le temps le plus court est "<<CalcPoidsCumul(nbS, PoidsCumul, graphe)<<" jours \n "; DefineChemin(nbS, CheminPlusCourt, PoidsCumul, graphe); return 0; } //---------------------------------------------------// int AppartVoisins(int graph[][8], int x, int y) { if (graph[x][y] == 0) return 0; return 1; } //---------------------------------------------------// void AfficheGraphe(int graph[][8], int nb) { int i,j; for (i=0; i<nb; i++) { for (j=0; j<nb; j++) { cout<<" "<<graph[i][j]<<"\t"; } cout<<"\n"; } } //---------------------------------------------------// int CalcPoidsCumul (int nb, int *Poids, int graph[][8]){ int i; int j; for (i = 0 ;i< nb ;i=i+1) { Poids[i] = 0; } for (i = 0 ;i< nb ;i=i+1) { for (j = i ;j< nb ;j=j+1) { if (AppartVoisins(graph, i, j) ==1) { if (Poids[j]==0 || Poids[j] < Poids[i] + graph[i][j]) { Poids[j] = Poids[i] + graph[i][j]; } } } } Affichetableau(Poids, nb); return Poids[nb-1]; } //---------------------------------------------------// void Affichetableau (int Poids[TailleMax], int nb) { int i; for (i=0; i<nb; i++) { cout<<" "<<Poids[i]<<" "; } cout<<"\n"; } //---------------------------------------------------// void DefineChemin(int nb, int *Chemin, int *Poids, int graph[][8]){ int i; int j ; int k; for (i = 0 ;i< nb ;i=i+1) { Chemin[i] = 0; } for (i=0; i<nb; i++) { for (j=i; j<nb; j++) { if (AppartVoisins(graph, i, j) == 1) { if( Poids[j] == 0 || Poids[j] < Poids[i] + graph[i][j]) { Poids[j] = Poids[i] + graph[i][j]; Chemin[i] = i; } } } } cout<<"les étapes du chemin le plus court sont "; for (k = 0 ;k< nb ;k=k+1){ cout<<Chemin[k]; } }
Commentaire