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 0000000..4274626 Binary files /dev/null and b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1 differ diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1.c new file mode 100644 index 0000000..29fd34d --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/1.c @@ -0,0 +1,80 @@ +#include +#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 0000000..1d0c989 Binary files /dev/null and b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/21-22/code/2 differ 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 0000000..df8cf55 Binary files /dev/null and b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/1 differ 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 0000000..35904a6 Binary files /dev/null and b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/2 differ 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 0000000..b8e05f1 Binary files /dev/null and b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3 differ diff --git a/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3.c b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3.c new file mode 100644 index 0000000..4215ac7 --- /dev/null +++ b/uni/mmme/3085_computer_engineering_and_mechatronics/past_papers/22-23/code/3.c @@ -0,0 +1,48 @@ +#include +#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 $*