diff --git a/first_quarter/exam/automne_machine/README.html b/first_quarter/exam/automne_machine/README.html new file mode 100644 index 0000000000000000000000000000000000000000..e4e772bbe68fa8331879c2429c581ac4dac1fce6 --- /dev/null +++ b/first_quarter/exam/automne_machine/README.html @@ -0,0 +1,226 @@ +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> +<head> + <meta charset="utf-8" /> + <meta name="generator" content="pandoc" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> + <title>README</title> + <style> + html { + color: #1a1a1a; + background-color: #fdfdfd; + } + body { + margin: 0 auto; + max-width: 36em; + padding-left: 50px; + padding-right: 50px; + padding-top: 50px; + padding-bottom: 50px; + hyphens: auto; + overflow-wrap: break-word; + text-rendering: optimizeLegibility; + font-kerning: normal; + } + @media (max-width: 600px) { + body { + font-size: 0.9em; + padding: 12px; + } + h1 { + font-size: 1.8em; + } + } + @media print { + html { + background-color: white; + } + body { + background-color: transparent; + color: black; + font-size: 12pt; + } + p, h2, h3 { + orphans: 3; + widows: 3; + } + h2, h3, h4 { + page-break-after: avoid; + } + } + p { + margin: 1em 0; + } + a { + color: #1a1a1a; + } + a:visited { + color: #1a1a1a; + } + img { + max-width: 100%; + } + svg { + height: auto; + max-width: 100%; + } + h1, h2, h3, h4, h5, h6 { + margin-top: 1.4em; + } + h5, h6 { + font-size: 1em; + font-style: italic; + } + h6 { + font-weight: normal; + } + ol, ul { + padding-left: 1.7em; + margin-top: 1em; + } + li > ol, li > ul { + margin-top: 0; + } + blockquote { + margin: 1em 0 1em 1.7em; + padding-left: 1em; + border-left: 2px solid #e6e6e6; + color: #606060; + } + code { + font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; + font-size: 85%; + margin: 0; + hyphens: manual; + } + pre { + margin: 1em 0; + overflow: auto; + } + pre code { + padding: 0; + overflow: visible; + overflow-wrap: normal; + } + .sourceCode { + background-color: transparent; + overflow: visible; + } + hr { + background-color: #1a1a1a; + border: none; + height: 1px; + margin: 1em 0; + } + table { + margin: 1em 0; + border-collapse: collapse; + width: 100%; + overflow-x: auto; + display: block; + font-variant-numeric: lining-nums tabular-nums; + } + table caption { + margin-bottom: 0.75em; + } + tbody { + margin-top: 0.5em; + border-top: 1px solid #1a1a1a; + border-bottom: 1px solid #1a1a1a; + } + th { + border-top: 1px solid #1a1a1a; + padding: 0.25em 0.5em 0.25em 0.5em; + } + td { + padding: 0.125em 0.5em 0.25em 0.5em; + } + header { + margin-bottom: 4em; + text-align: center; + } + #TOC li { + list-style: none; + } + #TOC ul { + padding-left: 1.3em; + } + #TOC > ul { + padding-left: 0; + } + #TOC a:not(:hover) { + text-decoration: none; + } + code{white-space: pre-wrap;} + span.smallcaps{font-variant: small-caps;} + div.columns{display: flex; gap: min(4vw, 1.5em);} + div.column{flex: auto; overflow-x: auto;} + div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} + /* The extra [class] is a hack that increases specificity enough to + override a similar rule in reveal.js */ + ul.task-list[class]{list-style: none;} + ul.task-list li input[type="checkbox"] { + font-size: inherit; + width: 0.8em; + margin: 0 0.8em 0.2em -1.6em; + vertical-align: middle; + } + .display.math{display: block; text-align: center; margin: 0.5rem auto;} + </style> +</head> +<body> +<h1 id="test-de-programmation-séquentielle">Test de programmation +séquentielle</h1> +<p>Lisez ce texte attentivement et <strong>JUSQU’À LA FIN</strong> !</p> +<h2 id="énoncés">Énoncés</h2> +<p>Les énoncés ainsi qu’un exécutable d’exemple des trois exercices de +ce test se trouvent dans le répertoire <code>enonces</code> et ont la +structure suivante :</p> +<pre><code>├── enonces +│ ├── ex1 +│ │ ├── README.html +│ │ ├── ex1 +│ ├── ex2 +│ │ ├── README.html +│ │ ├── ex2 +│ ├── ex3 +│ │ ├── README.html +│ │ ├── ex3</code></pre> +<h2 id="templates">Templates</h2> +<p>Votre projet est à effectuer dans le répertoire +<code>templates</code>. Votre projet a la structure suivante (il y a un +template dans le répertoire <code>templates</code>) :</p> +<pre><code>├── templates +│ ├── ex1 +│ │ ├── Makefile +│ │ ├── ex1.c +│ ├── ex2 +│ │ ├── Makefile +│ │ ├── ex2.c +│ ├── ex3 +│ │ ├── Makefile +│ │ ├── ex3.c</code></pre> +<h2 id="examen">Examen</h2> +<p>Le test est composé de trois exercices. Vous n’avez droit qu’à une +feuille A4 recto-verso de notes, ainsi que les slides des cours de +programmation séquentielle et d’algorithmique se trouvant dans le +système de fichier.</p> +<p>Il y a <strong>trois</strong> exercices, et un seul fichier +<code>.c</code> par exercice avec son <code>Makefile</code>. La commande +<code>make</code> doit uniquement permettre de compiler le projet et +générer un exécutable nommé <code>ex1</code>, <code>ex2</code>, +<code>ex3</code>. Par soucis de simplicité, nous vous recommendons de +tout faire dans le fichier <code>.c</code> prévu à cet effet. Si vous +désirez séparer votre code dans divers fichiers, vous en assumerez +l’entière responsabilité. Il faudra absolument corriger le +<code>Makefile</code> pour que tout compile proprement et que nous +puissions évaluer votre travail.</p> +<p>Un code qui ne compile pas ne sera <strong>pas évalué</strong> ! +Ainsi, il vaut mieux rendre quelque chose qui compile mais qui donne un +résultat faux (ou avec des erreurs à l’exécution), plutôt qu’un code qui +ne compile pas mais que vous pensez plus proche de la solution.</p> +<p>Le test dure de 18h30 à 21h30.</p> +<p>Si un exercice ne vous semble pas clair demandez-nous des +précisions.</p> +</body> +</html> diff --git a/first_quarter/exam/automne_machine/README.md b/first_quarter/exam/automne_machine/README.md new file mode 100644 index 0000000000000000000000000000000000000000..5d06591e52f90e7528ccdfa796fd5597771996b3 --- /dev/null +++ b/first_quarter/exam/automne_machine/README.md @@ -0,0 +1,57 @@ +# Test de programmation séquentielle + +Lisez ce texte attentivement et **JUSQU’À LA FIN** ! + +## Énoncés + +Les énoncés ainsi qu’un exécutable d’exemple des trois exercices de ce test se trouvent dans le répertoire `enonces` et ont la structure suivante : + +``` +├── enonces +│ ├── ex1 +│ │ ├── README.html +│ │ ├── ex1 +│ ├── ex2 +│ │ ├── README.html +│ │ ├── ex2 +│ ├── ex3 +│ │ ├── README.html +│ │ ├── ex3 +``` + +## Templates + +Votre projet est à effectuer dans le répertoire `templates`. +Votre projet a la structure suivante (il y a un template dans le répertoire `templates`) : + +``` +├── templates +│ ├── ex1 +│ │ ├── Makefile +│ │ ├── ex1.c +│ ├── ex2 +│ │ ├── Makefile +│ │ ├── ex2.c +│ ├── ex3 +│ │ ├── Makefile +│ │ ├── ex3.c +``` + +## Examen + +Le test est composé de trois exercices. +Vous n’avez droit qu’à une feuille A4 recto-verso de notes, ainsi que les slides des cours de programmation séquentielle et d’algorithmique se trouvant dans le système de fichier. + +Il y a **trois** exercices, et un seul fichier `.c` par exercice avec son `Makefile`. +La commande `make` doit uniquement permettre de compiler le projet et générer un exécutable nommé `ex1`, `ex2`, `ex3`. +Par soucis de simplicité, nous vous recommendons de tout faire dans le fichier `.c` prévu à cet effet. +Si vous désirez séparer votre code dans divers fichiers, vous en assumerez l’entière responsabilité. +Il faudra absolument corriger le `Makefile` pour que tout compile proprement et que nous puissions évaluer votre travail. + +Un code qui ne compile pas ne sera **pas évalué** ! +Ainsi, il vaut mieux rendre quelque chose qui compile mais qui donne un résultat faux (ou avec des erreurs à l’exécution), plutôt qu’un code qui ne compile pas mais que vous pensez plus proche de la solution. + +Le test dure de 18h30 à 21h30. + +Si un exercice ne vous semble pas clair demandez-nous des précisions. + diff --git a/first_quarter/exam/automne_machine/enonces/ex1/README.html b/first_quarter/exam/automne_machine/enonces/ex1/README.html new file mode 100644 index 0000000000000000000000000000000000000000..03f2aeba4ce89444a53f8b5eba47a34e8150fb6d --- /dev/null +++ b/first_quarter/exam/automne_machine/enonces/ex1/README.html @@ -0,0 +1,208 @@ +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> +<head> + <meta charset="utf-8" /> + <meta name="generator" content="pandoc" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> + <title>README</title> + <style> + html { + color: #1a1a1a; + background-color: #fdfdfd; + } + body { + margin: 0 auto; + max-width: 36em; + padding-left: 50px; + padding-right: 50px; + padding-top: 50px; + padding-bottom: 50px; + hyphens: auto; + overflow-wrap: break-word; + text-rendering: optimizeLegibility; + font-kerning: normal; + } + @media (max-width: 600px) { + body { + font-size: 0.9em; + padding: 12px; + } + h1 { + font-size: 1.8em; + } + } + @media print { + html { + background-color: white; + } + body { + background-color: transparent; + color: black; + font-size: 12pt; + } + p, h2, h3 { + orphans: 3; + widows: 3; + } + h2, h3, h4 { + page-break-after: avoid; + } + } + p { + margin: 1em 0; + } + a { + color: #1a1a1a; + } + a:visited { + color: #1a1a1a; + } + img { + max-width: 100%; + } + svg { + height: auto; + max-width: 100%; + } + h1, h2, h3, h4, h5, h6 { + margin-top: 1.4em; + } + h5, h6 { + font-size: 1em; + font-style: italic; + } + h6 { + font-weight: normal; + } + ol, ul { + padding-left: 1.7em; + margin-top: 1em; + } + li > ol, li > ul { + margin-top: 0; + } + blockquote { + margin: 1em 0 1em 1.7em; + padding-left: 1em; + border-left: 2px solid #e6e6e6; + color: #606060; + } + code { + font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; + font-size: 85%; + margin: 0; + hyphens: manual; + } + pre { + margin: 1em 0; + overflow: auto; + } + pre code { + padding: 0; + overflow: visible; + overflow-wrap: normal; + } + .sourceCode { + background-color: transparent; + overflow: visible; + } + hr { + background-color: #1a1a1a; + border: none; + height: 1px; + margin: 1em 0; + } + table { + margin: 1em 0; + border-collapse: collapse; + width: 100%; + overflow-x: auto; + display: block; + font-variant-numeric: lining-nums tabular-nums; + } + table caption { + margin-bottom: 0.75em; + } + tbody { + margin-top: 0.5em; + border-top: 1px solid #1a1a1a; + border-bottom: 1px solid #1a1a1a; + } + th { + border-top: 1px solid #1a1a1a; + padding: 0.25em 0.5em 0.25em 0.5em; + } + td { + padding: 0.125em 0.5em 0.25em 0.5em; + } + header { + margin-bottom: 4em; + text-align: center; + } + #TOC li { + list-style: none; + } + #TOC ul { + padding-left: 1.3em; + } + #TOC > ul { + padding-left: 0; + } + #TOC a:not(:hover) { + text-decoration: none; + } + code{white-space: pre-wrap;} + span.smallcaps{font-variant: small-caps;} + div.columns{display: flex; gap: min(4vw, 1.5em);} + div.column{flex: auto; overflow-x: auto;} + div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} + /* The extra [class] is a hack that increases specificity enough to + override a similar rule in reveal.js */ + ul.task-list[class]{list-style: none;} + ul.task-list li input[type="checkbox"] { + font-size: inherit; + width: 0.8em; + margin: 0 0.8em 0.2em -1.6em; + vertical-align: middle; + } + .display.math{display: block; text-align: center; margin: 0.5rem auto;} + </style> +</head> +<body> +<h1 id="exercice-1-lancer-de-dés">Exercice 1 : Lancer de dés</h1> +<p>Dans cet exercice, il faut simuler un lancer de dé à six faces, +compter le nombre d’occurrence de chaque résultat et stocker ces nombres +dans un tableau. À la fin de l’exécution, il faut afficher le nombre de +fois où chaque nombre est obtenu ainsi que son taux d’apparition. Le +nombre de lancer de dés est à lire sur la ligne de commande. Ainsi le +lancement de votre programme doit être</p> +<pre class="console"><code>./ex1 <N></code></pre> +<p>où <code>N</code> est le nombre de fois où le dé est lancé.</p> +<p><strong>Attention:</strong> Vous devez initialiser votre générateur +de nombres aléatoire avec la graine <code>0</code>.</p> +<h2 id="exécutions">Exécutions</h2> +<p>L’exécution du programme suivant doit donner exactement cette +solution :</p> +<pre class="console"><code>./ex1 100 +La face 1 est sortie 17 fois, soit 17.0%. +La face 2 est sortie 18 fois, soit 18.0%. +La face 3 est sortie 23 fois, soit 23.0%. +La face 4 est sortie 18 fois, soit 18.0%. +La face 5 est sortie 12 fois, soit 12.0%. +La face 6 est sortie 12 fois, soit 12.0%.</code></pre> +<pre class="console"><code>./ex1 1000 +La face 1 est sortie 173 fois, soit 17.3%. +La face 2 est sortie 150 fois, soit 15.0%. +La face 3 est sortie 179 fois, soit 17.9%. +La face 4 est sortie 181 fois, soit 18.1%. +La face 5 est sortie 155 fois, soit 15.5%. +La face 6 est sortie 162 fois, soit 16.2%.</code></pre> +<pre class="console"><code>./ex1 10001 +La face 1 est sortie 1651 fois, soit 16.5%. +La face 2 est sortie 1622 fois, soit 16.2%. +La face 3 est sortie 1743 fois, soit 17.4%. +La face 4 est sortie 1673 fois, soit 16.7%. +La face 5 est sortie 1693 fois, soit 16.9%. +La face 6 est sortie 1619 fois, soit 16.2%.</code></pre> +</body> +</html> diff --git a/first_quarter/exam/automne_machine/enonces/ex1/README.md b/first_quarter/exam/automne_machine/enonces/ex1/README.md new file mode 100644 index 0000000000000000000000000000000000000000..c163d3233a2815554edac6828584a987cb21c79e --- /dev/null +++ b/first_quarter/exam/automne_machine/enonces/ex1/README.md @@ -0,0 +1,48 @@ +# Exercice 1 : Lancer de dés + +Dans cet exercice, il faut simuler un lancer de dé à six faces, compter le nombre d'occurrence de chaque résultat et stocker ces nombres dans un tableau. +À la fin de l'exécution, il faut afficher le nombre de fois où chaque nombre est obtenu ainsi que son taux d’apparition. +Le nombre de lancer de dés est à lire sur la ligne de commande. +Ainsi le lancement de votre programme doit être +```console +./ex1 <N> +``` +où `N` est le nombre de fois où le dé est lancé. + +**Attention:** Vous devez initialiser votre générateur de nombres aléatoire avec la graine `0`. + +## Exécutions + +L'exécution du programme suivant doit donner exactement cette solution : + +```console +./ex1 100 +La face 1 est sortie 17 fois, soit 17.0%. +La face 2 est sortie 18 fois, soit 18.0%. +La face 3 est sortie 23 fois, soit 23.0%. +La face 4 est sortie 18 fois, soit 18.0%. +La face 5 est sortie 12 fois, soit 12.0%. +La face 6 est sortie 12 fois, soit 12.0%. +``` + +```console +./ex1 1000 +La face 1 est sortie 173 fois, soit 17.3%. +La face 2 est sortie 150 fois, soit 15.0%. +La face 3 est sortie 179 fois, soit 17.9%. +La face 4 est sortie 181 fois, soit 18.1%. +La face 5 est sortie 155 fois, soit 15.5%. +La face 6 est sortie 162 fois, soit 16.2%. +``` + +```console +./ex1 10001 +La face 1 est sortie 1651 fois, soit 16.5%. +La face 2 est sortie 1622 fois, soit 16.2%. +La face 3 est sortie 1743 fois, soit 17.4%. +La face 4 est sortie 1673 fois, soit 16.7%. +La face 5 est sortie 1693 fois, soit 16.9%. +La face 6 est sortie 1619 fois, soit 16.2%. +``` + + diff --git a/first_quarter/exam/automne_machine/enonces/ex2/README.html b/first_quarter/exam/automne_machine/enonces/ex2/README.html new file mode 100644 index 0000000000000000000000000000000000000000..f21135c786de867a0c2cb1e78a74bd484df430ef --- /dev/null +++ b/first_quarter/exam/automne_machine/enonces/ex2/README.html @@ -0,0 +1,236 @@ +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> +<head> + <meta charset="utf-8" /> + <meta name="generator" content="pandoc" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> + <title>README</title> + <style> + html { + color: #1a1a1a; + background-color: #fdfdfd; + } + body { + margin: 0 auto; + max-width: 36em; + padding-left: 50px; + padding-right: 50px; + padding-top: 50px; + padding-bottom: 50px; + hyphens: auto; + overflow-wrap: break-word; + text-rendering: optimizeLegibility; + font-kerning: normal; + } + @media (max-width: 600px) { + body { + font-size: 0.9em; + padding: 12px; + } + h1 { + font-size: 1.8em; + } + } + @media print { + html { + background-color: white; + } + body { + background-color: transparent; + color: black; + font-size: 12pt; + } + p, h2, h3 { + orphans: 3; + widows: 3; + } + h2, h3, h4 { + page-break-after: avoid; + } + } + p { + margin: 1em 0; + } + a { + color: #1a1a1a; + } + a:visited { + color: #1a1a1a; + } + img { + max-width: 100%; + } + svg { + height: auto; + max-width: 100%; + } + h1, h2, h3, h4, h5, h6 { + margin-top: 1.4em; + } + h5, h6 { + font-size: 1em; + font-style: italic; + } + h6 { + font-weight: normal; + } + ol, ul { + padding-left: 1.7em; + margin-top: 1em; + } + li > ol, li > ul { + margin-top: 0; + } + blockquote { + margin: 1em 0 1em 1.7em; + padding-left: 1em; + border-left: 2px solid #e6e6e6; + color: #606060; + } + code { + font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; + font-size: 85%; + margin: 0; + hyphens: manual; + } + pre { + margin: 1em 0; + overflow: auto; + } + pre code { + padding: 0; + overflow: visible; + overflow-wrap: normal; + } + .sourceCode { + background-color: transparent; + overflow: visible; + } + hr { + background-color: #1a1a1a; + border: none; + height: 1px; + margin: 1em 0; + } + table { + margin: 1em 0; + border-collapse: collapse; + width: 100%; + overflow-x: auto; + display: block; + font-variant-numeric: lining-nums tabular-nums; + } + table caption { + margin-bottom: 0.75em; + } + tbody { + margin-top: 0.5em; + border-top: 1px solid #1a1a1a; + border-bottom: 1px solid #1a1a1a; + } + th { + border-top: 1px solid #1a1a1a; + padding: 0.25em 0.5em 0.25em 0.5em; + } + td { + padding: 0.125em 0.5em 0.25em 0.5em; + } + header { + margin-bottom: 4em; + text-align: center; + } + #TOC li { + list-style: none; + } + #TOC ul { + padding-left: 1.3em; + } + #TOC > ul { + padding-left: 0; + } + #TOC a:not(:hover) { + text-decoration: none; + } + code{white-space: pre-wrap;} + span.smallcaps{font-variant: small-caps;} + div.columns{display: flex; gap: min(4vw, 1.5em);} + div.column{flex: auto; overflow-x: auto;} + div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} + /* The extra [class] is a hack that increases specificity enough to + override a similar rule in reveal.js */ + ul.task-list[class]{list-style: none;} + ul.task-list li input[type="checkbox"] { + font-size: inherit; + width: 0.8em; + margin: 0 0.8em 0.2em -1.6em; + vertical-align: middle; + } + .display.math{display: block; text-align: center; margin: 0.5rem auto;} + </style> +</head> +<body> +<h1 id="exercice-2-occurrences-dune-sous-chaine">Exercice 2 : +occurrences d’une sous-chaine</h1> +<p>On souhaite écrire un programme qui permet de compter le nombre +d’occurrences d’une sous-chaine de caractères dans une chaine de +caractères.</p> +<p>Votre détection ne doit pas être sensible à la casse (c’est à dire il +ne doit pas y avoir de différence entre les minuscules et les +majuscules).</p> +<p>Ainsi pour la chaine <code>Neeeeed</code> :</p> +<ul> +<li>et la sous-chaine <code>eee</code> le résultat est +<code>3</code> ;</li> +<li>et la sous-chaine <code>ne</code> le résultat est +<code>1</code> ;</li> +<li>et la sous-chaine <code>Pierre</code> le résultat est +<code>0</code>.</li> +</ul> +<h2 id="paramètres">Paramètres</h2> +<p>Les deux chaines doivent être passées au programme comme paramètres +de la fonction <code>main</code>. Le lancement de votre programme doit +être :</p> +<pre class="console"><code>./ex2 <mot> <sous-chaine></code></pre> +<p><em>Pensez à contrôler ces paramètres.</em></p> +<h2 id="affichage">Affichage</h2> +<p>Votre programme doit afficher le résultat en fin d’exécution.</p> +<h2 id="indications">Indications</h2> +<p>La table des caractères <a +href="https://fr.wikipedia.org/wiki/American_Standard_Code_for_Information_Interchange">ASCII</a> +peut vous être utile.</p> +<pre><code> Dec Char Dec Char Dec Char Dec Char +---------- --------- --------- ---------- + 0 NUL 32 SPACE 64 @ 96 ` + 1 SOH 33 ! 65 A 97 a + 2 STX 34 " 66 B 98 b + 3 ETX 35 # 67 C 99 c + 4 EOT 36 $ 68 D 100 d + 5 ENQ 37 % 69 E 101 e + 6 ACK 38 & 70 F 102 f + 7 BEL 39 ' 71 G 103 g + 8 BS 40 ( 72 H 104 h + 9 TAB 41 ) 73 I 105 i + 10 LF 42 * 74 J 106 j + 11 VT 43 + 75 K 107 k + 12 FF 44 , 76 L 108 l + 13 CR 45 - 77 M 109 m + 14 SO 46 . 78 N 110 n + 15 SI 47 / 79 O 111 o + 16 DLE 48 0 80 P 112 p + 17 DC1 49 1 81 Q 113 q + 18 DC2 50 2 82 R 114 r + 19 DC3 51 3 83 S 115 s + 20 DC4 52 4 84 T 116 t + 21 NAK 53 5 85 U 117 u + 22 SYN 54 6 86 V 118 v + 23 ETB 55 7 87 W 119 w + 24 CAN 56 8 88 X 120 x + 25 EM 57 9 89 Y 121 y + 26 SUB 58 : 90 Z 122 z + 27 ESC 59 ; 91 [ 123 { + 28 FS 60 < 92 \ 124 | + 29 GS 61 = 93 ] 125 } + 30 RS 62 > 94 ^ 126 ~ + 31 US 63 ? 95 _ 127 DEL</code></pre> +</body> +</html> diff --git a/first_quarter/exam/automne_machine/enonces/ex2/README.md b/first_quarter/exam/automne_machine/enonces/ex2/README.md new file mode 100644 index 0000000000000000000000000000000000000000..106ae20e7f489b6d292e145c2882f63896182976 --- /dev/null +++ b/first_quarter/exam/automne_machine/enonces/ex2/README.md @@ -0,0 +1,69 @@ +# Exercice 2 : occurrences d’une sous-chaine + +On souhaite écrire un programme qui permet de compter le nombre d’occurrences d’une sous-chaine de caractères dans une chaine de caractères. + +Votre détection ne doit pas être sensible à la casse (c’est à dire il ne doit pas y avoir de différence entre les minuscules et les majuscules). + +Ainsi pour la chaine `Neeeeed` : + +- et la sous-chaine `eee` le résultat est `3` ; +- et la sous-chaine `ne` le résultat est `1` ; +- et la sous-chaine `Pierre` le résultat est `0`. + +## Paramètres + +Les deux chaines doivent être passées au programme comme paramètres de la fonction `main`. +Le lancement de votre programme doit être : +```console +./ex2 <mot> <sous-chaine> +``` + +*Pensez à contrôler ces paramètres.* + +## Affichage + +Votre programme doit afficher le résultat en fin d’exécution. + +## Indications + +La table des caractères [ASCII](https://fr.wikipedia.org/wiki/American_Standard_Code_for_Information_Interchange) peut vous être utile. + +``` + Dec Char Dec Char Dec Char Dec Char +---------- --------- --------- ---------- + 0 NUL 32 SPACE 64 @ 96 ` + 1 SOH 33 ! 65 A 97 a + 2 STX 34 " 66 B 98 b + 3 ETX 35 # 67 C 99 c + 4 EOT 36 $ 68 D 100 d + 5 ENQ 37 % 69 E 101 e + 6 ACK 38 & 70 F 102 f + 7 BEL 39 ' 71 G 103 g + 8 BS 40 ( 72 H 104 h + 9 TAB 41 ) 73 I 105 i + 10 LF 42 * 74 J 106 j + 11 VT 43 + 75 K 107 k + 12 FF 44 , 76 L 108 l + 13 CR 45 - 77 M 109 m + 14 SO 46 . 78 N 110 n + 15 SI 47 / 79 O 111 o + 16 DLE 48 0 80 P 112 p + 17 DC1 49 1 81 Q 113 q + 18 DC2 50 2 82 R 114 r + 19 DC3 51 3 83 S 115 s + 20 DC4 52 4 84 T 116 t + 21 NAK 53 5 85 U 117 u + 22 SYN 54 6 86 V 118 v + 23 ETB 55 7 87 W 119 w + 24 CAN 56 8 88 X 120 x + 25 EM 57 9 89 Y 121 y + 26 SUB 58 : 90 Z 122 z + 27 ESC 59 ; 91 [ 123 { + 28 FS 60 < 92 \ 124 | + 29 GS 61 = 93 ] 125 } + 30 RS 62 > 94 ^ 126 ~ + 31 US 63 ? 95 _ 127 DEL +``` + + + diff --git a/first_quarter/exam/automne_machine/enonces/ex3/README.html b/first_quarter/exam/automne_machine/enonces/ex3/README.html new file mode 100644 index 0000000000000000000000000000000000000000..6cc89b32fcd10374159cfe4da637dc54e52ba40d --- /dev/null +++ b/first_quarter/exam/automne_machine/enonces/ex3/README.html @@ -0,0 +1,193 @@ +<!DOCTYPE html> +<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang=""> +<head> + <meta charset="utf-8" /> + <meta name="generator" content="pandoc" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> + <title>README</title> + <style> + html { + color: #1a1a1a; + background-color: #fdfdfd; + } + body { + margin: 0 auto; + max-width: 36em; + padding-left: 50px; + padding-right: 50px; + padding-top: 50px; + padding-bottom: 50px; + hyphens: auto; + overflow-wrap: break-word; + text-rendering: optimizeLegibility; + font-kerning: normal; + } + @media (max-width: 600px) { + body { + font-size: 0.9em; + padding: 12px; + } + h1 { + font-size: 1.8em; + } + } + @media print { + html { + background-color: white; + } + body { + background-color: transparent; + color: black; + font-size: 12pt; + } + p, h2, h3 { + orphans: 3; + widows: 3; + } + h2, h3, h4 { + page-break-after: avoid; + } + } + p { + margin: 1em 0; + } + a { + color: #1a1a1a; + } + a:visited { + color: #1a1a1a; + } + img { + max-width: 100%; + } + svg { + height: auto; + max-width: 100%; + } + h1, h2, h3, h4, h5, h6 { + margin-top: 1.4em; + } + h5, h6 { + font-size: 1em; + font-style: italic; + } + h6 { + font-weight: normal; + } + ol, ul { + padding-left: 1.7em; + margin-top: 1em; + } + li > ol, li > ul { + margin-top: 0; + } + blockquote { + margin: 1em 0 1em 1.7em; + padding-left: 1em; + border-left: 2px solid #e6e6e6; + color: #606060; + } + code { + font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace; + font-size: 85%; + margin: 0; + hyphens: manual; + } + pre { + margin: 1em 0; + overflow: auto; + } + pre code { + padding: 0; + overflow: visible; + overflow-wrap: normal; + } + .sourceCode { + background-color: transparent; + overflow: visible; + } + hr { + background-color: #1a1a1a; + border: none; + height: 1px; + margin: 1em 0; + } + table { + margin: 1em 0; + border-collapse: collapse; + width: 100%; + overflow-x: auto; + display: block; + font-variant-numeric: lining-nums tabular-nums; + } + table caption { + margin-bottom: 0.75em; + } + tbody { + margin-top: 0.5em; + border-top: 1px solid #1a1a1a; + border-bottom: 1px solid #1a1a1a; + } + th { + border-top: 1px solid #1a1a1a; + padding: 0.25em 0.5em 0.25em 0.5em; + } + td { + padding: 0.125em 0.5em 0.25em 0.5em; + } + header { + margin-bottom: 4em; + text-align: center; + } + #TOC li { + list-style: none; + } + #TOC ul { + padding-left: 1.3em; + } + #TOC > ul { + padding-left: 0; + } + #TOC a:not(:hover) { + text-decoration: none; + } + code{white-space: pre-wrap;} + span.smallcaps{font-variant: small-caps;} + div.columns{display: flex; gap: min(4vw, 1.5em);} + div.column{flex: auto; overflow-x: auto;} + div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} + /* The extra [class] is a hack that increases specificity enough to + override a similar rule in reveal.js */ + ul.task-list[class]{list-style: none;} + ul.task-list li input[type="checkbox"] { + font-size: inherit; + width: 0.8em; + margin: 0 0.8em 0.2em -1.6em; + vertical-align: middle; + } + .display.math{display: block; text-align: center; margin: 0.5rem auto;} + </style> +</head> +<body> +<h1 id="exercice-3-fusion-de-tableaux">Exercice 3 : fusion de +tableaux</h1> +<p>On désire implémenter une fonction <code>fusion</code> qui prend deux +chaînes de caractères en argument et renvoie une chaîne de caractères +dans laquelle les lettres sont prises alternativement de la première et +de la seconde chaîne. Lorsque toutes les lettres d’une des deux chaînes +ont été prises, on copie simplement toutes les lettres restantes dans la +chaîne finale.</p> +<p>Toutes les <strong>allocations de mémoire de tableaux</strong> +doivent être faites <strong>dynamiquement</strong>. Pensez donc à bien +libérer la mémoire.</p> +<p>Ainsi pour les exécutions suivantes, on aura comme résultat :</p> +<pre><code>./ex3 Mary Louise +Chaîne fusionnée: MLaoruyise</code></pre> +<pre><code>./ex3 Wollstonecraft Michel +Chaîne fusionnée: WMoilclhsetlonecraft</code></pre> +<h2 id="compilation">Compilation</h2> +<p>Vous noterez que le <em>Makefile</em> fourni contient les options de +compilation suivantes :</p> +<pre><code>-g -fsanitize=address -fsanitize=undefined -fsanitize=leak</code></pre> +</body> +</html> diff --git a/first_quarter/exam/automne_machine/enonces/ex3/README.md b/first_quarter/exam/automne_machine/enonces/ex3/README.md new file mode 100644 index 0000000000000000000000000000000000000000..af7f3eef97b7e9ab17aa396ac79adec698840d90 --- /dev/null +++ b/first_quarter/exam/automne_machine/enonces/ex3/README.md @@ -0,0 +1,27 @@ +# Exercice 3 : fusion de tableaux + +On désire implémenter une fonction `fusion` qui prend deux chaînes de caractères en argument et renvoie une chaîne de caractères dans laquelle les lettres sont prises alternativement de la première et de la seconde chaîne. +Lorsque toutes les lettres d’une des deux chaînes ont été prises, on copie simplement toutes les lettres restantes dans la chaîne finale. + +Toutes les **allocations de mémoire de tableaux** doivent être faites **dynamiquement**. +Pensez donc à bien libérer la mémoire. + +Ainsi pour les exécutions suivantes, on aura comme résultat : + +``` +./ex3 Mary Louise +Chaîne fusionnée: MLaoruyise +``` + +``` +./ex3 Wollstonecraft Michel +Chaîne fusionnée: WMoilclhsetlonecraft +``` + +## Compilation + +Vous noterez que le *Makefile* fourni contient les options de compilation suivantes : + +``` +-g -fsanitize=address -fsanitize=undefined -fsanitize=leak +``` diff --git a/first_quarter/exam/automne_machine/templates/ex1/Makefile b/first_quarter/exam/automne_machine/templates/ex1/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..6103d2b5f278bb6fe49a6418784bcb0d6a3bf53b --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex1/Makefile @@ -0,0 +1,11 @@ +CC = gcc -std=c11 -g +CFLAGS = -Wall -Wextra -Wpedantic + +main: main.c ex1.c ex1.h + $(CC) $(CFLAGS) -o $@ main.c ex1.c + +run: main + ./main 100 + +clean: + rm -f main \ No newline at end of file diff --git a/first_quarter/exam/automne_machine/templates/ex1/ex1.c b/first_quarter/exam/automne_machine/templates/ex1/ex1.c new file mode 100644 index 0000000000000000000000000000000000000000..9ad4f9d27ab5ed2349a6928679d2b16fcf78d765 --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex1/ex1.c @@ -0,0 +1,29 @@ +#include "ex1.h" + +//Helper functions +int rand_gen(int max_faces){ + return rand() % max_faces + 1; +} + +void int_to_array(int *tab, int size){ + for(int i = 0;i < size;i++){ + tab[i] = rand_gen(MAX_FACES); + } +} + +void count_occ(int *tab, int size){ + int occ[MAX_FACES] = {0}; + //count + for(int i = 0;i < size;i++){ + if(tab[i] >= 1 && tab[i] < MAX_FACES){ + occ[tab[i]- 1]++; + } + } + //Print + for(int i = 1; i <= MAX_FACES;i++){ + double percentage = (occ[i-1])/((double)size) * 100.0; + printf("Face %d: %d occurences, %.2f%%\n", i, occ[i -1], percentage); + } + +} + diff --git a/first_quarter/exam/automne_machine/templates/ex1/ex1.h b/first_quarter/exam/automne_machine/templates/ex1/ex1.h new file mode 100644 index 0000000000000000000000000000000000000000..573fb22395a39037db4a31db05b91e2e12c43bd5 --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex1/ex1.h @@ -0,0 +1,17 @@ +#ifndef EX1_H +#define EX1_H + +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +#define MAX_FACES 6 + +int rand_gen(int max_faces); + +void int_to_array(int *tab, int size); + +void count_occ(int *tab, int size); + + +#endif \ No newline at end of file diff --git a/first_quarter/exam/automne_machine/templates/ex1/main b/first_quarter/exam/automne_machine/templates/ex1/main new file mode 100755 index 0000000000000000000000000000000000000000..c6e9992768e2a361094bcee8598552cf70351008 Binary files /dev/null and b/first_quarter/exam/automne_machine/templates/ex1/main differ diff --git a/first_quarter/exam/automne_machine/templates/ex1/main.c b/first_quarter/exam/automne_machine/templates/ex1/main.c new file mode 100644 index 0000000000000000000000000000000000000000..d82f3be853fd4e8e4ddb9bddedb723e20a99db1c --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex1/main.c @@ -0,0 +1,30 @@ +#include "ex1.h" + +int main(int argc, char *argv[]) { + if(argc != 2){ + fprintf(stderr,"Usage:<%s><number of tries>", argv[0]); + return EXIT_FAILURE; + } + + srand(time(NULL)); + + int num_tries = atoi(argv[1]); + if (num_tries <= 0) { + fprintf(stderr, "Number of tries must be positive\n"); + return EXIT_FAILURE; + } + + int *tab = (int *)malloc(num_tries * sizeof(int)); + if (tab == NULL) { + fprintf(stderr, "Memory allocation failed\n"); + return EXIT_FAILURE; + } + + int_to_array(tab,num_tries); + count_occ(tab, num_tries); + + free(tab); + + + return EXIT_SUCCESS; +} diff --git a/first_quarter/exam/automne_machine/templates/ex2/Makefile b/first_quarter/exam/automne_machine/templates/ex2/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..8bbd40be2779af45b900e53bea0218084c64541c --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex2/Makefile @@ -0,0 +1,12 @@ +CC = gcc -std=c11 -g +CFLAGS = -Wall -Wextra -Wpedantic + +main: main.c ex2.c ex2.h + ${CC} ${CFLAGS} -o $@ main.c ex2.c + +run: main + ./main Neeeeed eee + +clean: + rm -f main + diff --git a/first_quarter/exam/automne_machine/templates/ex2/ex2.c b/first_quarter/exam/automne_machine/templates/ex2/ex2.c new file mode 100644 index 0000000000000000000000000000000000000000..10d219bdb11c30d4800af0d97708ba99fe953810 --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex2/ex2.c @@ -0,0 +1,12 @@ +#include "ex2.h" + +int sub_string_occ(char *s_str, char *str){ + int count = 0; + char *tmp_str = str; + while((tmp_str = strstr(tmp_str,s_str)) != NULL){ + count ++; + tmp_str ++; + } + return count; +} + diff --git a/first_quarter/exam/automne_machine/templates/ex2/ex2.h b/first_quarter/exam/automne_machine/templates/ex2/ex2.h new file mode 100644 index 0000000000000000000000000000000000000000..3f0ab60159df11fc3bc8423a94dd5ee0a292e8cd --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex2/ex2.h @@ -0,0 +1,11 @@ +#ifndef EX2_H +#define EX2_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> + +int sub_string_occ(char *s_str, char *str); + +#endif \ No newline at end of file diff --git a/first_quarter/exam/automne_machine/templates/ex2/main b/first_quarter/exam/automne_machine/templates/ex2/main new file mode 100755 index 0000000000000000000000000000000000000000..2ad53fe5531e88fee418d7d31028fd85c49367d4 Binary files /dev/null and b/first_quarter/exam/automne_machine/templates/ex2/main differ diff --git a/first_quarter/exam/automne_machine/templates/ex2/main.c b/first_quarter/exam/automne_machine/templates/ex2/main.c new file mode 100644 index 0000000000000000000000000000000000000000..29a3af89f4131ee38b33407cdf9834c3d7eb9b80 --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex2/main.c @@ -0,0 +1,35 @@ +#include "ex2.h" + +int main(int argc, char *argv[]) { + if(argc != 3){ + fprintf(stderr,"Usage:<%s><string><sub-string>\n",argv[0]); + return EXIT_FAILURE; + } + + char *str = (char *)malloc((strlen(argv[1]) + 1) * sizeof(char)); + if(str == NULL){ + fprintf(stderr,"Memory allocation error\n"); + } + char *s_str = (char *)malloc((strlen(argv[2]) + 1) * sizeof(char)); + if(str == NULL){ + fprintf(stderr,"Memory allocation error\n"); + free(str); + } + + + + + str = argv[1]; + s_str = argv[2]; + + + int count_s_str = sub_string_occ(s_str,str); + + printf("For sub-string (%s) there are (%d) occurences in string (%s) \n",argv[2], count_s_str, argv[1]); + + //free(str); + //free(s_str); + + + return EXIT_SUCCESS; +} diff --git a/first_quarter/exam/automne_machine/templates/ex3/Makefile b/first_quarter/exam/automne_machine/templates/ex3/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..b557fb49c9cc8e2b1981bca6fe0258407b080435 --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex3/Makefile @@ -0,0 +1,15 @@ +CC = gcc -std=c11 -g +CFLAGS = -Wall -Wextra -Wpedantic +SANITIZER = -fsanitize=leak -fsanitize=undefined -fsanitize=address + +main: main.c ex3.c ex3.h + ${CC} ${CFLAGS} ${SANITIZER} -o $@ main.c ex3.c + +run: main + ./main Mary Louise + +clean: + rm -f main + + + diff --git a/first_quarter/exam/automne_machine/templates/ex3/ex3.c b/first_quarter/exam/automne_machine/templates/ex3/ex3.c new file mode 100644 index 0000000000000000000000000000000000000000..7260fd4710f855815eae6ac205b11cdb1ff91901 --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex3/ex3.c @@ -0,0 +1,18 @@ +#include "ex3.h" + +char *fusion(char *str1, char *str2, char *tmp_str){ + int len_1 = strlen(str1); + int len_2 = strlen(str2); + int len = len_1 + len_2; + int i_1 = 0, i_2 = 0; + for(int i = 0;i < len;i++){ + if(i % 2 == 0 && i_1 < len_1){ + tmp_str[i] = str1[i_1++]; + }else if(i_2 < len_2){ + tmp_str[i] = str2[i_2++]; + } + } + tmp_str[len] = '\0'; + return tmp_str; +} + diff --git a/first_quarter/exam/automne_machine/templates/ex3/ex3.h b/first_quarter/exam/automne_machine/templates/ex3/ex3.h new file mode 100644 index 0000000000000000000000000000000000000000..2e166c76f1669e0b108f0433e0947edaa7abf0ed --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex3/ex3.h @@ -0,0 +1,11 @@ +#ifndef EX3_H +#define EX3_H + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +char *fusion(char *str1, char *str2, char *tmp_str); + +#endif diff --git a/first_quarter/exam/automne_machine/templates/ex3/main b/first_quarter/exam/automne_machine/templates/ex3/main new file mode 100755 index 0000000000000000000000000000000000000000..a3a9f6615fee68a9591cdcf40151b02b0fb82933 Binary files /dev/null and b/first_quarter/exam/automne_machine/templates/ex3/main differ diff --git a/first_quarter/exam/automne_machine/templates/ex3/main.c b/first_quarter/exam/automne_machine/templates/ex3/main.c new file mode 100644 index 0000000000000000000000000000000000000000..d81cc83cb312a0d4e2818a6888bd3bb73dadd77f --- /dev/null +++ b/first_quarter/exam/automne_machine/templates/ex3/main.c @@ -0,0 +1,46 @@ +#include "ex3.h" + +int main(int argc, char *argv[]) { + if(argc != 3){ + fprintf(stderr, "Usage:<%s><first string><Second string>\n", argv[0]); + return EXIT_FAILURE; + } + + int len_1 = strlen(argv[1]); + int len_2 = strlen(argv[2]); + + char *str1 = (char *)malloc((len_1 + 1) * sizeof(char)); + if(str1 == NULL){ + fprintf(stderr,"Memory allocation error\n"); + return EXIT_FAILURE; + } + strcpy(str1,argv[1]); + + char *str2 = (char *)malloc((len_2 + 1) * sizeof(char)); + if(str2 == NULL){ + fprintf(stderr,"Memory allocation error\n"); + free(str1); + return EXIT_FAILURE; + } + strcpy(str2,argv[2]); + + char *tmp_str = (char *)malloc((len_1 +len_2 + 1) * sizeof(char)); + if(tmp_str == NULL){ + fprintf(stderr,"Memory allocation error\n"); + free(str1); + free(str2); + return EXIT_FAILURE; + } + + tmp_str = fusion(str1,str2,tmp_str); + + free(str1); + free(str2); + + printf("merge string: %s\n", tmp_str); + + free(tmp_str); + + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/first_quarter/programs/arrays_manipulation.c b/first_quarter/programs/arrays_manipulation.c new file mode 100644 index 0000000000000000000000000000000000000000..72efd221224abb25ec46f656937d0107091ab6de --- /dev/null +++ b/first_quarter/programs/arrays_manipulation.c @@ -0,0 +1,48 @@ +#include "first_e.h" + +int disaggregate_int(int a, int *tab, int *tab_counts){ + //1. Memory allocation + int size = sizeof(tab)/sizeof(tab[0]); + tab = (int *)malloc(size * sizeof(int)); + tab_counts = (int *)calloc(size, sizeof(int)); + + //2. GREEDY ALGO + for(int i = 0; i < size; i++){ + while(a >= tab[i]){ + a -= tab[i]; + tab_counts[i]++; + } + } + return EXIT_SUCCESS; +} + +void erathosthene(int size, bool *tab){ + for(int i = 0; i < size; i++){ + tab[i] = true; + } + + tab[0] = false; + tab[1] = false; + + for(int i = 2; i <= sqrt(size) ;i++){ + if(tab[i]){ + for(int j = i*i;j < size;j+=i){ + tab[j] = false; + } + } + } +} + +int erathosthene_2_tab(int size, int *tab){ + + bool primes[size]; + erathosthene(size,primes); + int count = 0; + + for(int i = 0; i < size; i++){ + if(primes[i]){ + tab[count++] = i; + } + } + return count; +} \ No newline at end of file diff --git a/first_quarter/programs/first_e.h b/first_quarter/programs/first_e.h new file mode 100644 index 0000000000000000000000000000000000000000..85e5bde2568307835ba55322fd83b5f5bdb823b4 --- /dev/null +++ b/first_quarter/programs/first_e.h @@ -0,0 +1,31 @@ +#ifndef FIRST_E_H +#define FIRST_E_H + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <time.h> +#include <string.h> + +//1. Math functions +float weighted_average(int *a, int *coeff, int size); +float perimeter(float r); +bool is_prime(int num); +int gcd(int a, int b); +int lcm(int a, int b); + +//2. Array maniputation +int disaggregate_int(int a, int *tab, int *tab_counts); +void erathosthene(int size, bool *tab); +int erathosthene_2_tab(int size, int *tab); + +//3. Helper functions +void swap(int *a, int *b); +void select_sort(char *str); +void revert(char *str1, char *str2); + +//4. String manipulation +bool is_anagram(char *str1, char *str2); +bool is_palindrom(char *str); + +#endif \ No newline at end of file diff --git a/first_quarter/programs/helper_function.c b/first_quarter/programs/helper_function.c new file mode 100644 index 0000000000000000000000000000000000000000..26e3dd672a28e079a0c1de0e948b04e7b75dbbc0 --- /dev/null +++ b/first_quarter/programs/helper_function.c @@ -0,0 +1,27 @@ +void swap(int *a, int *b){ + *a = *a ^ *b; + *b = *b ^ *a; + *a = *a ^ *b; +} + +void select_sort(char *str){ + int n = strlen(str); + for(int i = 0; i < n -1; i++){ + for(int j = 0;j < n - i - 1;j++){ + if(str[j] > str[j + 1]){ + char temp = str[j]; + str[j] = str[j + 1]; + str[j + 1] = temp; + } + } + } +} + +void revert(char *str1, char *str2){ + int size = strlen(str1); + for(int i = 0;i < size;i++){ + str2[i] = str2[size - i - 1]; + } + str2[size] = '\0'; + return; +} \ No newline at end of file diff --git a/first_quarter/programs/main.c b/first_quarter/programs/main.c new file mode 100644 index 0000000000000000000000000000000000000000..a7db6cdf56dd3438115d4e8dea2c69215ce77cbb --- /dev/null +++ b/first_quarter/programs/main.c @@ -0,0 +1,5 @@ +#include "first_e.h" + +int main(int argc, char *argv[]){ + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/first_quarter/programs/math_functions.c b/first_quarter/programs/math_functions.c new file mode 100644 index 0000000000000000000000000000000000000000..bdde1c28a8381b6b9b00ff20dd423b722d84d2b6 --- /dev/null +++ b/first_quarter/programs/math_functions.c @@ -0,0 +1,45 @@ +#include "first_e.h" + +#define Pi 3.14159265359 + +float weighted_average(int *a, int *coeff, int size){ + int total_weight = 0; + int total_coeff = 0; + + for(int i = 0; i < size;i++){ + total_weight += a[i] * coeff[i]; + total_coeff += coeff[i]; + } + return (float)total_weight/total_coeff; +} + +float perimeter(float r){ + float d = 2 * r; + float prmt = Pi * d; + return prmt; +} + +bool is_prime(int num){ + if(num < 2){ + return false; + } + for(int i = 2; i * i <= num; i++){ + if(num % i == 0){ + return false; + } + } + return true; +} + +int gcd(int a, int b){ + while(b != 0){ + int temp = b; + b = a % b; + a = temp; + } + return a; +} + +int lcm(int a, int b){ + return (a * b)/gcd(a, b); +} \ No newline at end of file diff --git a/first_quarter/programs/str_manipulation.c b/first_quarter/programs/str_manipulation.c new file mode 100644 index 0000000000000000000000000000000000000000..9301efe5e3acd97b3cae63eb00228247e8ed6ebc --- /dev/null +++ b/first_quarter/programs/str_manipulation.c @@ -0,0 +1,26 @@ +#include "first_e.h" + + + +bool is_anagram(char *str1, char *str2){ + if(strlen(str1) != strlen(str2)){ + return false; + } + + select_sort(str1); + select_sort(str2); + + if(memcmp(str1,str2, strlen(str1)) == 0){ + return true; + }else { + return false; + } +} + + +bool is_palindrom(char *str){ + int SIZE = strlen(str); + char temp[SIZE + 1]; + revert(str, temp); + return strcmp(str,temp); +} \ No newline at end of file diff --git a/first_quarter/subjects/data_strucs/main b/first_quarter/subjects/data_strucs/main new file mode 100755 index 0000000000000000000000000000000000000000..678a74f7fccd7efc09f621ec51ab56c9b91547b1 Binary files /dev/null and b/first_quarter/subjects/data_strucs/main differ diff --git a/first_quarter/subjects/data_strucs/static_arrays.c b/first_quarter/subjects/data_strucs/static_arrays.c new file mode 100644 index 0000000000000000000000000000000000000000..db45b7dcb6e87f642e5fbfa697fe8c8b6a0218cf --- /dev/null +++ b/first_quarter/subjects/data_strucs/static_arrays.c @@ -0,0 +1,98 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <string.h> +#include <stdbool.h> + +#define size 10 //i n static arrays size must be knonw + +void init_tab(int tab[size]){ + for(int i = 0;i < size;i++){ + tab[i] = rand() % size;// % size init from 0 to size - 1; + } +} + +void print_tab(int tab[size]){ + for(int i = 0;i < size;i++){ + printf("%d ", tab[i]); + } + printf("\n"); +} + +void s_str_in_str(char *s_str,char *str){ + char *pos = strstr(str,s_str); + if (pos != NULL) { + printf("Substring (%s) found at position: %ld\n",s_str, pos - str); + } else { + printf("Substring not found.\n"); + } +} + +void split_str(char *str){ + char *token = strtok(str, " "); + while(token != NULL){ + printf("%s\n", token); + token = strtok(NULL, " "); + } +} + +int main(){ + //1. Array of ints + //1.1 Declaration + printf("1. Array of INTS\n"); + int tab[size]; + + //1.2 Initialization + //1.2.1 Declaration and Initialization at he same time + printf("1.1. Declaration and Init together\n"); + int tab2[] = {1,2,3,4,5,6,7,8,9,10}; + print_tab(tab2); + printf("\n"); + //1.2.2 Initialization with random values using function + srand(time(NULL));//plants the seed + printf("1.2. Init with rand vals using function\n"); + init_tab(tab); + print_tab(tab); + printf("\n"); + printf("\n"); + + //2. Array of chars a.i String lib + printf("2. Array of CHARS\n"); + //2.1 Declaration and Initialization using a pointer to a string literal(stored in read-only memory) + printf("2.1 Declaration and Initialization using a pointer\n"); + char *str = "Hello, world"; + printf("String literal POINTED is: %s\n", str); + //2.2 Declaration and Initialization using the content of the string literal + printf("2.2 Declaration and Initialization using the content\n"); + char str_char_array[] = "Array Char"; + printf("String literal CONTENT is: %s\n", str); + printf("\n"); + //2.3 String operations + printf("2.3. String operationsa\n"); + printf("\n"); + printf("2.3.1. String length\n"); + int len = strlen(str); + printf("%s contains %d chars\n", str, len); + printf("2.3.2. String copy\n"); + char *str2 = (char *)malloc((len + 1) * sizeof(char));//requires memory allocation + strcpy(str2,str); + printf("Second string(%s) in now = to first string(%s)\n", str2,str); + printf("2.3.3. String comparison\n"); + bool are_equal = strcmp(str2,str); + printf("Are second and first string equal ? if strcmp is %d then it's true", are_equal); + free(str2);//memory allocated must be freed + printf("\n"); + //2.4 String operations with functions + printf("2.4. String operationsa using functions\n"); + //2.4.1. Find sub_string in string + printf("2.4.1. Find substring in string\n"); + char *s_str = "world"; + s_str_in_str(s_str,str); + printf("\n"); + //2.4.2. Split string + printf("2.4.2. Split string\n"); + split_str(str_char_array); + + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/first_quarter/subjects/generals/data_types.c b/first_quarter/subjects/generals/data_types.c new file mode 100644 index 0000000000000000000000000000000000000000..1c081aae91a96cf305e86b8dda98e7038335a419 --- /dev/null +++ b/first_quarter/subjects/generals/data_types.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +#define typename(x) _Generic((x), \ + _Bool: "_Bool", \ + unsigned char: "unsigned char", \ + char: "char", \ + signed char: "signed char", \ + short int: "short int", \ + unsigned short int: "unsigned short int",\ + int: "int", \ + unsigned int: "unsigned int", \ + long int: "long int", \ + unsigned long int: "unsigned long int", \ + float: "float", \ + double: "double", \ + long double: "long double", \ + default: "other") + +/*This macro allows to manipulate better data types using the keyword: _Generic + +Sytax: _Generic(<expression>, <type>, <result>, <type>, <result> ... default:<result>) + +<expression>: variable data type we are checking +<type>: type we compare agains +<result>: what we do if type match +default: what we do if types do not match + + n + q: Evaluates to the sum of n and q. + + typename(n + q): Determines the type of the result of n + q and returns it as a string. + + %s: Prints the type name string. + + %ld: Intended to print a long integer, but should be corrected to %f for floating-point numbers. +*/ + + +int main(void) { + int n = 10; + int p = 4; + long q = 2; + float x = 1.75; + printf("n + q => %s : %ld\n", typename(n + q), n + q); + printf("n + x => %s : %f\n", typename(n + x), n + x); + printf("n %% p + q => %s : %ld\n", typename(n % p + q), n % p + q); + printf("n < p => %s : %d\n", typename(n < p), n < p); + printf("n >= p => %s : %d\n", typename(n >= p), n >= p); + printf("n > q => %s : %d\n", typename(n > q), n > q); + printf("q + 3 * (n > p) => %s : %ld\n", typename(q + 3 * (n > p)), q + 3 * (n > p)); + printf("q && n => %s : %d\n", typename(q && n), q && n); + printf("(q - 2) && (n - 10) => %s : %d\n", typename((q - 2) && (n - 10)), (q - 2) && (n - 10)); + printf("x * (q==2) => %s : %f\n", typename(x*(q==2)), x*(q==2)); + printf("x * (q=5) => %s : %f\n", typename(x*(q=5)), x*(q=5)); + + return EXIT_SUCCESS; +} diff --git a/first_quarter/subjects/generals/main b/first_quarter/subjects/generals/main new file mode 100755 index 0000000000000000000000000000000000000000..d51f09193d4964f24ef853b5212f692d7268b618 Binary files /dev/null and b/first_quarter/subjects/generals/main differ diff --git a/first_quarter/subjects/generals/main_structs b/first_quarter/subjects/generals/main_structs new file mode 100755 index 0000000000000000000000000000000000000000..ae65e602eba28dfc88ff9763c1e832a180658fb4 Binary files /dev/null and b/first_quarter/subjects/generals/main_structs differ diff --git a/first_quarter/subjects/generals/struct.c b/first_quarter/subjects/generals/struct.c new file mode 100644 index 0000000000000000000000000000000000000000..4fa8f1be71143947bcd3ed1f3bd79c4f485918f0 --- /dev/null +++ b/first_quarter/subjects/generals/struct.c @@ -0,0 +1,193 @@ +#include <stdio.h> +#include <stdlib.h> + +//1. Conditional statements + +int if_alone(int a, int b){ + int res = 0; + if(a != b){ + res = a + b; + } + printf("if condition is not meet then the following statement executer\n"); + return res; +} + +int if_else(int a, int b){ + int res = 0; + if (a == b){ + res = a + b; + } else if(a <= b){ + res = a - b + 1; + } else{ + res = a * b; + } + printf("following statement always execute\n"); + return res; +} + +void switch_stmt(int a, int b, int cases){ + int res = 0; + + switch (cases) + { + case 1: + res = a + b; + printf("case 1: %d\n",res); + break; + case 2: + res = a - b; + printf("case 2: %d\n",res); + default: + printf("default: %d\n",res); + break; + } +} + +int tern(int a, int b){ + return a < b ? a + b : a - b; +} + +int main(){ + // 1. Conditional statement + + //1.1 if alone + //1.1.1 when true + printf("1.1 IF ALONE\n"); + int a = 2; + int b = 4; + int res = if_alone(a,b); + printf("code execute if condition is true: %d\n", res); + //1.1.2 when false + int c = 5; + int d = 5; + int res2 = if_alone(c,d); + printf("code execute if condition is false: %d\n", res2); + printf("\n"); + + //1.2 if-else + printf("1.2 IF ELSE\n"); + int w = 10; + int x = 10; + int res3 = if_else(w,x); + printf("code execute if condition is true: %d\n", res3); + int y = 2; + int z = 4; + int res4 = if_else(y,z); + printf("code execute else if condition is true: %d\n", res4); + int aa = 4; + int bb = 2; + int res5 = if_else(aa,bb); + printf("code execute not if nor else if condition is true: %d\n", res5); + printf("\n"); + + //1.3 Switch + printf("1.3 SWITCH \n"); + int aaa = 2; + int bbb = 3; + int cases = 1; + switch_stmt(aaa,bbb,cases); + printf("\n"); + + //1.4 Ternary + printf("1.3 TERNARY \n"); + int xx = 3; + int yy = 4; + int res6 = tern(xx,yy); + printf("statement if true: %d\n", res6); + printf("\n"); + + + //2. Loops + //2.1 Number of iterations known, FOR loop + printf("2.1 FOR loop \n"); + int tab[] = {1,2,3,4,5}; + int size = 5; + for(int i = 0;i < size;i++){ + tab[i] = tab[i + 1]; + } + printf("first element in array is not longer 1 but 1 + 1: %d\n", *tab); + printf("\n"); + + //2.2 based on condition that might change during iteration, WHILE loop + printf("2.2 WHILE loop \n"); + int desired_first_val = 4; + int count = 0; + while(tab[0] != desired_first_val){ + *tab = *tab + 1; + count++; + } + printf("iterate until the value in first index is 4: there were %d iteration to get to %d\n",count, *tab); + printf("\n"); + + //2.3 Ensures at least one execution + printf("2.2 DO-WHILE loop \n"); + int count_2 = 0; + do{ + *tab -= 1; + count_2++; + }while (tab[0] >= tab[1]); + printf("iterate until the value in first index is bigger or qeual than value of second index: there were %d iteration\n",count_2); + printf("first index is %d, sencond index is %d",tab[0],tab[1]); + printf("\n"); + + + + + + + + + + +/* + printf("IF ELSE CAN BE: \n"); + printf("Simple\n"); + printf(" start "); + printf(" | "); + printf(" if-consition "); + printf(" | "); + printf(" ------------------- "); + printf(" | | "); + printf("true flase"); + printf("if-body else-body"); + printf(" | | "); + printf(" ------------------- "); + printf(" | "); + printf(" statement berlow "); + printf(" | "); + printf(" end "); + printf("\n"); + printf("Nested\n"); + printf(" start "); + printf(" | "); + printf(" if-consition "); + printf(" | "); + printf(" -------------------------------------- "); + printf(" | | "); + printf(" true flase "); + printf(" condition 1 else-body "); + printf(" | | "); + printf(" ------------------- --------------------- "); + printf(" | | | | "); + printf("true false true false "); + printf("st1 st2 str3 st4 "); + printf(" | | | | "); + printf(" ------------------------------------------------------- "); + printf(" | "); + printf(" end "); + printf("\n"); + printf("Laddered\n"); + printf("start "); + printf(" | "); + printf("condition 1----------------statement 1--------------"); + printf(" | |"); + printf(" no |"); + printf(" | |"); + printf("condition 2----------------statement 2--------------"); + printf(" | |"); + printf(" no-----------------------else body----------------"); + printf(" |"); + printf(" end"); + printf("\n"); +*/ +} \ No newline at end of file diff --git a/first_quarter/subjects/pointers/main b/first_quarter/subjects/pointers/main new file mode 100755 index 0000000000000000000000000000000000000000..c9286f758b5c0752f558283d3e53be2cf2a9adcb Binary files /dev/null and b/first_quarter/subjects/pointers/main differ diff --git a/first_quarter/subjects/pointers/pointers.c b/first_quarter/subjects/pointers/pointers.c new file mode 100644 index 0000000000000000000000000000000000000000..3c9c2d8688eea1d6353c56a85adba36b01d4f21e --- /dev/null +++ b/first_quarter/subjects/pointers/pointers.c @@ -0,0 +1,108 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <time.h> + +void tab_init(int *tab, int size){ + for(int i = 0;i < size;i++){ + tab[i] = rand() % size; + } +} + +void print_tab(int *tab, int size){ + printf("index | Value \n"); + for(int i = 0;i < size;i++){ + + printf("%d | %d\n", i, tab[i]); + } +} + +void linear_search_val(int *tab, int size, int i_search){ + for(int i = 0;i < size;i++){ + if(i == i_search){ + printf("in index %d value is %d\n",i, tab[i]); + } + } +} + + + + + +int main(int argc, char *argv[]){ + //Arguments check + if(argc != 2){ + fprintf(stderr,"Usage:<%s><size>\n", argv[0]); + return EXIT_FAILURE; + } + int size = atoi(argv[1]); + if(size <= 0){ + fprintf(stderr,"Size most be a positive integer\n"); + return EXIT_FAILURE; + } + //1. Ponter to an integer(dynamically allocated arrey of ints) + printf("1. Pointer to INT\n"); + //1.1 Declaration and Memory allocation + printf("1.1 Declaration and Memory allocation\n"); + + int *tab = (int *)malloc(size * sizeof(int)); + if(tab == NULL){ + fprintf(stderr,"Allocation Error\n"); + return EXIT_FAILURE; + } + printf("the array is of size(in bytes) %ld\n", size * sizeof(int)); + //1.2 Initialization using fonctions + printf("1.2 Init with rand vals using fonciton\n"); + srand(time(NULL)); + tab_init(tab,size); + print_tab(tab,size); + //1.3 Search value based on index (linear search) + printf("1.3 Access index unsing linear search \n"); + int i_mid_tab = (size/2); + linear_search_val(tab,size,i_mid_tab); + printf("\n"); + + //2. Malloc vs Calloc + printf("2. Calloc vs Malloc\n"); + int *tab_m = (int *)malloc(size * sizeof(int)); + if(tab == NULL){ + fprintf(stderr,"Allocation Error\n"); + free(tab); + return EXIT_FAILURE; + } + printf("2.1 Malloc init with garbage vals therefore need initialization\n"); + tab_init(tab_m,size); + print_tab(tab_m,size); + printf("2.2 Malloc init with 0\n"); + int *tab_c = (int *)calloc(size, sizeof(int)); + if(tab == NULL){ + fprintf(stderr,"Allocation Error\n"); + free(tab); + free(tab_m); + return EXIT_FAILURE; + } + print_tab(tab_c,size); + printf("\n"); + + //3. Realloc + printf("3. Realloc\n"); + int size_2 = 6; + int *new_tab = realloc(tab, size_2 * sizeof(int)); + if(tab == NULL){ + fprintf(stderr,"Reallocation Error\n"); + free(tab); + free(tab_c); + free(tab_m); + + return EXIT_FAILURE; + } + tab = new_tab; + tab_init(tab,size_2); + print_tab(tab,size_2); + + free(tab_c); + free(tab_m); + free(tab); + return EXIT_SUCCESS; +} \ No newline at end of file