CITS2002 Systems Programming | |
|
Solutions for Labsheet 2 - for the week commencing 7th August 2024This page provides some sample solutions and discussion on some of the tasks of Labsheet-2.Exercises
// (a multiple of 400) OR ((a multiple of 4) AND (NOT a multiple of 100))
if((year%400 == 0) || (((year%4 == 0) && (year%100 != 0))) {
printf("YES\n");
}
else {
printf("NO\n");
}
The solution to this exercise can directly use the solutions from task 1 and Workshop-2. Only in February do we need to calculate whether we're in a leap year or not, and, if so, to set the number of days in the month to 29 instead of 28.
switch (month) {
case 1: printf(" January %i\n", year);
daysinmonth = 31;
break;
case 2: printf(" February %i\n", year);
if(is_leap_year(year)) {
daysinmonth = 29;
}
else {
daysinmonth = 28;
}
break;
.....
The solution to this exercise forms a very standard "pattern" of writing our introductory programs - the main() function should check the number of command-line arguments; if incorrect, a helpful error message should be printed, and the program should exit indicating failure; otherwise, the rest of rest of main() processes the command-line arguments. This program's command-line arguments are strings of characters, and each needs to be converted to an integer value using the standard atoi() function. We first assume that the first value is the maximum value, and then check to see if the second, and then the third, are greater. Note how we've had to use a fixed number of if tests to find the maximum of all values:
#include <stdio.h>
#include <stdlib.h>
int main(int argcount, char *argv[])
{
// CHECK THE NUMBER OF COMMAND-LINE ARGUMENTS
if(argcount != 4) {
fprintf(stderr, "Usage: %s value1 value2 value3\n", argv[0]);
exit(EXIT_FAILURE); // Exit indicating failure
}
// CORRECT NUMBER OF COMMAND-LINE ARGUMENTS, PROCESS THEIR VALUES
else {
// CONVERT EACH COMMAND-LINE ARGUMENT TO AN INTEGER VALUE
int value1 = atoi(argv[1]);
int value2 = atoi(argv[2]);
int value3 = atoi(argv[3]);
// ASSUME THAT THE FIRST ARGUMENT PROVIDES THE MAXIMUM VALUE
int maximum = value1;
// COMPARE OUR ASSUMED MAXIMUM AGAINST THE OTHER TWO VALUES
if(maximum < value2) {
maximum = value2;
}
if(maximum < value3) {
maximum = value3;
}
// PRINT THE MAXIMUM VALUE FOUND
printf("maximum is %i\n", maximum);
exit(EXIT_SUCCESS); // Exit indicating success
}
return 0;
}
....
// CHECK THE NUMBER OF COMMAND-LINE ARGUMENTS
if(argcount < 2) {
fprintf(stderr, "Usage: %s value1 [value2 ...]\n", argv[0]);
exit(EXIT_FAILURE); // Exit indicating failure
}
else {
// ASSUME THAT THE FIRST ARGUMENT PROVIDES THE MAXIMUM VALUE
int maximum = atoi(argv[1]);
// LOOP OVER ANY REMAINING ARGUMENTS (THERE MAY BE NONE!)
for(int a = 2 ; a < argcount ; a = a + 1) {
int thisvalue = atoi(argv[a]);
// COMPARE OUR ASSUMED MAXIMUM AGAINST ANY OTHER VALUES
if(maximum < thisvalue) {
maximum = thisvalue;
}
}
// PRINT THE MAXIMUM VALUE FOUND
....
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// THE FUNCTION RECEIVES AN ARRAY OF CHARACTERS, WITHOUT SPECIFYING ITS LENGTH
bool Luhn(char creditcard[])
{
int s1 = 0;
int s2 = 0;
bool odd = true;
// ITERATE OVER THE STRING BACKWARDS
for(int i = strlen(creditcard)-1 ; i >= 0 ; i = i-1) {
char digit = creditcard[i] - '0';
if(odd) {
s1 += digit;
}
else {
int mult = digit*2;
s2 += (mult/10) + (mult%10);
}
odd = !odd;
}
return ((s1+s2) % 10) == 0;
}
int main(int argcount, char *argv[])
{
// ITERATE OVER EACH COMMAND-LINE ARGUMENT
for(int a=1 ; a<argcount ; ++a) {
if(Luhn(argv[a])) {
printf("%16s\t%s\n", argv[a], "OK");
}
else {
printf("%16s\t%s\n", argv[a], "not OK");
}
}
return 0;
}
void ordinal(int i)
{
int lastdigit = i % 10; // keep the remainder after dividing by 10
if(lastdigit == 1 && i != 11) {
printf("%ist\n", i);
} else if(lastdigit == 2 && i != 12) {
printf("%ind\n", i);
} else if(lastdigit == 3 && i != 13) {
printf("%ird\n", i);
} else {
printf("%ith\n", i);
}
}
int main(int argcount, char *argv[])
{
for(int a=1 ; a<argcount ; a = a+1) {
ordinal( atoi(argv[a]) );
}
return 0;
}
// Q7a)
for(int row = 1 ; row <= 5 ; ++row) {
for(int col = 1 ; col <= row ; col = col+1) {
printf("*");
}
printf("\n");
}
// Q7b)
for(int row = 1 ; row <= 5 ; ++row) {
for(int col = 1 ; col <= (5-row) ; col = col+1) {
printf(" ");
}
for(int col = 1 ; col <= row ; col = col+1) {
printf("*");
}
printf("\n");
}
// Q7c)
for(int row = 5 ; row >= 1 ; --row) {
for(int col = 1 ; col <= row ; col = col+1) {
printf("*");
}
printf("\n");
}
// Q7d)
for(int row = 1 ; row <= 5 ; ++row) {
for(int col = 1 ; col <= 5-row ; col = col+1) {
printf(" ");
}
for(int col = 1 ; col <= row*2 - 1 ; col = col+1) {
printf("*");
}
printf("\n");
}
|