From 5b11809c28181cf296fd9adf93bce9cdbefae5f8 Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@pm.me> Date: Mon, 7 Oct 2024 14:46:02 +0200 Subject: [PATCH] added codes --- slides/.clang-format | 183 +++++++++++++++++++++++++++++++ slides/exemples/array.c | 26 +++++ slides/exemples/pgcd.c | 36 ++++++ slides/exemples/ppcm.c | 22 ++++ slides/exemples/selection_sort.c | 68 ++++++++++++ slides/exemples/while.c | 22 ++++ 6 files changed, 357 insertions(+) create mode 100644 slides/.clang-format create mode 100644 slides/exemples/array.c create mode 100644 slides/exemples/pgcd.c create mode 100644 slides/exemples/ppcm.c create mode 100644 slides/exemples/selection_sort.c create mode 100644 slides/exemples/while.c diff --git a/slides/.clang-format b/slides/.clang-format new file mode 100644 index 0000000..e955634 --- /dev/null +++ b/slides/.clang-format @@ -0,0 +1,183 @@ +--- +Language: Cpp +# BasedOnStyle: Google +AccessModifierOffset: -4 +AlignAfterOpenBracket: AlwaysBreak +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignConsecutiveMacros: true +AlignEscapedNewlines: Left +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Empty +AllowShortLambdasOnASingleLine: Empty +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: true +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: MultiLine + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: NonAssignment +BreakBeforeBraces: Custom +BreakBeforeInheritanceComma: false +BreakInheritanceList: AfterColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: AfterColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 100 +CommentPragmas: '^ IWYU pragma:' +CompactNamespaces: false +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: false +DerivePointerAlignment: false +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^<ext/.*\.h>' + Priority: 2 + SortPriority: 0 + - Regex: '^<.*\.h>' + Priority: 1 + SortPriority: 0 + - Regex: '^<.*' + Priority: 2 + SortPriority: 0 + - Regex: '.*' + Priority: 3 + SortPriority: 0 +IncludeIsMainRegex: '([-_](test|unittest))?$' +IncludeIsMainSourceRegex: '' +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: false +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentWidth: 4 +IndentWrappedFunctionNames: true +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: false +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Never +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 1 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 200 +PointerAlignment: Right +RawStringFormats: + - Language: Cpp + Delimiters: + - cc + - CC + - cpp + - Cpp + - CPP + - 'c++' + - 'C++' + CanonicalDelimiter: '' + BasedOnStyle: google + - Language: TextProto + Delimiters: + - pb + - PB + - proto + - PROTO + EnclosingFunctions: + - EqualsProto + - EquivToProto + - PARSE_PARTIAL_TEXT_PROTO + - PARSE_TEST_PROTO + - PARSE_TEXT_PROTO + - ParseTextOrDie + - ParseTextProtoOrDie + - ParseTestProto + - ParsePartialTestProto + CanonicalDelimiter: '' + BasedOnStyle: google +ReflowComments: true +SortIncludes: false +SortUsingDeclarations: false +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: true +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: true +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: false +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +Standard: Auto +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE +... + diff --git a/slides/exemples/array.c b/slides/exemples/array.c new file mode 100644 index 0000000..cc7113c --- /dev/null +++ b/slides/exemples/array.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <strings.h> + +#define SIZE 4 + +int main() { + int tab[SIZE] = {1, -1, 10, 4}; + int new_tab[SIZE]; + for (int index = 0; index < SIZE - 1; ++index) { + + int min_index = index; + int min = tab[min_index]; + + for (int i = min_index + 1; i < SIZE; ++i) { + if (tab[i] < min) { + min = tab[i]; + min_index = i; + } + } + new_tab[index] = min; + } + for (int i = 0; i < SIZE; ++i) { + printf("%d ", new_tab[i]); + } + printf("\n"); +} diff --git a/slides/exemples/pgcd.c b/slides/exemples/pgcd.c new file mode 100644 index 0000000..9be8349 --- /dev/null +++ b/slides/exemples/pgcd.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <strings.h> + +int minimum(int a, int b) { + if (a < b) { + return a; + } else { + return b; + } +} + +int old_pgcd(int a, int b) { + int min = minimum(a, b); + for (int i = min; i >= 2; --i) { + if ((a % i == 0) && (b % i == 0)) { + return i; + } + } + return 1; +} + +int pgcd(int a, int b) { + while (b != 0) { + int rest = a % b; + a = b; + b = rest; + } + return a; +} + +int main() { + printf("pgcd(%d, %d) = %d\n", 3, 4, pgcd(3, 4)); + printf("pgcd(%d, %d) = %d\n", 5, 15, pgcd(5, 15)); + printf("pgcd(%d, %d) = %d\n", 36, 90, pgcd(36, 90)); + printf("pgcd(%d, %d) = %d\n", 4, 6, pgcd(4, 6)); +} diff --git a/slides/exemples/ppcm.c b/slides/exemples/ppcm.c new file mode 100644 index 0000000..84fc405 --- /dev/null +++ b/slides/exemples/ppcm.c @@ -0,0 +1,22 @@ +#include <stdio.h> + +int ppcm(int m, int n) { + int tmp_m = m; + int tmp_n = n; + while (m != n) { + if (m < n) { + m += tmp_m; + } else { + n += tmp_n; + } + } + return m; +} + +int main() { + int m = 36; + int n = 90; + + printf("the ppcm is %d\n", ppcm(m, n)); + return 0; +} diff --git a/slides/exemples/selection_sort.c b/slides/exemples/selection_sort.c new file mode 100644 index 0000000..bb5650b --- /dev/null +++ b/slides/exemples/selection_sort.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdbool.h> +#include <stdlib.h> +#include <time.h> + +#define SIZE 10 + +void tab_initialize(int size, double tab[size]) { + srand(time(NULL)); + for (int i = 0; i < size; ++i) { + double rdm = (double)rand() / (double)RAND_MAX; + tab[i] = rdm; + } +} + +void tab_show(int size, double tab[size]) { + for (int i = 0; i < size; ++i) { + printf("%lf ", tab[i]); + } + printf("\n"); +} + +int tab_find_min_index(int i, int size, double tab[size]) { + int i_min = i; + for (int j = i + 1; j < size; ++j) { + if (tab[j] < tab[i_min]) { + i_min = j; + } + } + return i_min; +} + +void swap(int i, int j, double tab[]) { + double tmp = tab[i]; + tab[i] = tab[j]; + tab[j] = tmp; +} + +void tab_selection_sort(int size, double tab[size]) { + for (int i = 0; i < size; ++i) { + int i_min = tab_find_min_index(i, size, tab); + + // échange tab[i] et tab[j] + if (i_min != i) { + swap(i, i_min, tab); + } + } +} + +bool tab_is_sorted(int size, double tab[size]) { + for (int i = 1; i < size; ++i) { + if (tab[i - 1] > tab[i]) { + return false; + } + } + return true; +} + +int main() { + // allocate tab + double tab[SIZE]; + tab_initialize(SIZE, tab); + tab_show(SIZE, tab); + tab_selection_sort(SIZE, tab); + tab_show(SIZE, tab); + bool is_sorted = tab_is_sorted(SIZE, tab); + printf("Is the tab sorted? %d\n", is_sorted); +} diff --git a/slides/exemples/while.c b/slides/exemples/while.c new file mode 100644 index 0000000..0f17683 --- /dev/null +++ b/slides/exemples/while.c @@ -0,0 +1,22 @@ +#include <math.h> +#include <stdbool.h> +#include <stdio.h> + +bool is_prime(int number) { + for (int i = 2; i <= sqrt(number); i++) { + if (0 == number % i) { + return false; + } + } + return true; +} + +int main() { + int max_number = 100000; + for (int i = 2; i <= max_number; ++i) { // max_number * sqrt(max_number) + if (is_prime(i)) { + printf("%d\n", i); + } + } + return 0; +} -- GitLab