From de32237fa31ef30b5242996406dc112462ad3c1d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 11 Feb 2017 22:20:10 +0100 Subject: [PATCH] Initial commit of Polytech'Marseille tutorial C exercices MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 --- .gitignore | 7 +++++++ README | 41 +++++++++++++++++++++++++++++++++++++ exo1/Makefile | 31 ++++++++++++++++++++++++++++ exo1/exo1.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ exo_skel/Makefile | 31 ++++++++++++++++++++++++++++ exo_skel/exo_skel.c | 6 ++++++ 6 files changed, 166 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 exo1/Makefile create mode 100644 exo1/exo1.c create mode 100644 exo_skel/Makefile create mode 100644 exo_skel/exo_skel.c 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; +} -- 2.34.1