Skip to content
Snippets Groups Projects
Commit 5830c37c authored by thibault.chatillo's avatar thibault.chatillo
Browse files

ex 1-6

parent c014585d
Branches main
No related tags found
No related merge requests found
1. affiche 5 a "l'infini" pas de cond de sortie
2. appel "infini" se limite a la mémoire (stack overflow)
3. 9 appels affiche : 1 3 1 5
cc=gcc
LIBS=-Wextra -Wall -g -fsanitize=address -fsanitize=leak -lm
test :test.x
./test.x
test.x: rec.o test.o
$(cc) -o $@ $^ $(LIBS)
test.o: test.c
$(cc) -c $^ $(LIBS)
main: main.x
./main.x
main.x: rec.o main.c
$(cc) -o $@ $^ $(LIBS)
rec.o: rec.c rec.h
$(cc) -c $^ $(LIBS)
clean:
rm -f *.o *.x *.gch
#include "rec.h"
int main(){
show(10);show(0);show(-5);
printf("\n%d\n",sum(10));
return EXIT_SUCCESS;
}
\ No newline at end of file
#include "rec.h"
//1
void show(int n){
if(n<=0){
return;
}
n--;
show(n);
printf("%d ",n);
}
//2
int sum(int x){
if(x<=0){
return 0;
}
return x+sum(x-1);
}
//3
int sum_arr(int arr[],int size){
if(size<1){
return 0;
}
size--;
return arr[size]+sum_arr(arr, size);
}
//4
int count_digits(int x){
if(x/10==0){
return 1;
}
x/=10;
return 1+count_digits(x);
}
//5
int sum_digits(int x){
if(x/10==0){
return x;
}
int r=x%10;
x/=10;
return r+sum_digits(x);
}
//ex6
bool is_sum_digits(char str[],int index,int len){
if(index>=len){
return true;
}
bool same=str[index]==str[len];
index++;len--;
return same&&is_sum_digits(str,index,len);
}
//ex7 aie aie aie
\ No newline at end of file
#ifndef _REC_H_
#define _REC_H_
#include <assert.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void show(int n);
int sum(int x);
int sum_arr(int arr[],int size);
int count_digits(int x);
int sum_digits(int x);
bool is_sum_digits(char str[],int index,int len);
#endif
\ No newline at end of file
#include <stdlib.h>
#include "rec.h"
int main(){
assert(sum(10)==55);
int arr[3]={1,5,3};
assert(sum_arr(arr, 3));
assert(count_digits(1234)==4);
assert(sum_digits(8345)==20);
assert(is_sum_digits("abba", 0, 3));
assert(!is_sum_digits("albuchef", 0, 7));
printf("\ntest ok\n");
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment