From: Jérôme Benoit Date: Sat, 11 Feb 2017 21:20:10 +0000 (+0100) Subject: Initial commit of Polytech'Marseille tutorial C exercices X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=de32237fa31ef30b5242996406dc112462ad3c1d;p=TD_C.git Initial commit of Polytech'Marseille tutorial C exercices * Introduce an exercice solution skeleton with a basic Makefile and piece of C elementary structure file. * Preliminary work on tutorial's exercice one : - add void swap(int* v1, int* v2); function; - add void displayArray(int* array, int count) function; - add bool sortFirst(int* array, int length) function; * Document a bit the usage in the README file. Signed-off-by: Jérôme Benoit --- de32237fa31ef30b5242996406dc112462ad3c1d diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c90c33b --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +*.o + +# editor trash +*.swp +*.~ + +thumbs.db diff --git a/README b/README new file mode 100644 index 0000000..2a8c022 --- /dev/null +++ b/README @@ -0,0 +1,41 @@ +Introduction: +------------- + +The whole purpose of the current C code is to solve the +Polytech'Marseille C tutorial exercices. + +The repositories organisation is pretty straightforward: +-> exo? where ? is a digit is the repository where lies the +solution to exercice one, and so on. + +Building the solutions: +----------------------- + + cd exo1 && make + cd exo2 && make + cd exo3 && make + +Cleaning the solutions: +----------------------- + + cd exo1 && make clean + cd exo2 && make clean + cd exo3 && make clean + +Running the solutions: +---------------------- + + cd exo1 && ./exo1 + cd exo2 && ./exo2 + cd exo3 && ./exo3 + +Exercice skeleton: +------------------ + +It's the directory exo_skel with a basic Makefile inside + +To use it: + cp -a exo_skel exo# where # is a digit + cd exo# && cp exo_skel.c exo# + edit the Makefile to change the TARGET variable to the + exercice name desired diff --git a/exo1/Makefile b/exo1/Makefile new file mode 100644 index 0000000..b24c120 --- /dev/null +++ b/exo1/Makefile @@ -0,0 +1,31 @@ +TARGET = exo1 +LIBS = +CC = gcc +# Enforce C11 ISO standard for now +CFLAGS = -std=c11 -g -Wall +LDFLAGS = -g -Wall + +.PHONY: default all clean + +default: $(TARGET) +all: default + +OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) +HEADERS = $(wildcard *.h) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c $< -o $@ + +.PRECIOUS: $(TARGET) $(OBJECTS) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -o $@ + +clean: + -rm -f $(TARGET) $(OBJECTS) + +disassemble: $(TARGET) + objdump -d $< | less + +symbols: $(TARGET) + objdump -t $< | sort | less diff --git a/exo1/exo1.c b/exo1/exo1.c new file mode 100644 index 0000000..11fc1fb --- /dev/null +++ b/exo1/exo1.c @@ -0,0 +1,50 @@ +#include +#include + + +void promptValue(const char* msg, int* addr) { + puts(msg); + scanf("%d", addr); +} + +void xorSwap (int *v1, int *v2) { + if (v1 != v2) { + *v1 ^= *v2; + *v2 ^= *v1; + *v1 ^= *v2; + } +} + +void swap(int* v1, int* v2) { + int tmp = *v1; + if (v1 != v2) { + *v1 = *v2; + *v2 = tmp; + } +} + +void displayArray(int* array, int count) { + for (int i = 0; i < count; i++) { + printf("Value in array index %d = %d\n", i, array[i]); + } +} + +bool sortFirst(int* array, int length) { + for (int i = 0; i < length-1; i++) { + if (array[i] > array[i+1]) { + swap(&array[i], &array[i+1]); + return true; + } + } + return false; +} + +int main() { + int array[5]; + for (int i = 0; i < 5; i++) { + promptValue("Valeur ?", &array[i]); + } + displayArray(array, 5); + + return 0; +} diff --git a/exo_skel/Makefile b/exo_skel/Makefile new file mode 100644 index 0000000..51af164 --- /dev/null +++ b/exo_skel/Makefile @@ -0,0 +1,31 @@ +TARGET = exo_skel +LIBS = +CC = gcc +# Enforce C11 ISO standard for now +CFLAGS = -std=c11 -g -Wall +LDFLAGS = -g -Wall + +.PHONY: default all clean + +default: $(TARGET) +all: default + +OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) +HEADERS = $(wildcard *.h) + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c $< -o $@ + +.PRECIOUS: $(TARGET) $(OBJECTS) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) $(LDFLAGS) $(LIBS) -o $@ + +clean: + -rm -f $(TARGET) $(OBJECTS) + +disassemble: $(TARGET) + objdump -d $< | less + +symbols: $(TARGET) + objdump -t $< | sort | less diff --git a/exo_skel/exo_skel.c b/exo_skel/exo_skel.c new file mode 100644 index 0000000..32d1af6 --- /dev/null +++ b/exo_skel/exo_skel.c @@ -0,0 +1,6 @@ +#include + +int main() { + + return 0; +}