diff --git a/Cviko2_2/.gitignore b/Cviko2_2/.gitignore new file mode 100644 index 0000000..f9bf443 --- /dev/null +++ b/Cviko2_2/.gitignore @@ -0,0 +1,93 @@ +# Created by https://www.toptal.com/developers/gitignore/api/c,visualstudiocode,linux +# Edit at https://www.toptal.com/developers/gitignore?templates=c,visualstudiocode,linux + +### C ### +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +### Linux ### +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +### VisualStudioCode ### +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +### VisualStudioCode Patch ### +# Ignore all local history of files +.history +.ionide + +# End of https://www.toptal.com/developers/gitignore/api/c,visualstudiocode,linux +main \ No newline at end of file diff --git a/Cviko2_2/Makefile b/Cviko2_2/Makefile new file mode 100644 index 0000000..7668f40 --- /dev/null +++ b/Cviko2_2/Makefile @@ -0,0 +1,13 @@ +all: main + +CC = gcc +override CFLAGS += -Wall -Wextra -Wno-unused-variable -Wno-unused-parameter -Wno-unused-result -Wno-unknown-pragmas -pedantic -lm + +main: main.c + $(CC) $(CFLAGS) -O1 ./*.c -o "$@" + +main-debug: main.c + $(CC) $(CFLAGS) -O1 -g ./*.c -o "$@" + +clean: + rm -f main main-* diff --git a/Cviko2_2/README.md b/Cviko2_2/README.md new file mode 100644 index 0000000..23c9474 --- /dev/null +++ b/Cviko2_2/README.md @@ -0,0 +1,51 @@ +# Replit Template + +**Branch switching** +``` +git switch "branch_name" +git submodule update --init +``` + +**Submodule updates** +``` +git submodule update --init --remote +git checkout main -- README.md student-config.sh .tests/.gitignore .tests/config.sh .tests/README.md -p +``` + +## Creating new branches +``` +BRANCH="branch_name" +git branch -c main "$BRANCH" +git submodule set-branch --branch "$BRANCH" solution +``` + +## Propagating changes from master +``` +git fetch +for BRANCH in $(git branch -r | grep -E -o "\d{2}-\d{2}-.*"); +do + echo "\nRebase $BRANCH" + read + git branch -D $BRANCH + git checkout $BRANCH || break + git reset --hard origin/"$BRANCH" || break + git rebase master || break + git push --force || break +done +``` + +## Propagating changes of scripts +``` +git fetch +for BRANCH in $(git branch -r | grep -E -o "\d{2}-\d{2}-.*"); +do + echo "\nSync test scripts on $BRANCH" + read + git branch -D $BRANCH + git checkout $BRANCH || break + git submodule update --remote .tests/scripts || break + git add .tests/scripts || break + git commit -m "feat: sync latest test scripts" || break + git push || break +done +``` diff --git a/Cviko2_2/main.c b/Cviko2_2/main.c new file mode 100644 index 0000000..c45fd37 --- /dev/null +++ b/Cviko2_2/main.c @@ -0,0 +1,103 @@ +#include "types.h" +#include + +#define ARRAY_LENGTH 5 +#define MERGED_ARRAY_LENGTH 2 * ARRAY_LENGTH + +int* ask_for_numbers(){ + static int p[ARRAY_LENGTH]; + for (int i = 0; i < ARRAY_LENGTH; ++i) { + printf("Enter number (%d/%d):\n",i+1,ARRAY_LENGTH); + scanf("%d",&p[i]); + } + return p; +} + +void read_print_array_reverse() { + //TODO 1. Read an array of numbers of length ARRAY_LENGTH from stdin. + int* p = ask_for_numbers(); + + //TODO 2. Print read array in reversed order to stdout. + for (int i = ARRAY_LENGTH - 1; i >= 0; --i) { + printf("%d ",p[i]); + } + return; +} + +void read_print_array_max() { + int* p = ask_for_numbers(); + + //TODO 2. Find maximum value and print it to stdout. + int max = p[0]; + for (int i = 1; i < ARRAY_LENGTH; ++i) { + if(p[i] > max){ + max = p[i]; + } + } + + printf("The max of the numbers entered is %d",max); + return; +} + +void read_print_array_avg() { + //TODO 1. Read an array of numbers of length ARRAY_LENGTH from stdin. + int* p = ask_for_numbers(); + + //TODO 2. Calculate average value and print it to stdout. + int all = 0; + for (int i = 0; i < ARRAY_LENGTH; ++i) { + all += p[i]; + } + + printf("The average value of the array is %.2f",(double)all/ARRAY_LENGTH); + return; +} + + +void read_print_array_merge() { + //TODO 1. Read 2 arrays of numbers, each of length ARRAY_LENGTH from stdin. + printf("ARRAY 1\n"); + int* p = ask_for_numbers(); + + printf("ARRAY 2\n"); + int* q = ask_for_numbers(); + + //TODO 2. To 3rd array of length MERGED_ARRAY_LENGTH copy first and + //second arrays respectively and resulting third array print to stdout. + int m[MERGED_ARRAY_LENGTH]; + for (int i = 0; i < MERGED_ARRAY_LENGTH; ++i) { + if(i < ARRAY_LENGTH){ + m[i] = p[i]; + } + else{ + m[i] = q[i-ARRAY_LENGTH]; + } + } + + for (int i = 0; i < MERGED_ARRAY_LENGTH; ++i) { + printf("%d ",m[i]); + } + printf("\n"); + return; +} + + +#ifndef TEST_BUILD + +int main(int argc, char *argv[]) { + printf("\n--read_print_array_reverse--\n"); + read_print_array_reverse(); + + printf("\n--read_print_array_max--\n"); + read_print_array_max(); + + printf("\n--read_print_array_avg--\n"); + read_print_array_avg(); + + printf("\n--read_print_array_merge--\n"); + read_print_array_merge(); + + return 0; +} + +#endif diff --git a/Cviko2_2/types.h b/Cviko2_2/types.h new file mode 100644 index 0000000..5b78adf --- /dev/null +++ b/Cviko2_2/types.h @@ -0,0 +1,11 @@ +// OBSAH TOHOTO SOUBORU NEUPRAVUJTE + +#ifndef TYPES_H +#define TYPES_H + + void read_print_array_reverse(); + void read_print_array_max(); + void read_print_array_avg(); + void read_print_array_merge(); + +#endif diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68a49da --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to