Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.8 kB
1
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void union_();
void intersection();
int arr1[10], arr2[10];
int i , j , c , d , n ,  p  , q ;
int union1[20], inter[10];

int main(){
    srand (time(0));

    /*printf("Input Set 1 :  ");
    for(int i = 0; i <= 9; i++){
        n = rand() % 30 + 1 ;
        arr1[i] = n ;
        printf( "%d " , n ) ;
    }*/
    input(1, &arr1);
    //printf("\n");
    //printf("Input Set 1 :  ");
    //for(int i = 0; i < 10; i++){printf("%d ", arr1[i]);}
    printf( "\n") ;
    input(2, &arr2);
    /*printf("Input Set 2 :  ");
    for(int i = 0; i <= 9; i++){
        n = rand() % 30 + 1 ;
        arr2[i] = n ;
        printf( "%d " , n ) ;
    }*/
    printf( "\n")  ;
    union_() ;
    printf( "\n" ) ;
    intersection() ;
    return 0 ;
}
void input(int a, int* arr){
    printf("Input Set %d :  ", a);
    for(int i = 0; i <= 9; i++){
        n = rand() % 30 + 1 ;
        arr[i] = n ;
        printf( "%d " , n ) ;
    }
}
void union_(void){
    int exist = 0;
    int idx = 0;
    for(int i = 0; i < 10; i++){
        for(int j = 0;  j < 20; j++){
            if(arr1[i] == union1[j]){
                exist = 1;
                break;
            }
        }
        if(exist){
            exist = 0;
            continue;
        }
        else{
            union1[idx] = arr1[i];
            idx++;
        }
    }
    exist = 0;
    for(int i = 0; i < 10; i++){
        for(int j = 0;  j < 20; j++){
            if(arr2[i] == union1[j]){
                exist = 1;
                break;
            }
        }
        if(exist){
            exist = 0;
            continue;
        }
        else{
            union1[idx] = arr2[i];
            idx++;
        }
    }
    int i = 0;
    while(union1[i] != 0){
        printf("%d ", union1[i]);
        i++;
    }
    printf("\n");
}

void intersection(void){
    int idx = 0;
    int exist = 0;
    int shouldadd = 0;
    for( i = 0 ; i < 10 ; i++ ){
        for( j = 0 ; j < 10 ; j++ ){
            if ( arr1[ i ] == arr2[ j ] ){
                for(int k = 0;  k < 10; k++){
                    if(arr1[i] == inter[k]){
                        exist = 1;
                        break;
                    }
                }
                if(exist){
                    shouldadd = 0;
                    exist = 0;
                    break;
                }
                else{
                    shouldadd = 1;
                    break;
                }
            }

        }
        if(shouldadd){
            inter[idx] = arr1[i];
            idx ++;
            shouldadd = 0;
        }
    }
    int i = 0;
    while(inter[i] != 0){
        printf("%d ", inter[i]);
        i++;
    }
}