From 966fde99f341d7c0fdf991d0aa4b45cbe20763fb Mon Sep 17 00:00:00 2001 From: kara-mosr Date: Wed, 15 Oct 2025 17:04:33 +0200 Subject: [PATCH] ajout travail control machine kara-mosr --- EX1.c | 193 +++++++++++++++++++++++++++++++++++++++++++++++++++ ex1 | Bin 0 -> 16376 bytes gmon.out | Bin 0 -> 4043 bytes reponses.txt | 91 ++++++++++++++++++++++++ 4 files changed, 284 insertions(+) create mode 100644 EX1.c create mode 100755 ex1 create mode 100644 gmon.out create mode 100644 reponses.txt diff --git a/EX1.c b/EX1.c new file mode 100644 index 0000000..cfdc777 --- /dev/null +++ b/EX1.c @@ -0,0 +1,193 @@ +/*Je me suis pas super bien organise le fichierest long pour rien le main est trop long +Je me suis lancer dans l'utilisation d'allocation et de pointeur ce qui etat peut etre pas necessaire +j'ai essayer de commenter et de bien expliquer dans reponses.txt pour que ca reste comprehensible +Et puis dans le main j'ai mis pas mal de printf qui devrait aider la comprehension a l'execution + +Pour compiler : +gcc -pg -o ex1 EX1.c +./ex1 +gprof ./ex1 + +Riad Kara-Mostefa +*/ + + +#include +#include +#include + + +int racineCarree(int n) { + if (n < 0) return -1; + if (n == 0 || n == 1) return n; + + int i = 1; + while (i <= n / i) { + if ((n % i == 0) && (i == n / i)) { + return i; + } + i++; + } + return -1; +} + +void racineCarreeTab(const int *in, int *out, size_t size) { + for (size_t i = 0; i < size; ++i) { + out[i] = racineCarree(in[i]); + } +} + +static void affichageTab(const char *label, const int *t, size_t n) { + printf("%s[", label); + for (size_t i = 0; i < n; ++i) { + if (i) printf(","); + printf("%d", t[i]); + } + printf("]\n"); +} + + +static void affichageTabLL(const char *label, const long long *t, size_t n) { + printf("%s[", label); + for (size_t i = 0; i < n; ++i) { + if (i) printf(","); + printf("%lld", t[i]); + } + printf("]\n"); +} + + +void TriSpecial(const int *in, size_t n, long long *out) { + if (!in || !out || n == 0) return; +/*some original et some des racines*/ + long long SommeOrigine = 0; + long long SommeSqrt = 0; + size_t nbNonEntieres = 0; +/* tab avec les racines carees*/ + int *roots = (int*)malloc(n * sizeof(int)); + if (!roots) { + for (size_t i = 0; i < n; ++i) { + int r = racineCarree(in[i]); + SommeOrigine += (long long)in[i]; + SommeSqrt += (long long)r; + if (r == -1) ++nbNonEntieres; + } + roots = NULL; + } else { + for (size_t i = 0; i < n; ++i) { + int r = racineCarree(in[i]); + roots[i] = r; + SommeOrigine += (long long)in[i]; + SommeSqrt += (long long)r; + if (r == -1) ++nbNonEntieres; + } + } + + const int pair = ((nbNonEntieres % 2) == 0); + + if (pair) { + for (size_t i = 0; i < n; ++i) { + if ((i % 2) == 0) out[i] = (long long)in[i]; + else out[i] = SommeOrigine * (long long)in[i]; + } + } else { + for (size_t i = 0; i < n; ++i) { + if ((i % 2) == 0) { + int r = roots ? roots[i] : racineCarree(in[i]); + out[i] = (long long)r; + } else { + out[i] = SommeSqrt * (long long)in[i]; + } + } + } + + free(roots); +} + +int main(void) { + printf("EX1 : tests simples\n"); + printf("racineCarree(9) = %d\n", racineCarree(9)); + printf("racineCarree(10) = %d\n", racineCarree(10)); + + int t1[] = {9,25,4}; + int r1[3]; + racineCarreeTab(t1, r1, 3); + affichageTab("racineCarreeTab([9,25,4]) = ", r1, 3); + + int t2[] = {10,36,2}; + int r2[3]; + racineCarreeTab(t2, r2, 3); + affichageTab("racineCarreeTab([10,36,2]) = ", r2, 3); + + /* le gros tableau de test comme demande dans l'exo2 */ + printf("\nEX2 : gros tableau (racineCarreeTab) \n"); + const size_t N = 10000; + int *big = (int*)malloc(sizeof(int) * N); + int *res = (int*)malloc(sizeof(int) * N); + /*je verifie l'allocation memeoire*/ + if (!big || !res) { + fprintf(stderr, "Allocation echouee pour EX2\n"); + free(big); free(res); + return 1; + } + + /* J'ai essaye de genere des carre parfait et des non-carres on va dire pour tester */ + for (size_t i = 0; i < N; ++i) { + int base = 1000000 + (int)(i * 1237); + if (i % 997 == 0) { + int s = 2000 + (int)(i % 500); + base = s * s; + } + big[i] = base; + } + + racineCarreeTab(big, res, N); + printf("res (debut 10) = ["); + for (size_t i = 0; i < 10 && i < N; ++i) { + if (i) printf(","); + printf("%d", res[i]); + } + if (N > 10) printf(", ...]"); + printf("\n"); + + printf("\n EX3 : tests TriSpecial\n"); + { + int a1[] = {3,5,25,16}; + long long out1[4]; + TriSpecial(a1, 4, out1); + affichageTabLL("TriSpecial([3,5,25,16]) = ", out1, 4); + } + { + int a2[] = {36,9,100,2,3,7}; + long long out2[6]; + TriSpecial(a2, 6, out2); + affichageTabLL("TriSpecial([36,9,100,2,3,7]) = ", out2, 6); + } + + /* On utilise un gros tableau comme demandéé dans l'exo4 */ + printf("\nEX4 : gros tableau (TriSpecial)\n"); + /*la sortie pour le long tableau de gros elements*/ + long long *sortieLL = (long long*)malloc(sizeof(long long) * N); + if (!sortieLL) { + fprintf(stderr, "Allocation echouee pour EX4 (sortieLL)\n"); + free(big); free(res); + return 1; + } + + TriSpecial(big, N, sortieLL); + /*Je me suis dit que 10 elements suffisait pour voir si on a le resultat attendu donc j'en affiche que 10*/ + printf("TriSpecial(big) (debut 10) = ["); + for (size_t i = 0; i < 10 && i < N; ++i) { + if (i) printf(","); + printf("%lld", sortieLL[i]); + } + if (N > 10) printf(", ...]"); + printf("\n"); + + /* Nettoyage */ + free(sortieLL); + free(big); + free(res); + + return 0; +} diff --git a/ex1 b/ex1 new file mode 100755 index 0000000000000000000000000000000000000000..356e7fa31fd0e9e453150299dd6cf0cf6ea01342 GIT binary patch literal 16376 zcmb<-^>JfjWMqH=W(GS35O0G3M8p9?F<4kY84L^z4h$9y{0t5Z@(gkeYzzzxEMPH+ zJWM@|_F#sH!)Oi&mjNUU()R|$U|?WCr)8k(U^K{0AR!P9(g$K=!w;Yqz-R`jKS26e zK^mZZm^h3+$ODpOV1UsuagaW+eF_kH1~j?{EXlwCqmlK2!sdW3MBf2hRC)pvguekw z!}Ni;Abl60`Yu5A!RQAd2Qe@(z-XvzGz-V-RC!qS!X%&bw8DKQX z4v-Lt2H_m21UiitOknqc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llNlHoKy6u= zepvezMuP|2q5IK5dSL2cbO8ec1E_5b4&w?VKl6L zjvjt6|93*iwn6*IK<2^P`!E{TKLF_m=|K;_$F8XBZ8duYjoq(dhPr7Mp>rfZ4ABO>i(8yrLMS z5sK062d!}Uf|UQz_jQ8SuR*nfDRlj385kIl%MV=pSHYU0X${5((V+dJpt%%K9t81W z{b$&|PjvfWd>H+lfdSOY1n<`b>4){NPeAwm!qkD}Kp4h{(VrO@zKWE%qm(spmK08BrO25lJ!#UD&Rte^A(sy_m(3qip2 z!Duc<$g*pgepr9U0W@(4-IIsVjTn!BiGk+dKxV=8!`jUOIP^o4HB38$DAazaa=185 zKYIQL`4yxHnTDlZkT?t%Kn+xYCU{xsKmy3EAT^+}2tFj_ literal 0 HcmV?d00001 diff --git a/gmon.out b/gmon.out new file mode 100644 index 0000000000000000000000000000000000000000..fe63c9104decdabc577b3c2d591f501720cc92f6 GIT binary patch literal 4043 zcmYe#&Cg?GzyXfPKqO4q85mL+7#NCElk@XZiXnmw3~&ZaV3ZyWfzc2c4S~@R7!85Z z5Eu=CksAV_CKU^s_qoydqukLD7!85Z5Eu;sLLtDoK?tIR4N8ltGcYhRh@uKgfdmD_ zQ3XK-F{1%C!3L;cG1N@3$^}rt`^bV9pn@nWA3z0BR8EkD*p8yIK?))$E&`z;7C{B8 YgpmXvKm}1$et-(1sGJ}T(T1!N0G!7a#Q*>R literal 0 HcmV?d00001 diff --git a/reponses.txt b/reponses.txt new file mode 100644 index 0000000..8e15cac --- /dev/null +++ b/reponses.txt @@ -0,0 +1,91 @@ + + + ----------------------------------------EX2--------------------------------------- +1- + +racineCarree : Temps : 0.09s et on a 10 008 appels. Ca fait donc un temps moyen d'appel de 0.01ms + +racineCarreeTab : avec grpof on nous dit que son temps d'execution est de 0s. On a 3 appels qui correspondent aux deux petits tableaux de test que j'ai dans mon main et au tableau +avec 10 000 elements tous plus grand que 1 000 000. Si son temps d'execution est aussi cours c'est parce qu'on appelle racineCarree. Si on prend cela en compte on voit que +racineCarreeTab s'execute sur 30s environ. + +On a donc racineCarreeTab qui appel 10 006 fois racineCarree et les 2 autres sont dans le main avec les deux petits test que j'ai mis. + +Pour resume on a racineCarreeTab qui ne consomme presque rien car tout est delegue a racineCareeTab qui est appele en boucle pour parcourir le tableau. + + + + +2- +Complexite cyclomatioque : + +de racineCarree est de 5 car : +1 "if (n < 0)" + +2 "if (n == 0 || n == 1) " + +3 "while (i <= n / i)" + +4 "if ((n % i == 0) && (i == n / i))" + +de racineCareeTab est de 2 car on a que la boucle for : +La boucle "for (k = 0; k < size; ++k)" + + +Complexite Algorithmique : + +---------de racineCarree : O(√n)------------- +Si n n’est pas un carré parfait : on ne retourne pas avant la fin. Donc la boucle fait exactement ⌊√n⌋ itérations, puis on sort et on renvoie -1 + +Si n est un carré parfait : on retourne à i = √n donc la boucle fait √n itérations (la dernière est celle qui trouve la racine) + +Dans tous les cas, le nombre d’itérations est proportionnel à √n + +-------- de racineCarreeTab dépend ---------- + +du nombre d’éléments dans le tableau qu'on note m + +du temps d’exécution de racineCarree() pour chaque élément + +Et Puisque racineCarreeTab appelle racineCarree() m fois (une fois par élément du tableau) le coût total est la somme des coûts de chaque appel : + +On va regarder le pire, c'est celui ou tous les elements du tableau sont proche de l'element le plus grand. Par exemple dans notre exemple avec des valeurs > 1 000 000. +On peut dire que toutes les valeurs sont proches de la plus grandes car a cette echelle de grandeur la difference est minim. On peut donc dire que dans le pire des cas +la complexite est dependante du max (la plus grande valeurs) et du nombre d'appels a la fonction racineCarree +on a comme complexite : O(m * √max) + + + +----------------------------EX4-------------------------------- +La complexite cyclomatique de TriSpecial : + +1 : if (!in || !out || n == 0) + +2 : if (!roots) + +3 : for (...) (branche !roots) + +4 : if (r == -1) (branche !roots) + +5 : for (...) (branche else) + +6 : if (r == -1) (branche else) + +7 : if (pair) + +8 : for (...) (branche pair) + +9 : if (i % 2 == 0) (branche pair) + +10 : for (...) (branche impair) + +11 : if (i % 2 == 0) (branche impair) + +Donc on a une complexite de 12 pour TriSpecial + +La complexite cyclomatique de TriSpecial : + +C'est le meme principe que racineCarreeTab et ca depend du nombre d'appel et de l'element le plus grand + +On a O(N * √M) ou est M est le plus grand nombre et N le nombre d'appel. +