FiddleComputers

TP3 C NF04

Sep 26th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h> //Bibliothèque nécessaire pour utiliser la fonction sqrt()
  4.  
  5. void exo1() //Exécute l'exercice 1
  6. {
  7.     int i = 1; int max = 0; int posMax; int saisie = -1;
  8.  
  9.     while(saisie!=30)
  10.     {
  11.         printf("Entrez une valeur entiere : ");
  12.         scanf("%d", &saisie);
  13.  
  14.         if(i == -1)
  15.         {
  16.             posMax = 0;
  17.             max = saisie;
  18.         }
  19.  
  20.         if(saisie > max)
  21.         {
  22.             max = saisie;
  23.             posMax = i;
  24.         }
  25.  
  26.         i++;
  27.     }
  28.  
  29.     printf("La plus grande valeur se trouve en position %d et est %d", posMax, max);
  30. }
  31.  
  32. void exo2() //Exécute l'exercice 2
  33. {
  34.     int tmp, N, posMin;
  35.     int tableau[99];
  36.  
  37.     printf("Combien de valeurs voulez-vous saisir ? ");
  38.     scanf("%d", &N);
  39.     printf("Saisir maintenant les valeurs du tableau !\n");
  40.  
  41.     int i;
  42.     for(i=0;i<N;i++)
  43.     {
  44.         scanf("%d", &tableau[i]);
  45.     }
  46.  
  47.     for(i=0;i<N-1;i++)
  48.     {
  49.         posMin = i;
  50.  
  51.         int j;
  52.         for(j=i+1;j<N;j++)
  53.         {
  54.             if(tableau[j] < tableau[posMin])
  55.             {
  56.                 posMin = j;
  57.             }
  58.         }
  59.  
  60.         if(posMin != i)
  61.         {
  62.             tmp = tableau[i];
  63.             tableau[i] = tableau[posMin];
  64.             tableau[posMin] = tmp;
  65.         }
  66.     }
  67.  
  68.     for(i=0;i<N;i++)
  69.     {
  70.         printf("%d ", tableau[i]);
  71.     }
  72. }
  73.  
  74. void exo3() //Exécute l'exercice 3
  75. {
  76.     float tableau_points[5][2]; //On crée un tableau de 5 points, qui ont chacun deux coordonnées x et y situées aux indexs 0 et 1
  77.     float x, y;
  78.     int i, pointPlusProche;
  79.     pointPlusProche = 0;
  80.  
  81.     for(i=0;i<5;i++)
  82.     {
  83.         printf("Entrez une coordonnee X : "); scanf("%f", &x);
  84.         printf("Entrez une coordonnee Y : "); scanf("%f", &y);
  85.  
  86.         tableau_points[i][0] = x;
  87.         tableau_points[i][1] = y;
  88.     }
  89.  
  90.     printf("Entrez une coordonnee X de plus : "); scanf("%f", &x);
  91.     printf("Entrez une coordonnee Y de plus : "); scanf("%f", &y);
  92.  
  93.     float plusPetiteDistance = sqrt(pow(tableau_points[pointPlusProche][0]-x, 2)+pow((tableau_points[pointPlusProche][1]-y), 2)); //Le calcul correspond à la formule donneé dans l'énoncé
  94.  
  95.     for(i=0;i<5;i++)
  96.     {
  97.         float nouvelleDistance = sqrt(pow(tableau_points[i][0]-x, 2)+pow((tableau_points[i][1]-y), 2)); //Le calcul correspond à la formule donneé dans l'énoncé
  98.  
  99.         if(nouvelleDistance < plusPetiteDistance)
  100.         {
  101.             plusPetiteDistance = nouvelleDistance;
  102.             pointPlusProche = i;
  103.         }
  104.     }
  105.  
  106.     printf("Le point le plus proche de (%.1f;%.1f) est (%.1f;%.1f) !", x, y, tableau_points[pointPlusProche][0], tableau_points[pointPlusProche][1]);
  107. }
  108.  
  109. int main()
  110. {
  111.     exo3(); //Ici entrez le nom de la fonction que vous voulez exécuter. Exemple : exo1() pour exe tout l'exercice 1.
  112.  
  113.     return 0;
  114. }
Add Comment
Please, Sign In to add comment