tp4.c 2.47 KB
#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;   
}