exo1 fini
This commit is contained in:
57
exo1/racine.c
Normal file
57
exo1/racine.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define LIMIT 256
|
||||
|
||||
long racineCarree(long n){
|
||||
for(long i=0; i<(n/2); i++){
|
||||
if((i*i)==n){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
long* racineCarreeTab(long* tab, int count){
|
||||
long* rst = (long*) malloc(LIMIT*sizeof(long));
|
||||
for(long i=0; i<count; i++){
|
||||
rst[i] = racineCarree(tab[i]);
|
||||
}
|
||||
return rst;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv){
|
||||
/*variables*/
|
||||
long* tab = (long*) malloc(LIMIT*sizeof(long));
|
||||
long* rst = (long*) malloc(LIMIT*sizeof(long));
|
||||
int count = 0;
|
||||
|
||||
/*tests*/
|
||||
if(argc < 2){
|
||||
printf("USAGE:./a.out <entier(s) strictement(s) positif(s)>\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for(int i=1; i<argc; i++){
|
||||
if((strtol(argv[i], NULL, 10)<0)||(strtol(argv[i], NULL, 10)==0L)){
|
||||
printf("FORMAT:entier strictement positif\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
tab[i-1] = strtol(argv[i], NULL, 10);
|
||||
count++;
|
||||
}
|
||||
|
||||
/*prog*/
|
||||
if(argc == 2){
|
||||
printf("racineCarree de %ld = %ld\n", strtol(argv[1], NULL, 10), racineCarree(strtol(argv[1], NULL, 10)));
|
||||
} else {
|
||||
rst = racineCarreeTab(tab, count);
|
||||
printf("racineCarreeTab : \n");
|
||||
for(int i=0; i<count; i++){
|
||||
printf(" . racine de %ld = %ld\n", tab[i], rst[i]);
|
||||
}
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Reference in New Issue
Block a user