I need to write a function which fills array rez with the conjugate-complex pairs from the array bounded by p1 and p2. The function returns the number of conjugate-complex pairs placed in the array. Duplicates must not be placed in the sequence. Conjugate-complex pairs are pairs of forms a + bi and a - bi.
This task should be solved using structures and pointer arithmetic. Auxiliary arrays are not allowed.
#include <stdio.h>
typedef struct {
int im, re;
} complex;
void remove_duplicates(complex *rez, int *number){
int i,j,k;
for (i = 0; i < *number; i++) {
for (j = i + 1; j < *number; j++) {
if (rez[i].im == rez[j].im && rez[i].re == rez[j].re) {
for (k = j; k < *number - 1; k++) {
rez[k].im = rez[k + 1].im;
rez[k].re = rez[k + 1].re;
}
(*number)--;
j--;
}
}
}
}
int conjugate_complex(complex *p1, complex *p2, complex *rez) {
int number_of_pairs = 0;
while (p1 < p2) {
if (p1->im == p1->re||p1->im == -1*p1->re) {
number_of_pairs++;
rez->re = p1->re;
rez->im = -1*p1->im;
}
rez++;
p1++;
}
remove_duplicates(rez,&number_of_pairs);
return number_of_pairs;
}
int main() {
int i;
complex arr1[5] = {{5, 5}, {3, 3}, {-5, -5}, {5, 5}, {-3, 3}};
complex arr2[5];
int vel = conjugate_complex(arr1, arr1 + 5, arr2);
printf("%d\n", vel);
for (i=0; i<vel; i++)
printf("(%d,%d) ",arr2[i].im,arr2[i].re);
return 0;
}
OUTPUT should be:
4
(-5,5) (-3,3) (5,-5) (3,3)
My output is:
5
(-5,5) (-3,3) (5,-5) (-5,5) (3,3)
The problem with my code is that it prints duplicates. Could you help me fix my remove_duplicates function? If I call it in main function it would work. However, I need to call it in the function conjugate_complex.