This repository has been archived on 2026-05-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
2026-02-09 03:43:16 +01:00

50 lines
959 B
C

#include <stdlib.h>
//#include <stdio.h>
int *compress_array(const int *input, int n, int *out_size){
if (input == NULL) return NULL;
int taille = 1;
for (int i = 0; i < n -1; i++) if(input[i] != input[i + 1]) taille ++;
*out_size = taille * 2;
int *rslt = malloc(*out_size * sizeof(int));
if (rslt == NULL) return NULL;
int index_rslt = 0;
int index = 0;
while(index < n){
int ch_cons = 0;
int chiffre = input[index];
while (index < n && input[index] == chiffre){
ch_cons ++;
index ++;
}
rslt[index_rslt ++] = chiffre;
rslt[index_rslt ++] = ch_cons;
}
return rslt;
}
/*
int main(){
int test[] = {3, 3, 3, 1, 1, 5, 5, 5, 5};
int sortie = 6;
int *rs = compress_array(test, 9, &sortie);
for(int i = 0; i < sortie; i++){
printf("%i;", rs[i]);
}
free(rs);
}*/