This commit is contained in:
Peter 2021-08-06 10:23:57 +08:00
commit 909bef9c7e
9 changed files with 290 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*
!*.*
!*/
*.out
.vscode/

1
README.md Normal file
View File

@ -0,0 +1 @@
# Peter's solutions for CITS2002 labs

4
Week 2/README.md Normal file
View File

@ -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

View File

@ -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 <stdlib.h>
#include <stdio.h>
/*
* 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);
}

View File

@ -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 <time.h>
#include <stdio.h>
#include <stdlib.h>
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);
}

View File

@ -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 <stdio.h>
int main(void)
{
float f = 1024.533;
printf("%.25f\n",f);
// Answer is 1024.5329589843750000000000000 due to imprecision.
return 0;
}

View File

@ -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 <time.h>
#include <stdlib.h>
#include <stdio.h>
#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;
}

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <time.h>
#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;
}

82
Week 2/rot13/rotate.c Normal file
View File

@ -0,0 +1,82 @@
#include <stdio.h> // => printf()
#include <stdlib.h> // => exit(), EXIT_SUCCESS, EXIT_FAILURE
#include <string.h> // strlen()
#include <ctype.h> // isupper(), islower()
#include <unistd.h>
// 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;
}