Answer the question
In order to leave comments, you need to log in
C - How to merge two sequences (array) into one without repeating numbers?
// задача 338б
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#define SIZE 25
int n, a[SIZE], *b, *c;
int getArraySize(int *arrayName) {
if (arrayName != NULL)
return sizeof(arrayName) / sizeof(arrayName[0]);
else {
printf("ERROR");
return 0;
}
}
void main() {
n = 0 + rand() % ((100 + 1) - 0);
b = (int*)malloc(sizeof(int)*n);
c = b;
for (int i = 0; i< SIZE; i++) {
a[i] = 0 + rand() % ((100 + 1) - 0);
}
for (int i = 0; i< n; i++) {
b[i] = 0 + rand() % ((200 + 1) - 0);
}
for (int i = 0; i< SIZE; i++) {
printf("> %d. %d\n", i, a[i]);
}
printf("\n");
for (int i = 0; i< n; i++) {
printf(">> %d. %d\n", i, b[i]);
}
for (int i = 0; i<SIZE; i++) {
for (int j = 0; j<n; j++) {
if (a[i] != b[j]) {
c = (int*)realloc(c, sizeof(int)*(n + 1));
c[getArraySize(c)] = a[i];
}
}
}
printf("pause");
for (int i = 0; i<getArraySize(c); i++) {
printf(">>> %d. %d ", i, c[40]);
}
system("pause");
}
Answer the question
In order to leave comments, you need to log in
With the construct:
return sizeof(arrayName) / sizeof(arrayName[0]);
you won't get the array size for dynamic arrays, this only works for static arrays when the compiler knows the array size in advance. The sizeof() operation is executed at compile time.
You must manually read the size of the array c and increase it with realloc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question