uni

University stuff
git clone git://git.christosmarg.xyz/uni-assignments.git
Log | Files | Refs | README | LICENSE

commit 4452241c36f65802d39e6a881870b15eb765ea5b
Author: Christos Margiolis <christos@margiolis.net>
Date:   Wed, 18 Dec 2019 00:26:58 +0200

initial version

Diffstat:
AREADME.md | 3+++
Aassignment-1.1-basic-elements/C1a-CORRECTED.c | 24++++++++++++++++++++++++
Aassignment-1.1-basic-elements/Cube-Sphere.c | 25+++++++++++++++++++++++++
Aassignment-1.1-basic-elements/IO-Exercise.c | 40++++++++++++++++++++++++++++++++++++++++
Aassignment-1.1-basic-elements/examples/C1a.c | 24++++++++++++++++++++++++
Aassignment-1.2-conditional-statements/examples/C2a.c | 26++++++++++++++++++++++++++
Aassignment-1.2-conditional-statements/examples/C2b.c | 40++++++++++++++++++++++++++++++++++++++++
Aassignment-1.2-conditional-statements/examples/C2c.c | 17+++++++++++++++++
Aassignment-1.2-conditional-statements/int-comparison.c | 24++++++++++++++++++++++++
Aassignment-1.2-conditional-statements/quadratic-equation.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.3-loops/int-calcs.c | 40++++++++++++++++++++++++++++++++++++++++
Aassignment-1.3-loops/shapes.c | 72++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.3-loops/sine-taylor.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.4-functions/README.md | 46++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.4-functions/autocompile.bat | 27+++++++++++++++++++++++++++
Aassignment-1.4-functions/autocompile.sh | 18++++++++++++++++++
Aassignment-1.4-functions/hanoi-tower/hanoi-tower.c | 34++++++++++++++++++++++++++++++++++
Aassignment-1.4-functions/menu/exec/a.out | 0
Aassignment-1.4-functions/menu/obj/main.o | 0
Aassignment-1.4-functions/menu/obj/menu-libs.o | 0
Aassignment-1.4-functions/menu/src/main.c | 18++++++++++++++++++
Aassignment-1.4-functions/menu/src/menu-full.c | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.4-functions/menu/src/menu-libs.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.4-functions/menu/src/menu.h | 12++++++++++++
Aassignment-1.4-functions/sine-cos-taylor/exec/a.out | 0
Aassignment-1.4-functions/sine-cos-taylor/obj/main.o | 0
Aassignment-1.4-functions/sine-cos-taylor/obj/sine-cos-taylor-libs.o | 0
Aassignment-1.4-functions/sine-cos-taylor/src/main.c | 26++++++++++++++++++++++++++
Aassignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-full.c | 125+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-libs.c | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.h | 13+++++++++++++
Aassignment-1.5-arrays-pointers-files/combinations/src/combinations.c | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/examples/1DDynamic.c | 223+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/examples/Lexico.c | 149+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/examples/mystring.c | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aassignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c | 9+++++++++
36 files changed, 1672 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,2 @@ +# uni-assignments +**Format:** ```assignment-x.y``` (```x``` = semester, ```y``` = number of assignment, ```z``` = subject).+ \ No newline at end of file diff --git a/assignment-1.1-basic-elements/C1a-CORRECTED.c b/assignment-1.1-basic-elements/C1a-CORRECTED.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + int A, B; + int C, D, E, F; + system ("chcp 1253"); + printf ("Βασικές αριθμητικές πράξεις με Ακεραίους\n"); + printf ("========================================\n\n"); + printf ("Εισάγετε τον πρώτο αριθμό : "); + scanf ("%d",&A); + printf ("Εισαγετε το δεύτερο αριθμό : "); + scanf ("%d", &B); + C = A + B; + D = A - B; + E = A * B; + F = A / B; + printf ("Άθροισμα : %d\n", C); + printf ("Διαφορά : %d\n", D); + printf ("Γινόμενο : %d\n", E); + printf ("Πηλίκο : %d\n", F); + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.1-basic-elements/Cube-Sphere.c b/assignment-1.1-basic-elements/Cube-Sphere.c @@ -0,0 +1,24 @@ +#include <stdio.h> +#include <math.h> + +int main(int argc, char **argv) +{ + double length, rad, area_cube, vol_cube, area_sphere, vol_sphere; + const double PI = 3.14; + + printf("Μήκος (σε μέτρα): "); + scanf("%lf", &length); + + area_cube = 6.0*pow(length, 2); + vol_cube = pow(length, 3); + rad = length; + area_sphere = 4.0*PI*pow(rad, 2); + vol_sphere = (4.0/3.0)*PI*pow(rad, 3); + + printf("Εμβαδόν κύβου: %.2lf\n", area_cube); + printf("Όγκος Κύβου: %.2lf\n\n", vol_cube); + printf("Εμβαδόν σφαίρας: %.2lf\n", area_sphere); + printf("Όγκος σφαίρας: %.2lf\n", vol_sphere); + + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.1-basic-elements/IO-Exercise.c b/assignment-1.1-basic-elements/IO-Exercise.c @@ -0,0 +1,39 @@ +#include <stdio.h> +#include <math.h> + +int main(int argc, char **argv) +{ + int int_1, int_2, sum, diff, prod, div, sqr; + double f_div, sqr_root; + + printf("Ακέραιος 1: "); + scanf("%d", &int_1); + printf("Ακέραιος 2: "); + scanf("%d", &int_2); + + sum = int_1 + int_2; + diff = int_1 - int_2; + prod = int_1 * int_2; + + sqr = pow(int_1, 2); + sqr_root = sqrt(int_2); + + printf("Άθροισμα: %d\n", sum); + printf("Διαφορά: %d\n", diff); + printf("Γινόμενο: %d\n", prod); + printf("Τετράγωνο: %d\n", sqr); + printf("Τετραγωνική ρίζα: %.2lf\n", sqr_root); + if (int_2 != 0) + { + div = int_1 / int_2; + f_div = (double)int_1 / int_2; + printf("Πηλίκο: %d\n", div); + printf("Πραγματικό πηλίκο: %.2lf\n", f_div); + } + else + { + printf("Δεν γίνεται διαίρεση με το 0.\n"); + } + + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.1-basic-elements/examples/C1a.c b/assignment-1.1-basic-elements/examples/C1a.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +int main(int argc, int **argv) +{ + int A, B; + int C, D, E, F; + system ("chcp 1253"); + printf ("Βασικές αριθμητικές πράξεις με Ακεραίους\n"); + printf ("========================================\n\n"); + printf ("Εισάγετε τον πρώτο αριθμό : "); + scanf ("%d",&A); + printf ("Εισαγετε το δεύτερο αριθμό : ); + scanf ("%d", &B); + C = A + B; + D = A - B; + E = A * B; + F = A / B; + printf ("Άθροισμα : %d\n", c); + printf ("Διαφορά : %d\n", D); + printf ("Γινόμενο : %d\n", Ε); + prantf ("Πηλίκο : %d\n", F); + return 0;; +}+ \ No newline at end of file diff --git a/assignment-1.2-conditional-statements/examples/C2a.c b/assignment-1.2-conditional-statements/examples/C2a.c @@ -0,0 +1,26 @@ +#include <stdio.h> + +int main (int NoP, char **Params) +{ + int a, b, c; + int Max; + printf ("ΕΥΡΕΣΗ ΜΕΓΙΣΤΟΥ ΑΡΙΘΜΟΥ\n\n"); + printf ("Εισάγετε τον πρώτο αριθμό : "); + scanf ("%d", &a); + printf ("Εισάγετε τον δεύτερο αριθμό : "); + scanf ("%d", &b); + printf ("Εισάγετε τον τρίτο αριθμό : "); + scanf ("%d", &c); + Max = a; + if (b > Max) + Max = b; + Max = (c > Max)? c: Max; + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη:\n", Max); + if (Max == a) + printf(" 1ος\n"); + if (Max == b) + printf(" 2ος\n"); + if (Max == c) + printf(" 3ος\n"); + return 124; +} diff --git a/assignment-1.2-conditional-statements/examples/C2b.c b/assignment-1.2-conditional-statements/examples/C2b.c @@ -0,0 +1,40 @@ +#include <stdio.h> + +int main (int NoP, char **Params) +{ + int a, b, c; + int Max; + printf ("ΕΥΡΕΣΗ ΜΕΓΙΣΤΟΥ ΑΡΙΘΜΟΥ\n\n"); + printf ("Εισάγετε τον πρώτο αριθμό : "); + scanf ("%d", &a); + printf ("Εισάγετε τον δεύτερο αριθμό : "); + scanf ("%d", &b); + printf ("Εισάγετε τον τρίτο αριθμό : "); + scanf ("%d", &c); + if (a > b) + if (a > c) + printf("Ο μεγαλύτερος είναι ο %d και εισήχθη 1ος\n", a); + else + if (a == c) + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 1ος και 3ος\n", a); + else + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 3ος\n", c); + else + if (a == b) + if (a == c) + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 1ος, 2ος και 3ος\n", a); + else + if (a > c) + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 1ος και 2ος\n", a); + else + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 3ος\n", c); + else + if (b > c) + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 2ος\n", b); + else + if (b == c) + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 2ος και 3ος\n", a); + else + printf ("Ο μεγαλύτερος είναι ο %d και εισήχθη 3ος\n", c); + return 32; +} diff --git a/assignment-1.2-conditional-statements/examples/C2c.c b/assignment-1.2-conditional-statements/examples/C2c.c @@ -0,0 +1,17 @@ +#include <stdio.h> +#define t 6 + +int main () +{ + int i = 7; + switch (i) + { + case 3: printf ("3\n"); + case 5: printf ("5\n"); break; + case 3 + 4: printf ("7\n"); + default: printf ("default\n"); + case 1: printf ("1\n"); break; + case t: printf ("t\n"); + } +} + diff --git a/assignment-1.2-conditional-statements/int-comparison.c b/assignment-1.2-conditional-statements/int-comparison.c @@ -0,0 +1,23 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + int intArr[3], maxInt, index; + + printf("Integer 1: "); + scanf("%d", &intArr[0]); + + printf("Integer 2: "); + scanf("%d", &intArr[1]); + + printf("Integer 3: "); + scanf("%d", &intArr[2]); + + maxInt = (intArr[0] > intArr[1]) ? ((intArr[0] > intArr[2]) ? intArr[0] : intArr[2]) : ((intArr[1] > intArr[2]) ? intArr[1] : intArr[2]); + + index = (maxInt == intArr[0]) ? 1 : ((maxInt == intArr[1]) ? 2 : (maxInt == intArr[1]) ? 3 : 4); + + printf("The biggest number is %d in position %d.\n", maxInt, index); + + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.2-conditional-statements/quadratic-equation.c b/assignment-1.2-conditional-statements/quadratic-equation.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <math.h> + + +void linearEquation(); +void quadraticEquation(); + + +int main(int argc, char **argv) +{ + double a, b, c; + + printf("a: "); + scanf("%lf", &a); + printf("b: "); + scanf("%lf", &b); + printf("c: "); + scanf("%lf", &c); + + if (a != 0) + quadraticEquation(a, b, c); + else + linearEquation(b, c); + + return 0; +} + + +void linearEquation(double b, double c) +{ + double x; + + if (b != 0) + { + x = -c / b; + printf("x = %.2lf\n", x); + } + else + printf("Infinite solutions.\n"); +} + + +void quadraticEquation(double a, double b, double c) +{ + double x, x1, x2, D = pow(b, 2) - 4*(a*c); + + if (D > 0) + { + x1 = (-b + sqrt(D)) / (2*a); + x2 = (-b - sqrt(D)) / (2*a); + printf("x1 = %.2lf\nx2 = %.2lf\n", x1, x2); + } + else if (D == 0) + { + x = (-b) / (2*a); + printf("x = %.2lf\n", x); + } + else + { + printf("There are no solutions.\n"); + } +}+ \ No newline at end of file diff --git a/assignment-1.3-loops/int-calcs.c b/assignment-1.3-loops/int-calcs.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + int num, averPos, sqrOdd; + int sumPos = 0, numPos = 0, numEven = 0, prodNeg = 1; + + do + { + printf("Number: "); + scanf("%d", &num); + + if (num > 0) + { + sumPos += num; + numPos += 1; + + + if (num % 2 != 0) + { + sqrOdd = num * num; + printf("Square of odd number %d: %d\n", num, sqrOdd); + } + else + numEven += 1; + } + else if (num < 0) + prodNeg *= num; + } + while (num != 0); + + averPos = sumPos / numPos; + + printf("Average value of positive numbers: %d\n", averPos); + printf("Product of negative numbers: %d\n", prodNeg); + printf("Number of even numbers: %d\n", numEven); + + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.3-loops/shapes.c b/assignment-1.3-loops/shapes.c @@ -0,0 +1,71 @@ +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + int row, rowsTotal, col, colsTotal, spaces; + + printf("How many rows?: "); + scanf("%d", &rowsTotal); + colsTotal = rowsTotal; + + printf("\n"); + + for (row = 0; row < rowsTotal; row++) + { + printf("*\n"); + for (col = row+1; col > 0; col--) + { + if (row+1 < colsTotal) + printf("*"); + } + } + + printf("\n"); + + for (row = 0; row < rowsTotal; row++) + { + for (col = row+1; col < colsTotal; col++) + printf(" "); + + for (col = 0; col <= row; col++) + printf("*"); + + printf("\n"); + } + + printf("\n"); + + spaces = rowsTotal; + for (row = 1; row <= rowsTotal; row++) + { + for (col = 1; col < spaces; col++) + printf(" "); + + for (col = 0; col < 2*row - 1; col++) + printf("*"); + + printf("\n"); + spaces--; + } + + printf("\n"); + + for (row = 1; row <= rowsTotal; row++) + { + for (col = 1; col <= colsTotal; col++) + { + if (row == 1 || row == rowsTotal || col == 1 || col == colsTotal) + printf("*"); + else if (row == col || col == (rowsTotal - row + 1)) + printf("·"); + else + printf(" "); + } + + printf("\n"); + } + + printf("\n"); + + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.3-loops/sine-taylor.c b/assignment-1.3-loops/sine-taylor.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <math.h> + +#define PI 3.141592654 +#define ACCURACY 0.000001 + +double power(double, int); +double factorial(int); + + +int main(int argc, char *argv[]) +{ + double xDegrees, xRads, currentFrac, previousFrac, sine = 0; + int exponent = 1, sign = 1; + + printf("x (in degrees): "); + scanf("%lf", &xDegrees); + xRads = xDegrees * (PI/180.0); + + currentFrac = power(xRads, exponent) / factorial(exponent); + + do + { + sine += sign * currentFrac; + + exponent += 2; + + previousFrac = currentFrac; + currentFrac = power(xRads, exponent) / factorial(exponent); + + sign *= -1; + } + while (fabs(previousFrac - currentFrac) > ACCURACY); + + printf("sin(%lf) = sin(%lf) = %lf rads\n", xDegrees, xRads, sine); + + return 0; +} + + +double power(double xRads, int exponent) +{ + int i; + double value; + + for (i = 0, value = 1; i < exponent; i++) + value *= xRads; + + return value; +} + + +double factorial(int exponent) +{ + int i; + double fac; + + for (i = 1, fac = 1; i <= exponent; i++) + fac *= i; + + return fac; +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/README.md b/assignment-1.4-functions/README.md @@ -0,0 +1,45 @@ +# Compilation, Linking & Execution steps + +* ```program``` = program's name +* ```..../``` = full path +* ```$``` = terminal command + +## Linux + +### Method 1 + +1. Open a terminal emulator, or the integrated terminal of your editor. +2. Move to the ```src``` directory of the program: ```$ cd ....programs/program/src``` +3. Compile ```main.c``` and ```program-libs.c```: ```$ gcc -c main.c program-libs.c && mv main.o program-libs.o ../obj && cd ../obj``` +4. Link ```main.o``` and ```program-libs.o```: ```$ gcc main.o program-libs.o -lm && mv a.out ../exec && cd ../exec``` +5. Execute program: ```$ ./a.out``` + +### Method 2 + +1. Open a terminal emulator, or the integrated terminal of your editor. +2. Move the ```programs``` directory: ```$ cd …./programs``` +3. _In case step 4 fails, do this first_: ```$ chmod +x autocompile.sh``` +4. Run the ```autocompile.sh``` script: ```$ ./autocompile.sh program``` + +## Windows + +__Requirements:__ MinGW gcc compiler + +### Method 1 + +1. Open Command Prompt, or the integrated terminal of your editor. +2. Move to the ```src``` directory of the program: ```$ cd ....programs/program/src``` +3. Compile ```main.c``` and ```program-libs.c```: ```$ gcc -c main.c program-libs.c && mv main.o program-libs.o ../obj && cd ../obj``` +4. Link ```main.o``` and ```program-libs.o```: ```$ gcc main.o program-libs.o -lm && mv a.out ../exec && cd ../exec``` +5. Execute program: ```$ ./a.exe``` + +### Method 2 + +1. Open Command Prompt, or the integrated terminal of your editor. +2. Move the ```programs``` directory: ```cd ..../programs``` +3. Run the ```autocompile.bat``` script: ```autocompile``` +4. Enter the name of the program __exactly__ as it's written in the folder when the script prints ```"Program name: "``` + + + +In case you want to compile and run ```hanoi-tower```, just do: ```$ gcc -c hanoi-tower.c && gcc hanoi-tower.o && ./a.out```+ \ No newline at end of file diff --git a/assignment-1.4-functions/autocompile.bat b/assignment-1.4-functions/autocompile.bat @@ -0,0 +1,26 @@ +@ECHO OFF + +set /p "program=Program name: " + +:: Cd to program directory +cd %program% + +:: Create (if missing) obj and exec directories +if not exist obj md obj +if not exist exec md exec + +:: Cd to source code directory +cd src + +:: Compile and move object files to the right directory +gcc -c main.c %program%-libs.c +move main.o ../obj && move %program%-libs.o ../obj +cd ../obj + +:: Link and move executable to the right directory +gcc main.o %program%-libs.o -lm +move a.out ../exec +cd ../exec + +:: Execute program +a.exe+ \ No newline at end of file diff --git a/assignment-1.4-functions/autocompile.sh b/assignment-1.4-functions/autocompile.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Cd to program directory +cd $1 + +# Create (if missing) obj and include directories +mkdir -p obj exec + +# Cd to source code directory +cd src + +# Compile and move new files to the right directory +gcc -c main.c $1-libs.c && mv main.o $1-libs.o ../obj && cd ../obj; +gcc main.o $1-libs.o -lm && mv a.out ../exec && cd ../exec; + +# Execute program +./a.out+ \ No newline at end of file diff --git a/assignment-1.4-functions/hanoi-tower/hanoi-tower.c b/assignment-1.4-functions/hanoi-tower/hanoi-tower.c @@ -0,0 +1,33 @@ +#include <stdio.h> + +void moves(int, char, char, char); + +int main(int argc, char **argv) +{ + int numDisks; + + printf("\t\tTower of Hanoi\n\n"); + + do + { + printf("Number of disks: "); + scanf("%d", &numDisks); + } while (numDisks <= 0); + + printf("\n"); + moves(numDisks, 'A', 'B', 'C'); + + return 0; +} + +void moves(int numDisks, char tower1, char tower2, char tower3) +{ + if (numDisks == 1) + printf("Move disk 1 from tower %c to tower %c\n", tower1, tower3); + else + { + moves(numDisks - 1, tower1, tower3, tower2); + printf("Move disk %d from tower %c to tower %c\n", numDisks, tower1, tower3); + moves(numDisks - 1, tower2, tower1, tower3); + } +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/menu/exec/a.out b/assignment-1.4-functions/menu/exec/a.out Binary files differ. diff --git a/assignment-1.4-functions/menu/obj/main.o b/assignment-1.4-functions/menu/obj/main.o Binary files differ. diff --git a/assignment-1.4-functions/menu/obj/menu-libs.o b/assignment-1.4-functions/menu/obj/menu-libs.o Binary files differ. diff --git a/assignment-1.4-functions/menu/src/main.c b/assignment-1.4-functions/menu/src/main.c @@ -0,0 +1,17 @@ +#include <stdio.h> +#include "menu.h" + + +int main(int argc, char **argv) +{ + int a, b; + + printf("A: "); + scanf("%d", &a); + printf("B: "); + scanf("%d", &b); + + menu(a, b); + + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/menu/src/menu-full.c b/assignment-1.4-functions/menu/src/menu-full.c @@ -0,0 +1,110 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +void menu(int, int); +void pause(); +void results(int, int, int); +int power(int, int); +int factorial(int); +int combinations(int, int); + + +int main(int argc, char **argv) +{ + int a, b; + + printf("A: "); + scanf("%d", &a); + printf("B: "); + scanf("%d", &b); + + menu(a, b); + + return 0; +} + + +void menu(int a, int b) +{ + int menuChoice; + + do + { + system("clear||cls"); + + printf("[1] %d to the power of %d\n[2] %d! and %d!\n[3] Number of combinations between %d and %d\n[4] Exit\n", a, b, a, b, a, b); + printf("Choice: "); + scanf(" %d", &menuChoice); + + switch(menuChoice) + { + case 1: + results(a, b, 1); + break; + case 2: + results(a, b, 2); + break; + case 3: + results(a, b, 3); + break; + } + + pause(); + } while (menuChoice != 4); + + results(a, b, 4); +} + +void pause() +{ + do + { + printf("\nPress [Enter] to continue. . ."); + getchar(); + } while (getchar() != '\n'); + +} + +void results(int a, int b, int menuChoice) +{ + static int numChoices = 0; + + switch(menuChoice) + { + case 1: + printf("%d^%d = %d\n", a, b, power(a, b)); + break; + case 2: + printf("%d! = %d\n", a, factorial(a)); + printf("%d! = %d\n", b, factorial(b)); + break; + case 3: + printf("Combinations between %d and %d: %d\n", a, b, combinations(a, b)); + break; + case 4: + printf("Total number of choices: %d\n", numChoices); + } + + numChoices++; +} + +int power(int a, int b) +{ + return pow(a, b); +} + +int factorial(int num) +{ + int i, fac; + + for (i = 1, fac = 1; i <= num; i++) + fac *= i; + + return fac; +} + +int combinations(int a, int b) +{ + return factorial(b) / (factorial (a) * factorial(abs(b-a))); +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/menu/src/menu-libs.c b/assignment-1.4-functions/menu/src/menu-libs.c @@ -0,0 +1,89 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include "menu.h" + + +void menu(int a, int b) +{ + int menuChoice; + + do + { + system("clear||cls"); + + printf("[1] %d to the power of %d\n[2] %d! and %d!\n[3] Number of combinations between %d and %d\n[4] Exit\n", a, b, a, b, a, b); + printf("Choice: "); + scanf(" %d", &menuChoice); + + switch(menuChoice) + { + case 1: + results(a, b, 1); + break; + case 2: + results(a, b, 2); + break; + case 3: + results(a, b, 3); + break; + } + + pause(); + } while (menuChoice != 4); + + results(a, b, 4); +} + +void pause() +{ + do + { + printf("\nPress [Enter] to continue. . ."); + getchar(); + } while (getchar() != '\n'); + +} + +void results(int a, int b, int menuChoice) +{ + static int numChoices = 0; + + switch(menuChoice) + { + case 1: + printf("%d^%d = %d\n", a, b, power(a, b)); + break; + case 2: + printf("%d! = %d\n", a, factorial(a)); + printf("%d! = %d\n", b, factorial(b)); + break; + case 3: + printf("Combinations between %d and %d: %d\n", a, b, combinations(a, b)); + break; + case 4: + printf("Total number of choices: %d\n", numChoices); + } + + numChoices++; +} + +int power(int a, int b) +{ + return pow(a, b); +} + +int factorial(int a) +{ + int i, fac; + + for (i = 1, fac = 1; i <= a; i++) + fac *= i; + + return fac; +} + +int combinations(int a, int b) +{ + return factorial(b) / (factorial (a) * factorial(abs(b-a))); +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/menu/src/menu.h b/assignment-1.4-functions/menu/src/menu.h @@ -0,0 +1,11 @@ +#ifndef HEADER_FILE +#define HEADER_FILE + +void menu(int, int); +void pause(); +void results(int, int, int); +int power(int, int); +int factorial(int); +int combinations(int, int); + +#endif+ \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/exec/a.out b/assignment-1.4-functions/sine-cos-taylor/exec/a.out Binary files differ. diff --git a/assignment-1.4-functions/sine-cos-taylor/obj/main.o b/assignment-1.4-functions/sine-cos-taylor/obj/main.o Binary files differ. diff --git a/assignment-1.4-functions/sine-cos-taylor/obj/sine-cos-taylor-libs.o b/assignment-1.4-functions/sine-cos-taylor/obj/sine-cos-taylor-libs.o Binary files differ. diff --git a/assignment-1.4-functions/sine-cos-taylor/src/main.c b/assignment-1.4-functions/sine-cos-taylor/src/main.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <math.h> +#include "sine-cos-taylor.h" + + +int main(int argc, char **argv) +{ + double xDegrees, xRads; + + xDegrees = input(); + + xRads = rads_conv(xDegrees); + + if (compare(sine_calc(xRads), sin(xRads))) + printf("sin(%f) = sin(%f) = %f rads\n", xDegrees, xRads, sine_calc(xRads)); + else + printf("Error.\n"); + + if (compare(cosine_calc(xRads), cos(xRads))) + printf("cos(%f) = cos(%f) = %f rads\n", xDegrees, xRads, cosine_calc(xRads)); + else + printf("Error.\n"); + + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-full.c b/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-full.c @@ -0,0 +1,124 @@ +#include <stdio.h> +#include <math.h> + +#define PI 3.141592654 +#define ACCURACY 0.000001 +#define COMPARISON 0.000009 + +double input(); +double rads_conv(double); +double sine_calc(double); +double cosine_calc(double); +double power(double, int); +double factorial(int); +int compare(double, double); + + +int main(int argc, char **argv) +{ + double xDegrees, xRads; + + xDegrees = input(); + + xRads = rads_conv(xDegrees); + + if (compare(sine_calc(xRads), sin(xRads))) + printf("sin(%f) = sin(%f) = %f rads\n", xDegrees, xRads, sine_calc(xRads)); + else + printf("Error.\n"); + + if (compare(cosine_calc(xRads), cos(xRads))) + printf("cos(%f) = cos(%f) = %f rads\n", xDegrees, xRads, cosine_calc(xRads)); + else + printf("Error.\n"); + + return 0; +} + + +double input() +{ + double xDegrees; + + printf("x (in degrees): "); + scanf("%lf", &xDegrees); + + return xDegrees; +} + +double rads_conv(double xDegrees) +{ + return xDegrees * (PI/180.0); +} + +double sine_calc(double xRads) +{ + double currentFrac, previousFrac, sineValue = 0; + int exponent = 1, sign = 1; + + currentFrac = power(xRads, exponent) / factorial(exponent); + + do + { + sineValue += sign * currentFrac; + + exponent += 2; + + previousFrac = currentFrac; + currentFrac = power(xRads, exponent) / factorial(exponent); + + sign *= -1; + } while (fabs(previousFrac - currentFrac) > ACCURACY); + + return sineValue; +} + +double cosine_calc(double xRads) +{ + double currentFrac = 1, previousFrac, cosineValue = 0; + int exponent = 0, sign = 1; + + do + { + cosineValue += sign * currentFrac; + + exponent += 2; + + previousFrac = currentFrac; + currentFrac = power(xRads, exponent) / factorial(exponent); + + sign *= -1; + } while (fabs(previousFrac - currentFrac) > ACCURACY); + + return cosineValue; +} + +double power(double xRads, int exponent) +{ + int i; + double value; + + for (i = 0, value = 1; i < exponent; i++) + value *= xRads; + + return value; +} + +double factorial(int exponent) +{ + int i; + double fac; + + for (i = 1, fac = 1; i <= exponent; i++) + fac *= i; + + return fac; +} + +int compare(double calcValue, double funcValue) +{ + if (fabs(calcValue - funcValue) <= COMPARISON) + return 1; + else + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-libs.c b/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor-libs.c @@ -0,0 +1,94 @@ +#include <stdio.h> +#include <math.h> +#include "sine-cos-taylor.h" + +#define PI 3.141592654 +#define ACCURACY 0.000001 +#define COMPARISON 0.000009 + +double input() +{ + double xDegrees; + + printf("x (in degrees): "); + scanf("%lf", &xDegrees); + + return xDegrees; +} + +double rads_conv(double xDegrees) +{ + return xDegrees * (PI/180.0); +} + +double sine_calc(double xRads) +{ + double currentFrac, previousFrac, sineValue = 0; + int exponent = 1, sign = 1; + + currentFrac = power(xRads, exponent) / factorial(exponent); + + do + { + sineValue += sign * currentFrac; + + exponent += 2; + + previousFrac = currentFrac; + currentFrac = power(xRads, exponent) / factorial(exponent); + + sign *= -1; + } while (fabs(previousFrac - currentFrac) > ACCURACY); + + return sineValue; +} + +double cosine_calc(double xRads) +{ + double currentFrac = 1, previousFrac, cosineValue = 0; + int exponent = 0, sign = 1; + + do + { + cosineValue += sign * currentFrac; + + exponent += 2; + + previousFrac = currentFrac; + currentFrac = power(xRads, exponent) / factorial(exponent); + + sign *= -1; + } while (fabs(previousFrac - currentFrac) > ACCURACY); + + return cosineValue; +} + +double power(double xRads, int exponent) +{ + int i; + double value; + + for (i = 0, value = 1; i < exponent; i++) + value *= xRads; + + return value; +} + +double factorial(int exponent) +{ + int i; + double fac; + + for (i = 1, fac = 1; i <= exponent; i++) + fac *= i; + + return fac; +} + +int compare(double calcValue, double funcValue) +{ + if (fabs(calcValue - funcValue) <= COMPARISON) + return 1; + else + return 0; +}+ \ No newline at end of file diff --git a/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.h b/assignment-1.4-functions/sine-cos-taylor/src/sine-cos-taylor.h @@ -0,0 +1,12 @@ +#ifndef HEADER_FILE +#define HEADER_FILE + +double input(); +double rads_conv(double); +double sine_calc(double); +double cosine_calc(double); +double power(double, int); +double factorial(int); +int compare(double, double); + +#endif+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/combinations/src/combinations.c b/assignment-1.5-arrays-pointers-files/combinations/src/combinations.c @@ -0,0 +1,111 @@ +#include <stdio.h> + + +int define_size(); +void fill_array(int); +int exists_in_array(int*, int, int); +void pairs(); + + +int main(int argc, char *argv[]) +{ + int N = define_size(); + + fill_array(N); + pairs(); + + return 0; +} + + + +int define_size() +{ + int N; + + do + { + printf("N: "); + scanf("%d", &N); + } while (N <= 6 || N > 49); + + return N; +} + + + +void fill_array(int N) +{ + int i = 0, num, arr[N]; + + do + { + printf("arr[%d]: ", i); + scanf("%d", &num); + + if (num >= 1 && num <= 49) + { + if (i == 0) + { + arr[i] = num; + i++; + } + else + { + if (!exists_in_array(arr, N, num)) + { + arr[i] = num; + i++; + } + else + printf("Give a different number.\n"); + } + } + else + printf("Give a number in [1, 49].\n"); + + } while (i < N); +} + + + +int exists_in_array(int *arr, int N, int num) +{ + int *arrEnd = arr + N - 1; + + while (arr <= arrEnd && *arr != num) + arr++; + + if (arr <= arrEnd) + return 1; + else + return 0; +} + + + +void pairs() +{ + int x1, x2, y1, y2; + + do + { + printf("x1: "); + scanf("%d", &x1); + + printf("x2: "); + scanf("%d", &x2); + + } while (x1 < 0 || x1 > x2 || x2 > 6); + + do + { + printf("y1: "); + scanf("%d", &y1); + + printf("y2: "); + scanf("%d", &y2); + + } while (y1 < 21 || y1 > y2 || y2 > 279); + +}+ \ No newline at end of file diff --git a/assignment-1.5-arrays-pointers-files/examples/1DDynamic.c b/assignment-1.5-arrays-pointers-files/examples/1DDynamic.c @@ -0,0 +1,223 @@ +#include <stdio.h> +#include <stdlib.h> + +int GetIntWithinLimits (int, int); +int *ReadMatrix (int); +int ReadMatrix1 (int **M, int S); +void PrintMatrix (int *, int); +int CompareMatrices (int *M1, int M2[], int S); +int GetRandom (int, int); +void RandomFill (int *, int, int, int); +int IsSorted (int *, int); +int LudicrousSort (int *, int); +void Strange1 (int *, int); + +int main (int argc, char **argv) +{ + int *Mat1; + int *Mat2; + int *Mat3; + int NoN; + + srand (time (NULL)); + + NoN = GetIntWithinLimits (1, 100000); + printf ("\n\n"); + Mat1 = ReadMatrix (NoN); + PrintMatrix (Mat1, NoN); + printf ("\n\n"); + if (ReadMatrix1 (&Mat2, NoN)) + PrintMatrix (Mat2, NoN); + else + printf ("Not enough memory...\n"); + printf ("\n\n"); + if (CompareMatrices (Mat1, Mat2, NoN)) + printf ("Matrices are Equal\n"); + else + printf ("Matrices are not Equal\n"); + printf ("\n\n"); + // Δοκιμάστε το παρακάτω για λίγο μεγαλύτερους πίνακες, π.χ. με 15 στοιχεία. + Mat3 = (int *) malloc (5 * sizeof (int)); + RandomFill (Mat3, 5, -5, 300); + PrintMatrix (Mat3, 5); + printf ("Matrix 3 Sorted. Steps: %d\n", LudicrousSort (Mat3, 5)); + PrintMatrix (Mat3, 5); + printf ("\n\n"); + Strange1 (Mat1, NoN); + PrintMatrix (Mat1, NoN); + + +} + +/** + * Διαβάζει από το πληκτρολόγιο και επιστρέφει ένα ακέραιο αριθμό μέσα σε συγκεκριμένο διάστημα. + * Παρόμοια τεχνική πρέπει να χρησιμοποιείται κάθε φορά που πρέπει να εισαχθούν τιμές και να γίνει έλεγχος ορθότητάς τους + */ +int GetIntWithinLimits (int L1, int L2) +{ + int R; + do + { + printf ("Enter an Integer Between %d and %d : ", L1, L2); + scanf ("%d", &R); + } + while (R < L1 || R > L2); + return R; +} + +/* + * Δημιουργεί έναν δυναμικά δεσμευμένο πίνακα ακεραίων συγκεκριμένου μεγέθους, τον γεμίζει με τιμές από την standard είσοδο + * και τον επιστρέφει. Σε περίπτωση προβλήματος δέσμευσης μνήμης η εκτέλεση του προγράμματος διακόπτεται. + * Πώς θα μπορούσε να επιστρέφεται η πληροφορία ότι δεν έγινε η δέσμευση μνήμης (αν αποτύχει η malloc) και να μην χρειαστεί να τερματιστεί το πρόγραμμα; + */ +int *ReadMatrix (int S) +{ + int *T, i; + T = (int *) malloc (S * sizeof (int)); + if (T == NULL) + { + //Ενημέρωση χρήστη για μοιραίο πρόβλημα... + exit (1); + } + for (i = 0; i < S; i++) + { + printf ("Enter Value for Position %d: ", i + 1); + scanf ("%d", T + i); //Γιατί T + i και όχι &T[i]; + } + return T; +} + +/* + * Δημιουργεί έναν δυναμικά δεσμευμένο πίνακα ακεραίων συγκεκριμένου μεγέθους, τον γεμίζει με τιμές από την standard είσοδο + * και επιστρέφει true αν η δέσμευση έγινε κανονικά, false διαφορετικά. + * Η διαφορά με την προηγούμενη συνάρτηση είναι ότι η διεύθυνση του πίνακα δεν επιστρέφεται με return αλλά τοποθετείται + * στον pointer που δίδεται ως είσοδος. Η τεχνική αυτή χρησιμοποιείται αν η συνάρτηση πρέπει να δημιουργήσει περισότερους + * του ενός πίνακες ή αν πρέπει να επιστρέψει και κάποια άλλη πληροφορία, όπως σε αυτή την περίπτωση. + * Είναι κατανοητό γιατί ο pointer είναι διπλός; + */ +int ReadMatrix1 (int **M, int S) +{ + int i; + *M = (int *) malloc (S * sizeof (int)); + if (*M == NULL) + return 0; + for (i = 0; i < S; i++) + { + printf ("Enter Value for Position %d: ", i + 1); + scanf ("%d", *M + i); + } + return 1; +} + +/* + * Τυπώνει τα στοιχεία ενός πίνακα ακεραίων. + * Τροποποιείστε την συνάρτηση ώστε κάθε ένα συγκεκριμένο αριθμό γραμμών να σταματά το + * τύπωμα και να περιμένει να πατηθεί <Enter> από τον χρήστη. + */ +void PrintMatrix (int *Mat, int N) +{ + int i; + for (i = 0; i < N; i++) + printf ("Element %4d Value: %5d\n", i + 1, Mat[i]); +} + +/** + * Συγκρίνει τα περιεχόμενα 2 πινάκων και επιστρέφει true αν όλα τα στοιχεία τους είναι ίσα ένα προς ένα. + * Δοκιμάστε την με και χωρίς το else. + */ +int CompareMatrices (int *M1, int M2[], int S) +{ + int i; + for (i = 0; i < S; i++) + if (M1[i] != *(M2 + i)) + return 0; + //else + return 1; +} + +/** + * Επιστρέφει ένα τυχαίο αριθμό στο διάστημα [L1...L2]. + * Το L2 πρέπει να είναι μεγαλύτερο ή ίσο του L1 με ευθύνη του προγραμματιστή + */ +int GetRandom (int L1, int L2) +{ + return random () % (L2 - L1 + 1) + L1; +} + +/** + * Γεμίζει έναν πίνακα ακεραίων με τυχαίες τιμές σε συγκεκριμένα όρια + */ +void RandomFill (int *P, int N, int O1, int O2) +{ + int i; + for (i = 0; i < N; P[i++] = GetRandom (O1, O2)); +} + +/** + * Ελέγχει αν είνας πίνακας είναι ταξινομημένος. + * Δουλεύει αν ο πίνακας έχει ένα μόνο ή κανένα στοιχείο; + */ +int IsSorted (int *P, int S) +{ + int i; + for (i = 1; i < S; i++) + if (P[i] < P[i - 1]) + return 0; + return 1; +} + +/** + * Ταξινομεί έναν πίνακα με ένα καθόλου έξυπνο αλγόριθμο και επιστρέφει το πλήθος των επαναλήψεων που πραγματοποιήθηκαν. + * Ο αριθμός που θα επιστρέφεται θα είναι πάντα σωστός; Αν όχι σε ποια περίπτωση και τι μπορεί να γίνει για αυτό; + * Βελτιώστε την απόδοση της συνάρτησης διατηρώντας την βασική ιδέα. + */ +int LudicrousSort (int *Mat, int N) +{ + int C = 0; + int tmp; + int i, j; + while (!IsSorted (Mat, N)) + { + C++; + i = GetRandom (0, N - 1); + j = GetRandom (0, N - 1); + tmp = Mat[i]; + Mat[i] = Mat[j]; + Mat[j] = tmp; + } + return C; +} + +/** + * Τι κάνει αυτή η συνάρτηση; + * Υλοποιείστε την με διαφορετικό τρόπο + */ +void Strange1 (int *P, int S) +{ + int i; + char *p; + p = (char *)P; + for (i = 0; i < S; i++) + { + *p = *p & (unsigned char ) 254; + p += sizeof (int); + } +} + +/* +================================================================================================= +Γράψτε μία συνάρτηση η οποία θα επιστρέφει τους δύο μεγαλύτερους αριθμούς ενός πίνακα σαρώνοντάς +τον μόνο μία φορά. + +Υλοποιείστε μία δική σας συνάρτηση η οποία θα λειτουργεί όπως η "realloc" + +Υλοποιείστε σειριακή και δυαδική αναζήτηση +*/ + + + + + + + + diff --git a/assignment-1.5-arrays-pointers-files/examples/Lexico.c b/assignment-1.5-arrays-pointers-files/examples/Lexico.c @@ -0,0 +1,149 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +int GetSize (void); +void ClearBuf (void); +void RemoveEnter (char *); +char **ReadWords (int); +void PrintWords (char **, int); +void FreeMem (char **, int); +void SortWords (char **, int); +void SortWordsByLength (char **, short int *, int); +short int *CreateLexeisSize (char **, int); +void ShowMemory (char **, int); + +int main (int argc, char **argv) +{ + char **Lexeis; + short int *LexeisSize; + int N; + N = GetSize (); + Lexeis = ReadWords (N); + PrintWords (Lexeis, N); + SortWords (Lexeis, N); + PrintWords (Lexeis, N); + LexeisSize = CreateLexeisSize (Lexeis, N); + SortWordsByLength (Lexeis, LexeisSize, N); + PrintWords (Lexeis, N); + ShowMemory (Lexeis, N); + FreeMem (Lexeis, N); + +} + +int GetSize () +{ + int S; + do + { + printf ("Enter Number of Words: "); + scanf ("%d", &S); + } + while (S < 1); + ClearBuf (); + return S; +} + +void ClearBuf () +{ + char ch; + while (( ch = getc (stdin)) != '\n' && ch != EOF); +} + +void RemoveEnter (char *W) +{ + int i; + for (i = 0; W[i] != 10 && W[i] != 13; i++); + W[i] = 0; +} + +char **ReadWords (int S) +{ + char **Words; + char *Buf; + int i; + Buf = (char *) malloc (201); + Words = (char **) malloc (S * sizeof (char *)); + for (i = 0; i < S; i++) + { + printf ("Enter word %d: ", i + 1); + fgets (Buf, 201, stdin); + RemoveEnter (Buf); + Words[i] = (char *) malloc (strlen (Buf) + 1); + strcpy (Words[i], Buf); + } + free (Buf); + return Words; +} + +void PrintWords (char **Words, int NoW) +{ + int i; + printf (" AA Word Length\n"); + for (i = 0; i < NoW; i++) + printf ("[%3d] %-30s (%2d)\n", i + 1, Words[i], strlen (Words[i])); + printf ("----------------------------------------------------------------------------------\n\n"); +} + +void FreeMem (char **L, int N) +{ + int i; + for (i = 0; i < N; i++) + free (*(L + i)); + free (L); +} + +void SortWords (char **Words, int S) +{ + char *tmp; + int i, j; + for (i = 0; i < S - 1; i++) + for (j = i + 1; j < S; j++) + if (strcmp (Words[i], Words[j]) > 0) + { + tmp = Words[i]; + Words[i] = Words[j]; + Words[j] = tmp; + } +} + +void SortWordsByLength (char **Words, short int *WL, int S) +{ + short int tmp; + char *tmp1; + int i, j; + for (i = 0; i < S - 1; i++) + for (j = i + 1; j < S; j++) + if (WL[i] > WL[j]) + { + tmp = WL[i]; + WL[i] = WL[j]; + WL[j] = tmp; + tmp1 = Words[i]; + Words[i] = Words[j]; + Words[j] = tmp1; + } +} + +short int *CreateLexeisSize (char **Words, int S) +{ + short int *Mat; + int i; + Mat = (short int *) malloc (S * sizeof (short int)); + for (i = 0; i < S; i++) + *(Mat + i) = strlen (*(Words + i)); + return Mat; +} + +void ShowMemory (char **W, int P) +{ + int i; + printf ("The Lexeis pointer Contains: %p\n", W); + printf (" Lexeis+i Lexeis[i] Word\n"); + printf (" --------------\n"); + for (i = 0; i < P; i++) + { + printf (" %10p | %10p | ---> %s\n", W + i, *(W + i), *(W + i)); + printf (" --------------\n"); + } +} diff --git a/assignment-1.5-arrays-pointers-files/examples/mystring.c b/assignment-1.5-arrays-pointers-files/examples/mystring.c @@ -0,0 +1,103 @@ +#include <stdio.h> +#include <string.h> + +int mystrcmp (char *, char *); +int mystrlen (char *); +void mystrcpy (char *, char *); +char *mystrcat (char *, char *); + +int main (int argc, char **argv) +{ + char Str1[50] = "Thanasis"; + char Str2[50] = "Maria"; + char Str3[50] = "MariaP"; + char Str4[50] = ""; + + printf ("Name 1 : %s\n", Str1); + printf ("Name 2 : %s\n", Str2); + printf ("Name 3 : %s\n", Str3); + printf ("Name 4 : %s\n", Str4); + + printf ("\n"); + printf ("Compare Name 1 with Name 2: %d \n", mystrcmp (Str1, Str2)); + printf ("Compare Name 2 with Name 1: %d \n", mystrcmp (Str2, Str1)); + printf ("Compare Name 1 with Name 1: %d \n", mystrcmp (Str1, Str1)); + printf ("Compare Name 2 with Name 3: %d \n", mystrcmp (Str2, Str3)); + printf ("Compare Name 3 with Name 2: %d \n", mystrcmp (Str3, Str2)); + + printf ("\n"); + printf ("Length of Name 1 : %d\n", mystrlen (Str1)); + printf ("Length of Name 2 : %d\n", mystrlen (Str2)); + printf ("Length of Null String : %d\n", mystrlen ("")); + + printf ("\n"); + mystrcpy (Str4, Str1); + printf ("Name 4: %s is copy of Name 1: %s\n", Str4, Str1); + + printf ("\n"); + mystrcat (Str4, Str2); + printf ("New Name 4: %s\n", Str4); + + +} + +/* + * Υλοποίηση της "strcmp" + * Αυτό που επιστρέφει είναι το ίδιο / συμβατό με αυτό που επιστρέφει η έτοιμη συνάρτηση; + * Υλοποιείστε την strncmp + */ + +int mystrcmp (char *s1, char *s2) +{ + int i; + for (i = 0; s1[i] == s2[i] && s1[i] != 0; i++); + return s1[i] - s2[i]; +} + +/* + * Υλοποίηση της "strlen". + * Ξεκαθαρείστε το νόημα της συνθήκης, συντακτικά και λογικά + */ +int mystrlen (char *s) +{ + int i; + for (i = 0; s[i]; i++); + return i; +} + +/* + * Υλοποίηση της "strcpy" + * Είναι κατανοητό πως δουλεύει; + * Σε τι διαφέρει από την έτοιμη συνάρτηση; Διορθώστε την ώστε να πληροί τις προδιαγραφές της έτοιμης συνάρτησης. + * Υλοποιείστε την με έναν... πιο αλγοριθμικό τρόπο. + * Υλοποιείστε την strncpy + */ +void mystrcpy (char *d, char *s) +{ + int i = 0; + while (d[i]=s[i++]); +} + +/* + * Υλοποίηση της "strcat" + * Υλοποιείστε την χωρίς την κλήση άλλης συνάρτησης. + * Υλοποιείστε την strncat + */ +char *mystrcat (char *d, char *s) +{ + char *tmp; + tmp = d; + while (*tmp) + tmp++; + // while (*(tmp++)); // Γιατί όχι μόνο αυτό; + mystrcpy (tmp, s); + return d; +} + +/* + * Υλοποιείστε τις παρακάτω συναρτήσεις: + * -strdup + * -strlwr + * -strupr + */ + diff --git a/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c b/assignment-1.5-arrays-pointers-files/minesweeper/src/minesweeper.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +int main(int argc, char *argv[]) +{ + + + return 0; +}+ \ No newline at end of file