From 909bef9c7e595ac937952d093a27b52de004fa08 Mon Sep 17 00:00:00 2001 From: Peter Date: Fri, 6 Aug 2021 10:23:57 +0800 Subject: [PATCH] Lab 1 --- .gitignore | 6 ++ README.md | 1 + Week 2/README.md | 4 + Week 2/refresher_tasks/1_coins.c | 50 ++++++++++++ Week 2/refresher_tasks/2_external_function.c | 27 +++++++ Week 2/refresher_tasks/3_floatingpoint.c | 17 ++++ Week 2/refresher_tasks/4_1d_array.c | 39 ++++++++++ Week 2/refresher_tasks/5_2d_array.c | 64 +++++++++++++++ Week 2/rot13/rotate.c | 82 ++++++++++++++++++++ 9 files changed, 290 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 Week 2/README.md create mode 100644 Week 2/refresher_tasks/1_coins.c create mode 100644 Week 2/refresher_tasks/2_external_function.c create mode 100644 Week 2/refresher_tasks/3_floatingpoint.c create mode 100644 Week 2/refresher_tasks/4_1d_array.c create mode 100644 Week 2/refresher_tasks/5_2d_array.c create mode 100644 Week 2/rot13/rotate.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca8a6c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +* +!*.* +!*/ + +*.out +.vscode/ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f3286dc --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Peter's solutions for CITS2002 labs \ No newline at end of file diff --git a/Week 2/README.md b/Week 2/README.md new file mode 100644 index 0000000..49ae592 --- /dev/null +++ b/Week 2/README.md @@ -0,0 +1,4 @@ +# Week 2 + +Lab sheet: +http://web.archive.org/web/20210731041540/http://teaching.csse.uwa.edu.au/units/CITS2002/labsheets/labsheet1.php \ No newline at end of file diff --git a/Week 2/refresher_tasks/1_coins.c b/Week 2/refresher_tasks/1_coins.c new file mode 100644 index 0000000..0344bb0 --- /dev/null +++ b/Week 2/refresher_tasks/1_coins.c @@ -0,0 +1,50 @@ + +/* +* 1 [integer arithmetic, basic loop] When Australia first adopted decimal +* currency in 1966, coins had the denominations of 1c, 2c, 5c, 10c, 20c, and +* (a round!) 50c. If a person purchased something valued at $1 or less, and paid +* with a $1 note (yes), write a program to list the minimum number of coins, and +* their demoninations, that they should receive as change. +*/ + +#include +#include + +/* +* argument => cost in cents. +* Assume a value of less than 100c as per the question. +* Assume only the first argument has a price, else use the default example price +*/ +int main(int argc, char const *argv[]) +{ + int price = 53; + if (argc > 1) + { + price = 0; + int i = 0; + while (argv[1][i] != '\0') + { + int digit = argv[1][i]-'0'; + if (digit > 9 || digit < 0) + { + exit(EXIT_FAILURE); + } + price *= 10; + price += digit; + i++; + } + } + int remainder = 100 - price; + const int coinValues[] = {50, 20, 10, 5, 2, 1}; + printf("Change: "); + for (size_t i = 0; i < 6; i++) + { + int coinValue = coinValues[i]; + if (remainder / coinValue > 0) { + printf("%d %dc coins, ", remainder/coinValue, coinValue); + remainder -= coinValue*(remainder/coinValue); + } + } + printf("\n"); + exit(EXIT_SUCCESS); +} diff --git a/Week 2/refresher_tasks/2_external_function.c b/Week 2/refresher_tasks/2_external_function.c new file mode 100644 index 0000000..5c4951e --- /dev/null +++ b/Week 2/refresher_tasks/2_external_function.c @@ -0,0 +1,27 @@ +/* +* 2 [calling external function] Using your chosen programming language, write a +* short program to print out today's date and time +* (example output: Thu Jul 29 18:19:34 2021). The actual format is unimportant. +*/ + +// man strftime + +#include +#include +#include + +int main(void) +{ + char outstr[200]; + time_t t; + struct tm *tmp; + + t = time(NULL); + tmp = localtime(&t); + if (tmp == NULL) { + exit(EXIT_FAILURE); + } + strftime(outstr, sizeof(outstr), "%FT%T", tmp); + printf("%s\n", outstr); + exit(EXIT_SUCCESS); +} diff --git a/Week 2/refresher_tasks/3_floatingpoint.c b/Week 2/refresher_tasks/3_floatingpoint.c new file mode 100644 index 0000000..6b38fd7 --- /dev/null +++ b/Week 2/refresher_tasks/3_floatingpoint.c @@ -0,0 +1,17 @@ +/* +* 3 [floating-point numbers] In Lecture-2 we saw how some (in fact, most) +* floating-point values cannot be represented exactly on contemporary computers +* and, so, we must use them with great caution, or not at all. In your chosen +* programming language, repeat the exercise from Lecture-2, displaying +* sufficient decimal-points to 'expose' the problem. +*/ + +#include + +int main(void) +{ + float f = 1024.533; + printf("%.25f\n",f); + // Answer is 1024.5329589843750000000000000 due to imprecision. + return 0; +} diff --git a/Week 2/refresher_tasks/4_1d_array.c b/Week 2/refresher_tasks/4_1d_array.c new file mode 100644 index 0000000..4c10fc7 --- /dev/null +++ b/Week 2/refresher_tasks/4_1d_array.c @@ -0,0 +1,39 @@ +/* +* 4 [integer arithmetic, 1D array] Revise (or learn) how to generate a +* non-negative random integer in your chosen programming language. Saturday +* Lotto involves drawing 6 unique numbers, between 1 and 45. Write a simple +* program to choose and display your 6 lucky numbers. +*/ + +#include +#include +#include + +#define NUMBERS 6 +#define LOWER 1 +#define UPPER 45 +#define RANGE UPPER-LOWER+1 + + +int main(void) +{ + srand(time(NULL)); // Seed rng + int luckyNumbers[NUMBERS]; + for (size_t i = 0; i < NUMBERS; i++) + { + luckyNumbers[i] = rand() % RANGE + LOWER; + } + + // Could've printf in the last loop but seems like the exercise wants + // a 1D array. + + printf("Lucky numbers "); + for (size_t i = 0; i < NUMBERS; i++) + { + printf("%d ",luckyNumbers[i]); + } + printf("\n"); + + return 0; +} + diff --git a/Week 2/refresher_tasks/5_2d_array.c b/Week 2/refresher_tasks/5_2d_array.c new file mode 100644 index 0000000..8aaeb00 --- /dev/null +++ b/Week 2/refresher_tasks/5_2d_array.c @@ -0,0 +1,64 @@ +/* +* 🌶 [2D array, nested loop] Fill a 2D array of, say, 10x20 integers with +* non-negative random integers. Without using your language's builtin/standard +* sort() method/function, print out the second largest value in the array. +*/ +#include +#include +#include + + +#define X_LENGTH 10 +#define Y_LENGTH 20 + +// Testing +void iterArray(int arr[][Y_LENGTH]) { + int n = 0; + for (int i = 0; i < X_LENGTH; i++) + { + for (int j = 0; j < Y_LENGTH; j++) + { + arr[i][j] = n; + n++; + } + } +} + +void randomizeArray(int arr[][Y_LENGTH]) { + srand(time(NULL)); // Seed rng + for (int i = 0; i < X_LENGTH; i++) + { + for (int j = 0; j < Y_LENGTH; j++) + { + arr[i][j] = rand(); + } + } +} + +int twoLargest(int arr[][Y_LENGTH]) { + int secondLargest = 0; + int largest = 0; + for (int i = 0; i < X_LENGTH; i++) + { + for (int j = 0; j < Y_LENGTH; j++) + { + if (arr[i][j] > largest) + { + secondLargest = largest; + largest = arr[i][j]; + } + } + } + return secondLargest; +} + +int main(void) +{ + int arr[X_LENGTH][Y_LENGTH]; + // randomizeArray(arr); + iterArray(arr); + printf("Second largest : %d\n", twoLargest(arr)); + return 0; +} + + diff --git a/Week 2/rot13/rotate.c b/Week 2/rot13/rotate.c new file mode 100644 index 0000000..d2a5607 --- /dev/null +++ b/Week 2/rot13/rotate.c @@ -0,0 +1,82 @@ +#include // => printf() +#include // => exit(), EXIT_SUCCESS, EXIT_FAILURE +#include // strlen() +#include // isupper(), islower() + +#include + +// Compile this program with: +// cc -std=c11 -Wall -Werror -o rotate rotate.c + +// The rotate function returns the character ROT positions further along the +// alphabetic character sequence from c, or c if c is not lower-case + +char rotate(char c, int rot) +{ + // Check if c is lower-case or not + if(islower(c)) { + // The ciphered character is ROT positions beyond c, + // allowing for wrap-around + return ('a' + (c - 'a' + rot) % 26); + } else if (isupper(c)) + return ('A' + (c - 'A' + rot) % 26); + else { + return c; + } +} + +void rotateString(char string[], int rot) { + // Define a variable for a later loop + int i; + // Calculate the length of the first argument + int length = strlen(string); + + // Loop for every character in the text + for(i = 0; i < length; i++) { + // Determine and print the ciphered character + char c = string[i]; + printf("%d :\t%c => %c\n", i, c, rotate(c, rot)); + } + + // Print one final new-line character + printf("\n"); +} + +// Execution of the whole program begins at the main function + +int main(int argcount, char *argvalue[]) +{ + int rot = 13; + for (int i = 1; i < argcount; i++) + { + char *ptr; + int n = (int)strtol(argvalue[i], &ptr, 10); + if (*ptr == '\0') { + rot = n; + printf("Rotate by %d\n\n", rot); + } else { + // Try reading file + if (access(argvalue[i], F_OK) == 0) + { + printf("-- Open file %s --\n\n", argvalue[i]); + FILE* f = fopen(argvalue[i], "r"); + char c; + while ((c = getc(f)) != EOF) + { + putchar(rotate(c, rot)); + } + fclose(f); + } + + // Else interpret as string + printf("-- String %d --\n\n",i); + rotateString(argvalue[i], rot); + printf("-- End of string %d --\n\n",i); + } + + } + + // Exit indicating success + exit(EXIT_SUCCESS); + return 0; +}