From a24eb028a98f101c0d6910df489025d40e0348bd Mon Sep 17 00:00:00 2001 From: Akbar Rahman Date: Tue, 23 Jan 2024 16:27:24 +0000 Subject: [PATCH] add code for solutions for exam papers --- .../past_papers/20-21/code/1.c | 36 ++++++++ .../past_papers/20-21/code/2.c | 44 ++++++++++ .../past_papers/20-21/code/3.c | 59 +++++++++++++ .../past_papers/20-21/code/Makefile | 4 + .../past_papers/20-21/code/Results.txt | 15 ++++ .../past_papers/21-22/code/1 | Bin 0 -> 15792 bytes .../past_papers/21-22/code/1.c | 80 ++++++++++++++++++ .../past_papers/21-22/code/2 | Bin 0 -> 15680 bytes .../past_papers/21-22/code/2.c | 54 ++++++++++++ .../past_papers/22-23/code/1 | Bin 0 -> 15584 bytes .../past_papers/22-23/code/1.c | 32 +++++++ .../past_papers/22-23/code/2 | Bin 0 -> 15720 bytes .../past_papers/22-23/code/2.c | 50 +++++++++++ .../past_papers/22-23/code/3 | Bin 0 -> 15832 bytes .../past_papers/22-23/code/3.c | 48 +++++++++++ .../past_papers/22-23/code/Makefile | 4 + 16 files changed, 426 insertions(+) create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/1.c create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/2.c create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/3.c create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Makefile create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Results.txt create mode 100755 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1 create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1.c create mode 100755 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2 create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2.c create mode 100755 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1 create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1.c create mode 100755 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2 create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2.c create mode 100755 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3 create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3.c create mode 100644 uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/Makefile diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/1.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/1.c new file mode 100644 index 0000000..6d45619 --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/1.c @@ -0,0 +1,36 @@ +#include +#include + +#define WIDTH 10 +#define INPUT_BUFFER_LEN 64 +/// Used to clear the buffer scanf reads from. +/// Useful when user enters malformed input, as scanf does not clear +/// when it fails to read an input. +#define READ_STDIN_UNTIL_NEWLINE() while (getchar() != '\n'){}; + +int main() { + int rc, height, i, j; + char *input[INPUT_BUFFER_LEN]; + + do { + printf("Enter height: "); + rc = scanf("%d", &height); + READ_STDIN_UNTIL_NEWLINE(); + + if (rc == 1) { + break; + } + + } while (1); + + for(i = 0; i < height; i++) { + for (j = 0; j < i; j++) { + printf(" "); + } + for (j = 0; j < WIDTH; j++) { + printf("#"); + } + + printf("\n"); + } +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/2.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/2.c new file mode 100644 index 0000000..5ef3a61 --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/2.c @@ -0,0 +1,44 @@ +#include +#include + +#define BUF_LEN 1024 + +#define GRADE_FIRST 70 +#define GRADE_UPPER_SECOND 60 +#define GRADE_LOWER_SECOND 50 +#define GRADE_THIRD 40 + +int main() { + int rc, mark; + FILE *fp; + char buf[BUF_LEN]; + char first_name[BUF_LEN]; + char last_name[BUF_LEN]; + + fp = fopen("Results.txt", "r"); + if (fp == NULL) { + printf("Unable to open file Results.txt\n"); + return 1; + } + + while (fgets(buf, BUF_LEN, fp) != NULL) { + rc = sscanf(buf, "%s %s\t%d", first_name, last_name, &mark); + if (rc != 3) { + printf("Failed to read line: %s\n", buf); + return 1; + } + printf("%s %s\t", first_name, last_name); + + if (mark >= GRADE_FIRST) { + printf("First\n"); + } else if (mark >= GRADE_UPPER_SECOND ) { + printf("Upper second\n"); + } else if (mark >= GRADE_LOWER_SECOND) { + printf("Lower second\n"); + } else if (mark >= GRADE_THIRD) { + printf("Third\n"); + } else { + printf("Fail\n"); + } + } +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/3.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/3.c new file mode 100644 index 0000000..d2c8109 --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/3.c @@ -0,0 +1,59 @@ +#include +#include +#include + + +#define BUF_LEN 1024 +/// Used to clear the buffer scanf reads from. +/// Useful when user enters malformed input, as scanf does not clear +/// when it fails to read an input. +#define READ_STDIN_UNTIL_NEWLINE() while (getchar() != '\n'){}; + + +int cone_volume(float *volume, float radius, float height) { + if (radius < 0.0 || height < 0.0) { + return -1; + } + *volume = M_PI*radius*radius*height/3.0; + return 0; +} + +int cone_area(float *area, float radius, float height) { + if (radius < 0.0 || height < 0.0) { + return -1; + } + *area = M_PI*radius*(radius + sqrt(radius*radius + height*height)); + return 0; +} + +int main() { + int rc; + float result, height, radius; + char buf[BUF_LEN]; + + do { + printf("Enter radius of cone: "); + rc = scanf("%f", &radius); + READ_STDIN_UNTIL_NEWLINE(); + } while(rc != 1 && radius < 0.0); + + do { + printf("Enter height of cone: "); + rc = scanf("%f", &height); + READ_STDIN_UNTIL_NEWLINE(); + } while(rc != 1 && height < 0.0); + + rc = cone_area(&result, radius, height); + if(rc != 0) { + printf("Unable to calculate surface area\n"); + } else { + printf("Surface area: %.3f\n", result); + } + + rc = cone_volume(&result, radius, height); + if(rc != 0) { + printf("Unable to calculate volume\n"); + } else { + printf("Volume: %.3f\n", result); + } +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Makefile b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Makefile new file mode 100644 index 0000000..01e2cdf --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-lm + +%.c: + $(CC) $(CFLAGS) -c $< -o $* diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Results.txt b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Results.txt new file mode 100644 index 0000000..4f6c2cf --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/20-21/code/Results.txt @@ -0,0 +1,15 @@ +Fred Bloggs 73 +Michael Smith 43 +Claire White 64 +Simon Black 23 +Joshua Mason -20 +Jenny Jones 57 +George Clark 40 +Ann Bell 101 +Fay Allen 52 +John Morris 60 +Steven Hayes 47 +Hannah Jackson 94 +Alex Benson 50 +Susan Baker 70 +Wendy Brooks 57 \ No newline at end of file diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1 b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1 new file mode 100755 index 0000000000000000000000000000000000000000..4274626c5491bfb5f1f19dfd60c7ae15cdeac38a GIT binary patch literal 15792 zcmeHOeQX@X6`%75I6z{PCg21@7KkBfm5aX;oFJ1+VmoV;5H~n2P+)cO?b!?GJNIr) z9ZIQ@aT?2Y8<3zCr4pbD3KbQFwxXp}PQq6ZO^u3BRQ{+{mFnD3NClKY+gyKdX5MK; zgF-OnanGMY%4mk%dvt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br+WeP7@IdA$q~*@DOaUZ8$`L+df! zul1PdVb9G&0e${Xlj*Ml4q2b=JsQyG^#@aa9$@)^Iq`(n=llypOKSQRnw+n3{up`+ z9TH)EUZ*i#!Gf9w{T~kKfB$?X$n=(ge$fAKFivYkUXSy04u2mQlxLpl@1R@l)92@) zqpJa;v7F*&dB*AbqH~A+^ZM2M-c#p~&VOpG$H(y +#include + +#define BUF_SIZE (size_t) 4096 + +typedef struct { + int time_seconds; + float temperature; +} Datum; + +int main() { + int rc, i, data_size; + char buf[BUF_SIZE+1]; + char *rc_cp; + FILE *fp; + Datum *data; + + printf("Enter filename of temperature data: "); + rc = scanf("%s", buf); + // scanf return number of successfully scanned items + if (rc != 1) { + printf("Unable to read input\n"); + return -1; + } + + fp = fopen(buf, "r"); + if (fp == NULL) { + printf("Unable to open file '%s' for reading\n", buf); + return -1; + } + + rc_cp = fgets(buf, BUF_SIZE, fp); + if (rc_cp == NULL) { + printf("Unable to read first line of file\n"); + return -1; + } + + rc = sscanf(buf, "%d", &data_size); + if (rc != 1) { + printf("Unable to parse first line of file as integer\n"); + return -1; + } + + data = (Datum*) malloc(data_size * sizeof(Datum)); + + for(i = 0; i < data_size; i++) { + rc_cp = fgets(buf, BUF_SIZE, fp); + if (rc_cp == NULL) { + printf("Unexpected EOF or file read error\n"); + return -1; + } + + rc = sscanf(buf, "%d", &data[i].time_seconds); + if (rc != 1) { + printf("Unable to parse line as time (integer): %s\n", buf); + return -1; + } + + rc_cp = fgets(buf, BUF_SIZE, fp); + if (rc_cp == NULL) { + printf("Unexpected EOF or file read error\n"); + return -1; + } + + rc = sscanf(buf, "%f", &data[i].temperature); + if (rc != 1) { + printf("Unable to parse line as temperature (float): %s\n", buf); + return -1; + } + } + + printf("Time/s\tTemperature\n"); + for(i = 0; i < data_size; i++) { + printf("%d\t%.2f\n", data[i].time_seconds, data[i].temperature); + } + + fclose(fp); + free(data); + return 0; +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2 b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2 new file mode 100755 index 0000000000000000000000000000000000000000..1d0c989fbcd9b6739fcfa60601127a3e7e36de05 GIT binary patch literal 15680 zcmeHOeQX?85r22K8>g*vPRLgiXttrL+z+3>9Vf-jIrcg05a+8-X{)rG_1&I*&H2vU zty4Rq#g$uxz77`L1}y27$B3OWvqD?tB0Obn{Z|rM&bLavZ7{H$!nV6lWuiH$%wz z#GK&PM91OA5ZF*caxa8CZjj>K*hc=0I0nkY@0rp6WW0kEcS@1n2sb`p(Sni3I|Ol< zmrp8unh(=>%bka6%0DBeRa7P7-Hr8CiD+dao*Wpg9BgW=Y^)DvQo&j|1)NtkYzB;wPQ$=(`p+OgqLO9S#{DVq zirX1<%mt2dlW1CGP}gGdq@IYMG=!$X365wPQ%{>(pB_(&9>a|E>S@tGU}nG^&!i$d zc4(Q1o{WJCj*;V9r1!WM)8h%zpN=QZn7}^pnrIYz_I2!T)oO#=?N+_rstq>S?YerQ zb#!%VQ6p{i#51Om?&@q!q>@IL-krc{_4K8ZG%d|?E$Aq(D7boxVJm{8E`bfNB_A-4 z0P^*}As#QotMnLgYo%T+o+5obdeSarA@)I!I7yT` zc<238?%?I;h4hBcDF^R-4k-?vkC{R|@G{_Kz{`M_0WSky2D}XXPiEjs;LdC6$Xuy9 zRx$}|ZS{q$>CaEABfl)2lJm_s{2J)|w%@=$uq6a7)O*o3JCg_5b{2J9(#=jw{b#7- zQf@XY^*2$+rPu6_r2Yo#xa6As7U(#B&HEjr?>woF{zV;mcm7~kxF%b3UL9}V1)b*C zZ3g#+STL~Vc?f|CjyFFCj5=O&5@E-pxelg&J!nmRWqu~GWf=XY2*Ll5^l#XUaPM_> zbY7kOV3#_%Sfu*Ssqb7j*Fp!sr4CB-GqJ#ywj1#{zv1RGh!q2MN7Rw#H^5VR9s!pyb!R0{dJ94(43RyU_GKF?D0UQ?VOu%pdkQ+XKEwpbvHQnZ+xC)!_x$b8=iSQW<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f;^Z)1T#Z@rrr%`rY-E{7F6OJ>3$JRrc5K) zlN<>4r&Ik#+8lCF-2-v>D@Qy^RLky;N>lF<(!E#D^on3~C<#MYXr?X82_v0}r;g;>_C2u)9?b(knda2q<- z`{EH8H3faaE+_}EI)l86U?kPoXCxsj|GnVnJKPHNE%43%$Kua-9tnM&@_A73GnGGw zm;2ul$MEMZ>y55>_y3RZdt0aQbBVbQXgJ4zl|E=06?Pl#LUexl$UvB}5J>x(#WIAxCKbrRz=*qq|d|ChmtYY>j-bqC{m z3g-Eyslw9Nb&CZ6D`J}fjQ-PURN<@$&tr*AOFh~znlDcoyN%PI*iA) zoIC#8z+q0ffBSh(&vjnEf{PUQ`F|ByXS_mjjK{1ndrxM2m@}^UVRB$R!-=6DIG#KF zfh(TZ35>ixXMgwne@yZAFba7Ro+6cj^T+yI(7@y8_*05yVSK<+?Y(<^Tz@#@Clt$- z5%cR5_xKmUh-*1sAM<+$|6kAT&pP9uAvWNM=l7C}l|X{9pQYW zzr=$B!RsG|)LQ}TT>&mt{1Yhsu`>Vw literal 0 HcmV?d00001 diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2.c new file mode 100644 index 0000000..70abe61 --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2.c @@ -0,0 +1,54 @@ +#include +#include + +#define CLR_STDIN() while(getchar() != '\n'){} + +/// Calculate kinetic energy of object +/// +/// *e - pointer to store calculated energy in +/// m - mass of object, must be greater than 0 +/// v - velocity of object +/// +/// Returns 1 on success, 0 on failure +int calculate_kinetic_energy(float *e, float m, float v) { + // cannot have negative mass + if (m <= 0.0) return 0; + + *e = 0.5 * m * v * v; + + return 1; +} + +int main() { + int rc; + float e, m = 0.0, v = 0.0; + + do { + printf("Enter mass of object: "); + rc = scanf("%f", &m); + CLR_STDIN(); + if (rc != 1) { + continue; + } + } while (m <= 0.0); + + do { + printf("Enter velocity of object: "); + rc = scanf("%f", &v); + CLR_STDIN(); + if (rc != 1) { + continue; + } + break; + } while (1); + + rc = calculate_kinetic_energy(&e, m, v); + if (rc != 1) { + printf("calculate_kinetic_energy failed\n"); + return -1; + } + + printf("Kinetic energy: %.3f kJ\n", e/1000); + + return 0; +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1 b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1 new file mode 100755 index 0000000000000000000000000000000000000000..df8cf556c62bc47f922f5f1e6c83405c9319b3dc GIT binary patch literal 15584 zcmeHOYitzP6~607a2{qCii<;?mMcb$DS zCTaqzo3<9R`uLIdM}APLX(OtrKvM~&f7VbD6GfF#)d~?+bt@t*Dk+XkfV3&w@7%d( zJf5zrq>3sNwdhcEtO*`FXKuwk5uB#D&DN(v7A@& zSaLooC$zPy<8aCVY*YerFNBjfu6Rz_sr*?|3@T5&lZpH<@%AX*X-zgGoN|K+3zi)3 zOW;vnK5F5wxlhGg?t2KR{IgW)74;+W!ItLwkx1Q0Jdqu*8*go?YiahUll~^z1?pD= zHX2hqcMXV=BO*r_^F6{N^*e!4DN#Dx$#)GlN;~m|=jHj+Tjza;u4RAvwf9bbSeO0X zCi0=ie0?~$%EV{Xb%=4F z!fOMjopIK4+f5veqqmD?#)hj?Pa0=!IB~dKwBa0sY;!i8&oRjf$i1N3fV!kfpu&dR z&mW%+m-h$hjr)=fSGN>pafO=!Hv?`4+zhxGa5La$;M1Lf1@8m@&?l}|=ts+^>xIx? z$Yn|jv--quE6&OTD{OfaaG~Zc?7iy)sF6NQwkvZ5l$tZ7(-iN@tkhp8ou+nIa#BA< zI!(o{oPbX88sF`nyl_CD{Hs23d45lSurb$oUY}~a1fN3HCb(aZ`MvAE1r;5dYMTV2 zPnCa@#I{?RDs*)v)zQ^E3v=G}N67E2lJMUv{kQay*mzr?oY$xSzFnWbRjPZ==@)Ki z)}Vnu`OO9@3Ue{<`p!i@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C zVAL;e-?~n#>ow0WRWq`$TqpSwHalNaJeGX?%u!}5%+DQsey@?`E1VeB0Q0#$nh)6H z^L?Hr&!5@fIeveo_*NKAc`8ZwcYATh{|$^3pZUwZFUM^-{(<~_NWHGtBiCVrtgXS l!`DxGp5nfvYL$ykW_6SUDT_nJoV7SV*lZF$>mWE(@o(_zj%WY? literal 0 HcmV?d00001 diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1.c new file mode 100644 index 0000000..8df65d5 --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1.c @@ -0,0 +1,32 @@ +#include +#include +#include + +#define CLR_STDIN() while(getchar() != '\n'){} + +int main() { + int rc, i = 0; + float input, guess, prev; + + do { + printf("Number to square root: "); + rc = scanf("%f", &input); + CLR_STDIN(); + if (rc != 1) { + continue; + } + } while (input <= 0.0); + + guess = input/3; + + do { + i++; + prev = guess; + guess = 0.5 * (prev + (input/prev)); + printf("Input: %.5f Result: %.5f Iterations: %d Error: %f\n", input, guess, i, labs(prev-guess)/input); + } while ( fabs((prev-guess)/input) > 0.000001 ); + + printf("Input: %.5f Result: %.5f Iterations: %d\n", input, guess, i); + + return 0; +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2 b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2 new file mode 100755 index 0000000000000000000000000000000000000000..35904a6b64e41cdc80837ad437ff92b3db38328a GIT binary patch literal 15720 zcmeHOYit}>6~4QU8z*V9PMWk%OPXm&s-)B#zw%b-I`KN=5~n3~pefMFdLQ;G_O98T zO`V9AAVbMw1%>ykD1eZ7NJvolQI%5G38W72!zmJ~pcD&Hv~EJ{+M#j@Wckj$_iV<~ zbyX?^f96Ot=brDL@7_CS?#$lVIrGu*-gdvwCs?Y)y9HUNY84XF6~#@u02LBj#bWGl z6|2N`u;)ok)rX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W3%X42WPVZO4N>sa$X%lOBj??4u5-JD0@g9?6)7T6cBS+mRk2-FqY1UJ;BPO`!{= z?To5963=9l>C`O4LR}`_7a@W!4fNR}D2ExeHgbf`Jl)%V;>#pNyi{G}^*0C_7M%;UtJ)G~M4HPa!J*9iepqE(!f_@f*L};&lM` zM!rsYCk*=S^=ojh?~|CrzsqcIM#0HSSKox$rTAX41% z|1eNmt1zF}C#<<29zXlBegu44`>@UHAJ+7HNWwAZ@hen_lPFM3na}Gl)&=dz{WG7( z^(D>UrTut4$NH3mPisCm|C_)lCft7czOL`@yw26l%=(stu*a}D`G)4P=Hq9+t}b`; z9T%U!uVj6LYk|moZtn*!KCdHK^E#gW-Sd~e>!I;;{Dq-CwLYXxRvbUJe~N;Wf5=cZ ztnXw;t=;|8`ozgUWhh716kn&f`={@SXsyR#;^!0ouFvhyHtRou8*uRX`DL^oAcXyt zb&GUw_PZ z>hjzzvxPXM8ywS>bRVYk#N*-n#{ytHb_ZQFG%M?C+>x$ynOGB7=Ks1y5pHx5T&DOp DN|WVN literal 0 HcmV?d00001 diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2.c new file mode 100644 index 0000000..7ab1f44 --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2.c @@ -0,0 +1,50 @@ +#include +#include +#include + +#define CLR_STDIN() while(getchar() != '\n'){} + +int polygon_area(float *p_area, unsigned int sides, float length) { + if (sides < 3) return 0; + + *p_area = length*length*(float)sides / (4*tan(M_PI/sides)); + + return 1; +} + +int main() { + int rc; + unsigned int sides = 0; + float length, area; + + polygon_area(&area, sides, length); + + do { + printf("Enter number of sides: "); + rc = scanf("%u", &sides); + CLR_STDIN(); + + if (rc == 1) { + break; + } + } while (1); + + do { + printf("Enter side length: "); + rc = scanf("%f", &length); + CLR_STDIN(); + + if (rc == 1) { + break; + } + } while (1); + + rc = polygon_area(&area, sides, length); + if (rc == 0) { + printf("Error\n"); + return 1; + } + + printf("Area: %.2f\n", area); + return 0; +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3 b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3 new file mode 100755 index 0000000000000000000000000000000000000000..b8e05f1a2cd6eec8ccc2efc0087513415a88cef6 GIT binary patch literal 15832 zcmeHO4RBOf6~4P6CHy4;iKHO#tb_uZB|i`XowOVB!;6L=Ay`^}9?SkDYd5>;?%Osn zj>e{;#!MXiLq{uvIx2&sWvZ>M4(%dHp`)F2bR48JC8KmEJCsUCT2rg6=iGbF%ggI- zr=z1Yj_=OAd+zzp`R>no@9o~V@9byVI@`S-kKj@yZWpL!$`#@#!=46efbfe(F%Rlx zVyUuhDocIi463 zJQ`^oPU#057Qd{8aPsR2?M?yv4|Y^ZKn=SwAgb#e+guNpY;n%c5$ zx9|>#3}Vdp2+uU#OFFp7g1!skz_26#(J%MCa(V8`-`sogyWaML{oi_V&c&~w53U3I zL?0}O;lTxMd@%M-9L~}U zK)&@pQ~>_~;2Ipx(h0yVP7rmYqid@X3Ma$e(UciZc5Q8mCF0?(z`j^m7)Ez*B5tJ2 zK+-e}<(k(q(j7K~;2!J`7?Ef^5Q`o_He8EAV)q83aS=)Mg`p0{5~;92N>3mu`qE|! ze50vEaN|ZJ6%529h)J7)?BG2{u;(5l5{Sk`UosjuBLX_^#cR3&TS3*@(%G@O#i;Xb z5Zqd4wd$vub-wkuY%g5F9;m%=Rbma-IUL2ZM!N^_3`o8o7e%A<@UD55u!T~eA$~{t zPrrB=Zu27XBI)VH(OEc37IZB9Im(}3C*~{s9DPnQzL)SFer0Ez^~@a#hryutsdCPS z(?_B-j@j_pQ|{t|4TptUW!#3BS|~L9N|^})?DyG=I&ZUo#2xDjw8;6~to zCjx&dTlP17@Z}PHr1;cYA@re)>CKJlgJ()k%SGhYKMQcK>eo=0E%!qU^&Yfk$8%7s zeu+A6Ww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++;_mOW98;o(ibR8-godg=xZC#4m^L}PqF0n(>CKR^A7+Y^Y)p~`z@9-V-M^& zM~oe>pZ7gcz;MdU=lvT?{GPBEXZ-I4Bkqlu&-)>k%ZSYLXFryo20QMxSm*r~%N@jb z&Yxg03Jr`Y^Lf9=GDD6$zH|LwBmQRc@j76*e&+N4 zoic0DTZmOkST*xmz5y0HpP#oTh`-ljss{5|;(pQ2=l!H!qx^ZE+zs +#include +#include + +#define BUF_LEN 4096 +#define CLR_STDIN() while(getchar() != '\n'){} + +int main() { + char buf[BUF_LEN+1]; + char letter; + unsigned int mode; + FILE *fp; + int rc; + + do { + printf("Enter file name: "); + rc = scanf("%s", buf); + if (rc == 1) break; + } while(1); + + fp = fopen(buf, "r"); + if (fp == NULL) { + printf("Unable to open file '%s'\n", buf); + return -1; + } + + printf("---Formats---\n"); + printf("1) Character only\n"); + printf("2) ASCII only\n"); + printf("3) Character and ASCII\n"); + + do { + printf("Select format: "); + rc = scanf("%d", &mode); + CLR_STDIN(); + if (rc == 1 && mode < 4 && mode > 0) break; + } while(1); + + while ((letter = fgetc(fp)) != EOF) { + if (mode == 1 || mode == 3) printf("Character: %c", letter); + if (mode == 3) printf(" "); + if (mode == 2 || mode == 3) printf("ASCII: %u", letter); + printf("\n"); + } + + fclose(fp); + return 0; +} diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/Makefile b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/Makefile new file mode 100644 index 0000000..01e2cdf --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-lm + +%.c: + $(CC) $(CFLAGS) -c $< -o $*