CONTROLE
This commit is contained in:
139
TriSpecial.c
Normal file
139
TriSpecial.c
Normal file
@@ -0,0 +1,139 @@
|
||||
|
||||
/*
|
||||
Comme j'ai eu des soucis que je ne comprend pas en faisant appel
|
||||
a des fonctions d'un autre fichier je recrit les fonction
|
||||
racineCarree et racineCarreeTab ici
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
///Ex2
|
||||
int racineCarree(long long x) {
|
||||
if (x < 0) return -1;
|
||||
|
||||
long long i = 0;
|
||||
while (i * i <= x) {
|
||||
if (i * i == x) return (int)i;
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* racineCarreeTab(const long long *arr, size_t n) {
|
||||
int *out = malloc(n * sizeof(int));
|
||||
if (!out) return NULL;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
out[i] = racineCarree(arr[i]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
///Ex3
|
||||
long long* TriSpecial(const long long *arr, size_t n) {
|
||||
if (!arr || n == 0) return NULL;
|
||||
|
||||
long long somme_arr = 0;
|
||||
long long *rc = malloc(n * sizeof(long long));
|
||||
|
||||
size_t nonIntCount = 0;
|
||||
long long somme_rc = 0;
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
somme_arr += arr[i];
|
||||
int r = racineCarree(arr[i]);
|
||||
rc[i] = r;
|
||||
if (r == -1) nonIntCount++;
|
||||
somme_rc += rc[i];
|
||||
}
|
||||
|
||||
|
||||
long long *out = malloc(n * sizeof(long long));
|
||||
if (!out) { free(rc); return NULL; }
|
||||
|
||||
if (nonIntCount % 2 == 0) {
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (i % 2 == 0) out[i] = arr[i];
|
||||
else out[i] = somme_arr * arr[i];
|
||||
}
|
||||
} else {
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
if (i % 2 == 0) out[i] = rc[i];
|
||||
else out[i] = somme_rc * arr[i];
|
||||
}
|
||||
}
|
||||
|
||||
free(rc);
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
static void print_all_tab(const long long *a, size_t n) {
|
||||
printf("[");
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
printf("%lld", a[i]);
|
||||
if (i + 1 < n) printf(", ");
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
static void print_int_tab(const int *a, size_t n) {
|
||||
printf("[");
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
printf("%d", a[i]);
|
||||
if (i + 1 < n) printf(", ");
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
|
||||
{
|
||||
long long tab1[] = {25,48,2};
|
||||
int *r1 = racineCarreeTab(tab1, 3);
|
||||
printf("racineCarreeTab([25,48,2]) -> ");
|
||||
print_int_tab(r1, 3);
|
||||
free(r1);
|
||||
|
||||
long long tab2[] = {81,58,9};
|
||||
int *r2 = racineCarreeTab(tab2, 3);
|
||||
printf("racineCarreeTab([81,58,9]) -> ");
|
||||
print_int_tab(r2, 3);
|
||||
free(r2);
|
||||
|
||||
long long tab3[] = {68,10,54,16};
|
||||
long long *ts1 = TriSpecial(tab3, 4);
|
||||
printf("TriSpecial([68,10,54,16]) -> ");
|
||||
print_all_tab(ts1, 4);
|
||||
free(ts1);
|
||||
|
||||
long long tab4[] = {58,45,100,74,6,9};
|
||||
long long *ts2 = TriSpecial(tab4, 6);
|
||||
printf("TriSpecial([58,45,100,74,6,9]) -> ");
|
||||
print_all_tab(ts2, 6);
|
||||
free(ts2);
|
||||
}
|
||||
|
||||
|
||||
size_t n = 10000;
|
||||
long long *test = malloc(n * sizeof(long long));
|
||||
if (!test) return 1;
|
||||
|
||||
srand(42);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
|
||||
test[i] = 1000001LL + (rand() % 2000000);
|
||||
}
|
||||
|
||||
|
||||
int *racine_test = racineCarreeTab(test, n);
|
||||
long long *ts_test = TriSpecial(test, n);
|
||||
|
||||
free(racine_test);
|
||||
free(ts_test);
|
||||
free(test);
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user