Skip to content
Snippets Groups Projects
Commit 0194040b authored by BobLeHibou's avatar BobLeHibou
Browse files

init

parents
Branches
No related tags found
No related merge requests found
*.o
**/*.o
a.out
**/a.out
main
**/main
Makefile 0 → 100644
CC := gcc
CFLAGS := -std=c11 -Wall -Wextra -fsanitize=address -fsanitize=leak -g
SRC = $(wildcard *.c)
all: exec
main: ${SRC}
${CC} ${CFLAGS} ${SRC} -o $@
exec: main
./main
ex1/e1.c 0 → 100644
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
char lower(const char c) {
if ('A' <= c && c <= 'Z') { return c + 32; }
else { return c; }
}
int distance(char s1[], char s2[]) {
int dist = 0;
int i = 0;
char c1, c2;
do {
c1 = s1[i];
c2 = s2[i];
dist += abs(lower(c2) - lower(c1));
++i;
} while (c1 != '\0' && c2 != '\0');
if (c1 == c2) { return dist; }
else { return (-1); }
}
int main() {
char s1[256];
char s2[256];
scanf("%s", s1);
scanf("%s", s2);
printf("\ndistance: %d\n\n", distance(s1, s2));
}
ex2/e2.c 0 → 100644
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define N 4
bool is_normalised(const int **mat, const int side) {
for (int i = 0; i < side; ++i) {
const int norm = i + 1;
if (mat[0][i] != norm) { return false; }
if (mat[i][0] != norm) { return false; }
bool inrow[side];
bool incol[side];
for (int k = 0; k < side; ++k) {
inrow[k] = false;
incol[k] = false;
}
for (int k = 0; k < side; ++k) {
// check rows
const int r = mat[i][k] - 1;
if (r < 0 || r >= side) { return false; }
if (inrow[r]) { return false; }
else { inrow[r] = true; }
// check columns
const int c = mat[k][i] - 1;
if (c < 0 || c >= side) { return false; }
if (incol[c]) { return false; }
else { incol[c] = true; }
}
}
return true;
}
int main() {
int **mat;
mat = malloc(N * sizeof(int*));
for (int i = 0; i < N; ++i) { mat[i] = malloc(N * sizeof(int)); }
for (int row = 0; row < N; ++row) {
for (int col = 0; col < N; ++col) {
scanf("%d", &(mat[row][col]));
}
}
if (is_normalised((const int**) mat, N)) {
printf("\nlatin normalise: oui\n\n");
}
else {
printf("\nlatin normalise: non\n\n");
}
for (int i = 0; i < N; ++i) { free(mat[i]); }
free(mat);
return 0;
}
ex3/e3.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
int count_change_options(const int amount) {
if (amount < 0) { return 0; }
if (amount == 0) { return 1; }
int count = 0;
for (int tens = amount / 10; tens >= 0; --tens) {
const int rem10 = amount - tens * 10;
for (int twos = rem10 / 2; twos >= 0; --twos) {
const int rem2 = rem10 - twos * 2;
#ifdef DEBUG
printf("%d = %d x10 + %d x2 + %d x1\n", amount, tens, twos, rem2);
#endif
++count;
}
}
return count;
}
int main(int argc, char **argv) {
const int amount = atoi(argv[1]);
printf("\n%d\n\n", count_change_options(amount));
return 0;
}
ex4/e4.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#define N 7
int main() {
}
ex5/e5.c 0 → 100644
#include <stdio.h>
void reverse(const int rem);
void reverse(const int rem) {
if (rem <= 0) {
printf("\n"); // copie exemple enonce
return;
}
float num;
scanf("%f", &num);
reverse(rem - 1);
printf("%.2f\n", num);
}
int main(int argc, char **argv) {
reverse(6);
printf("\n");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment