-2

I am a beginner. I think the main issue I have is with flags. (I cannot use a break command to stop the loops). Ex:

A={1,2,3,4,5} B={1,2,3,6} 

I have to generate a new array with the previous ones, without duplicates. So: C={1,2,3,4,5,6}.

#include <stdio.h>
#include <stdlib.h>

#define M 5
#define N 4
int main() {

int A[M]={1,2,3,4,5};
int B[N]={1,2,3,6};
int U[M+N]; //M+N is the maximum size. it'll decrease with a counter.

int i=0, j=0,count=0,flag=0;

while(i<M){           //Array A is copied into Array U.
U[count]=A[i];
count++;        //counter that will determine the size.
i++;
}

i=0,j=0;  

while(i<M){     
    j=0;
    flag=0;
while(flag==1 || j<N){         //check if the flag is on or if the array is ended.
    if(B[j]!=A[i]){               // check if the element of the b array is different from
                                            //the element of array A (cause i already copied the array A)
        count++;                                //i make some space for the element to be copied.
        U[count]=B[j];            
    }
    else flag=1;                            //if the flag is on it means the element was equal, so i just
    j++;                                        //go to the next one
}       
i++;
}

for(i=0;i<count;i++)
printf(" %d ", U[i]);       //here i print, it prints the first 5 values from Array a correctly, for the other ones is a mess.

return 0;
}

My idea is to copy the longest array (A) to the new one (C) and then scan the second one (B) and checking each value with each value of the array A. If the value is different (out all of the values in A) I add the B value into C, otherwise I start to check the next value of array B with all of the values in A.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yaron Pray
  • 13
  • 1
  • 3
  • 2
    Have you implemented your idea? – Rushy Panchal Jun 10 '18 at 00:23
  • 2
    Show us what you've implemented so far – alamoot Jun 10 '18 at 00:30
  • 1
    Possible duplicate of [Merge two arrays and omit all repeating elements](https://stackoverflow.com/q/20124895/608639), [Merge two int array from one array without duplication](https://stackoverflow.com/q/20481983/608639), [Merge two arrays without sorting using array](https://stackoverflow.com/q/18475063/608639), [How do I merge two arrays having different values into one array?](https://stackoverflow.com/q/1700182/608639), etc. – jww Jun 10 '18 at 02:05

3 Answers3

1

Alright, i figured this out after some tries.

#include <stdio.h>
#include <stdlib.h>


#define F 5
#define S 4
#define L F+S

int main() {
    int A[F]={1,2,3,4,5};
    int B[S]={1,5,6,7};
    int U[L];

    int i=0,j=0,x=0, count=F, flag=1;

    printf("Primo vettore:\n");
    for(i=0;i<F;i++){     
        printf("%d ", A[i]);
    }

    printf("\n");

    printf("Secondo vettore:\n");
    for(i=0;i<S;i++){     
        printf("%d ", B[i]);
    }

    printf("\n");

    for(i=0;i<F;i++){     //i copy each element of array A to array B.
        U[i]=A[i];
    }

    for(i=0;i<S;i++){
        flag=1;
        x=0;
        for(j=0;flag==1 && j<F;j++){
            if(A[j]==B[i]){
            flag=0;
            }
        }
        if(flag==1){
            U[count]=B[i];
            count++;
        }
    }

    printf("Vettore generato:\n");
    for(i=0;i<count;i++)
    printf(" %d ", U[i]);


    return 0;
}

thanks for the help everyone!

Yaron Pray
  • 13
  • 1
  • 3
0

You define array A with size M and array B with size N. In the nested while loops, you check i (index used with array A) with N. You're just mixing up the array sizes.

while(i<N){     
    j=0;
    flag=0;
    while(flag==1 || j<M){         
        if(B[j]!=A[i]){               
            count++;                               
            U[count]=B[j];            
        }
        else 
            flag=1;                            
        j++;                                        
    }   
i++;
}
0

I would use the Union() Methode

int A[M]={1,2,3,4,5};
int B[N]={1,2,3,6};
int C[]= A.Union(B).ToArray

This works for me every time

obind
  • 105
  • 1
  • 8