Well done everyone for such a great effort in the first assignment.
Take a moment to think about how you found the assignment, and what you learned in the process of working on it.
If you could go back in time to the very start of the assignment, and give one piece of advice to your past self, what would you say?
Your tutor has asked a lab pair to present their week 7 work.
Discuss the good, the bad and the ugly aspects of their code.
Please be gentle in any criticism - we are all learning!
The information should include the pet's name, type of animal, age and weight.
Create a variable of this type and assign information to it to represent an axolotl named "Fluffy" of age 7 that weighs 300grams.
void age_fluffy(struct pet *my_pet);
eg: If fluffy goes from age 7 to age 8, it should end up weighing 8/7 times the amount it weighed before. You can store the weight as an int and ignore any fractions.
Show how this function can be called by passing the address of a struct variable to the function.
//
// Starting code for COMP1511 lab exercises
//
#include <stdio.h>
#include <string.h>
#define MAX_SPECIES_NAME_LENGTH 128
#define MAX_SIGHTINGS 10000
// a struct to represent the date
// a whale pod sighting was made
struct date {
int year;
int month;
int day;
};
// a struct to represent a sighting
// of a pod (group) of whales
struct pod {
struct date when;
int how_many;
char species[MAX_SPECIES_NAME_LENGTH];
};
int read_sightings_file(char filename[], int len, struct pod sightings[len]);
int read_sighting(FILE *f, struct pod *w);
int read_date(FILE *f, struct date *d);
int count_orca_sightings(int n_sightings, struct pod sightings[n_sightings]);
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <file>\n", argv[0]);
return 1;
}
struct pod whale_sightings[MAX_SIGHTINGS];
int n_sightings = read_sightings_file(argv[1], MAX_SIGHTINGS, whale_sightings);
if (n_sightings > 0) {
int n_orca_pods = count_orca_sightings(n_sightings, whale_sightings);
printf("%d Orca sightings in %s\n", n_orca_pods, argv[1]);
}
return 0;
}
// return the number of sightings of Orca
int count_orca_sightings(int n_sightings, struct pod sightings[n_sightings]) {
// REPLACE THIS COMMENT WITH YOUR CODE
// THIS FUNCTION SHOULD NOT CALL SCANF OR PRINTF
// IT SHOULD JUST RETURN A VALUE
return 42; // CHANGE ME
}
//
// DO NOT CHANGE THE FUNCTIONS BELOW HERE
//
// return number of sightings read from filename
// -1 is returned if there is an error
int read_sightings_file(char filename[], int len, struct pod sightings[len]) {
FILE *f = fopen(filename, "r");
if (f == NULL) {
fprintf(stderr,"error: file '%s' can not open\n", filename);
return -1;
}
int n_sightings = 0;
while (read_sighting(f, &sightings[n_sightings]) == 1 && n_sightings < len) {
n_sightings = n_sightings + 1;
}
fclose(f);
return n_sightings;
}
// return 1 if a sighting can be read, 0 otherwise
int read_sighting(FILE *f, struct pod *s) {
if (read_date(f, &(s->when)) != 1) {
return 0;
}
if (fscanf(f, "%d", &(s->how_many)) != 1) {
return 0;
}
fgetc(f);
if (fgets(s->species, MAX_SPECIES_NAME_LENGTH, f) == NULL) {
return 0;
}
// finish string at '\n' if there is one
char *newline_ptr = strchr(s->species, '\n');
if (newline_ptr != NULL) {
*newline_ptr = '\0';
}
// also finish string at '\r' if there is one - files from Windows will
newline_ptr = strchr(s->species, '\r');
if (newline_ptr != NULL) {
*newline_ptr = '\0';
}
return 1;
}
// return 1 if a date can be read, 0 otherwise
int read_date(FILE *f, struct date *d) {
int n_scanned = fscanf(f, "%d/%d/%d", &(d->year), &(d->month), &(d->day));
return n_scanned == 3;
}
Note the C library functions like fopen, fscanf,
fclose
which are used to read and write files haven't been covered lectures.
We'll squeeze them if we have time but they definitely won't be needed for the exam.
Your tutor will explain basically what they do in the supplied code for this week's lab exercise but you don't need to change the code that calls these functions.
Your tutor may still choose to cover some of the questions time permitting.
#include <stdio.h> int main(void) { char str[10]; str[0] = 'H'; str[1] = 'i'; printf("%s", str); return 0; }
int n; int *p, *q;What will happen when each of the following statements is executed (in order)?
p = &n; *p = 5; *q = 17; q = p; *q = 8;
#include <stdio.h>
#define MAX_PLATE 10000
struct parking_fine {
double amount;
char number_plate[MAX_PLATE];
};
int read_parking_fine(struct parking_fine);
int main(void) {
struct parking_fine f;
if (read_parking_fine(f)) {
printf("%lf %s\n", f.amount, f.number_plate);
}
return 0;
}
// return 1 if a parking fine is successfully was read, 0 otherwise
int read_parking_fine(struct parking_fine fine) {
if (scanf("%lf", &(fine.amount)) != 1) {
return 0;
}
return fgets(fine.number_plate, MAX_PLATE, stdin) != NULL;
}
Fix it.
strip_comments.c
which reads lines from
its input and prints them after removing any C // style comments. In
another words if the line contains // it does not print the // or
anything after it.
The only functions you can use are fgets
and
printf
.
You can assume lines contain at most 4096 characters.
For example:
./strip_comments x = x + 1; // This means add one to the variable x x = x + 1;Also - is that a good comment to add to a C program?
filter_empty_lines.c
which reads lines
from its input and prints them only if they contain a
non-white-space-character.
In another words remove lines are empty or contain only white-space.
The only functions you can use are fgets
and
printf
.
You can assume lines contain at most 4096 characters.
You can assume there are only 3 white space characters, space, tab & new-line.
For example:
./filter_empty_lines full line full line another no-empty line another no-empty line
reverse.c
which reads lines and writes
them out with the characters of each line in reverse order. It should
stop when it reaches the end of input.
For example:
./reverse The quick brown fox jumped over the lazy dog. .god yzal eht revo depmuj xof nworb kciuq ehT It was the best of times. It was the worst of times. .semit fo tsrow eht saw tI .semit fo tseb eht saw tI This is the last line. .enil tsal eht si sihT
int myStrlen(char *string);
int myStrCmp(char *string1, char *string2);
int beginsWith(char *string1, char *string2);
int isSubstring(char *substring, char *string);