Blame view

Tp4/tp4.c 2.47 KB
5eb7fe63   Vincent Benoist   TP de C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  #include <stdio.h>
  #include <string.h>
  
  #define N 4
  
  void printVector (float vect[], int n){
      int i ;
       for(i=0; i <= n-1; i++){
          printf("V[%d]:%f     ",i, vect[i]);
          
      }
      printf("\n\n");
  }
  
  void setVector (float vect[], int n){
      int i ;
      scanf("%f", &vect[0]);
     
      for(i=1; i <= n-1; i++){
          scanf("%f", &vect[i]);
          while (vect[i] < vect[i-1]){
              printf("001- Suite non croissante \nVecteurActuel:");
              printVector(vect, i);
              
              scanf("%f", &vect[i]);
          }
      }
  }
  
  
  void calcSommeFloat (float* sommeFloatNeg, float* sommeFloatPos, float vect[], int n){
      int i ;
      float resNeg=0;
      float resPos=0;
      
       for(i=0; i <= n-1; i++){
          if(vect[i]>=0){
              resPos += vect[i];
          }else {
              resNeg += vect[i];
          }
      }
      
      *sommeFloatPos = resPos;
      *sommeFloatNeg = resNeg;
  }
  
  void fusion ( float vect1[],float vect2[],float vectres[], int n){
   int indV1=0;
   int indV2=0;
   int i;
   while( indV1 <= n-1 && indV2 <= n-1){
      if( vect1[indV1] <= vect2[indV2]){
          vectres[indV1+indV2] = vect1[indV1] ;
          indV1++;
      }else {
           vectres[indV1+indV2] = vect2[indV2] ;
           indV2++;
      }
      
   }
  
   if( indV1 == n ){
      for( i = indV2; i <= n-1; i++){
          vectres[indV1+i] = vect2[i];
      }     
   }else {
        for( i = indV1; i <= n-1; i++){
          vectres[indV2+i] = vect1[i];
      }
   }
  }
  
  void intersection ( float vect1[],float vect2[],float vectres[], int n, int * nbValV3){
      
   int indV1=0;
   int indV2=0;
  
   *nbValV3 = 0;
   while( indV1 <= n-1 && indV2 <= n-1){
      if( vect1[indV1] < vect2[indV2]){
          
          indV1++;
      } else if(vect1[indV1] == vect2[indV2]){
         vectres[*nbValV3]= vect1[indV1];
         (*nbValV3)++;
         indV1++;
          indV2++;
      } else {
           indV2++;
      }
      
   }
  
  }
  
  
  
  int main (){
      /*float vector[N];
      float sommeFloatNeg;
      float sommeFloatPos;
     
      setVector(vector, N);
      printVector(vector, N);
      
      calcSommeFloat ( &sommeFloatNeg, & sommeFloatPos, vector, N);
      printf("Pos %f ; Neg %f", sommeFloatPos, sommeFloatNeg); 
      
      */
      float vector1[N];
      float vector2[N];
      setVector(vector1, N);
      setVector(vector2, N);
      float vector3[N*2];
      fusion( vector1, vector2, vector3, N);
      printVector( vector3, 2*N);
      float vector4[N];
      int nbVal=0;
      intersection(vector1, vector2, vector4, N, &nbVal);
      printVector( vector4, nbVal);
      
      
   return 1;   
  }