SYSC2006 Lab7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SYSC 2006: Foundations of Imperative Programming

Lab 7 - C Structs: Pointers and Functions.


Strings
General Requirements
Your functions must not be recursive. Repeated actions must be implemented using C's while,
for or do-while loop structures.

None of the functions you write should perform console input; for example, contain scanf
statements. None of your functions should produce console output; for example, contain printf
statements.
You must format your C code so that it adheres to one of two commonly-used conventions for
indenting blocks of code and placing braces (K&R style or BSD/Allman style). Instructions were
given in the VS Code installation instructions.
Finish each exercise (i.e., write the function and verify that it passes all its tests) before you move
on to the next one.

Getting Started

Step 1: Depending on your operating system, download (lab7_win.zip or lab7_mac.zip) the


starting code from Brightspace and extract the zipped file.
Step 2: Open the unzipped folder and double-click on the workspace file. You are now ready to
start working in your lab.
Step 3: lab7_xxx folder contains the following files:
● struct_string.c contains incomplete definitions of several functions you have to design and
code.
● struct_string.h contains the declaration of the fraction_t struct, grade_t struct, and
the function prototypes for the functions you will implement. Do not modify
struct_string.h.
● main.c implements a test harness (functions that will test your code, and a main function
that calls these test functions). Do not modify main or any of the test functions.

Step 4: Run the project. It should build without any compilation or linking errors.
Step 5: Execute the project. For the functions where the test harness is provided (the functions in
main.c) will report several errors as it runs, which is what we would expect, because you have not
started working on the functions.
Step 6: Open struct_string.c in the editor. Do Exercises 1 through 4. Remember, you must not
make any changes to main.c or struct_string.h All the code you write must be in struct_string.c.

1
SYSC 2006: Foundations of Imperative Programming

Exercise 1
In this exercise, you are going to rewrite the functions that you wrote in Lab 6, so that all your
functions use pointers to fraction_t objects, i.e., call-by-reference, rather than passing structures by
value. Note that you will not update your Lab 6 files. You will write the new functions in Exercise
1 of your Lab 7 solution.
Copy your gcd function from Lab 6 to Lab 7. No changes are needed for this function.
The other functions in Lab 7 exercise 1, all need to be updated from your Lab 6 versions as per the
new signatures provided.
Build and execute the project. Use the console output to help you identify and correct any flaws.
Verify that the functions pass all of its tests before you start Exercise 2.

Exercise 2
In this exercise, we have an array of student data. Each element of the array is a student_t struct.
This struct contains an array of grade_t structs. Thus, we have an array of structs, where each struct
contains another array of structs.
Your task is to write two functions: update_gpa and calc_gpa. Each function has one input
parameter: a pointer to one of these structs.
The first one, update_gpa is very short and basically consists of calling calc_gpa for each student.
The function calc_gpa is a bit more complex. It calculates the GPA for each student. If you are not
familiar with how to calculate a GPA, here is the information: https://carleton.ca/npsia/program-
hub/how-to-calculate-your-gpa/.
Check that the output for Exercise 2 now includes the correct GPAs. In addition, double-check that
you have followed the instructions above and in the function descriptions.

Exercise 3
In this exercise, you are going to write the function count_char that takes two parameters: a string
and a char. The function returns how many times the char appears in the string.
For example,
count_char("Hello world ",'l') should return 3
count_char("Hello world ",'a') should return 0
count_char("Hello world ",'L') should return 0
Your function must use array-indexing notation to iterate over the characters of the string.

Exercise 4
In this exercise, you are going to rewrite the function count_char from exercise 3 to use a walking
pointer. The function has the following signature.
int count_char_wp(const char *, char);

2
SYSC 2006: Foundations of Imperative Programming
Wrap-up
Submit struct_string.c to Brightspace. Make sure it has been formatted to use K&R style or
BSD/Allman style, as explained in lab 3, Part 2, General Requirements.
Ensure that you submit the version of the file containing your solutions and not the unmodified file
that you downloaded from Brightspace!
Make sure that the code you submit can be opened, compiled, and run in the VS Code IDE.
Ensure that your code does not use any non-standard C extensions or libraries. If your code does
not compile and run, you will receive 0 for that submission.
We recommend that, after submitting source code files, you download the files from Brightspace
and verify that:
● the downloaded files have the correct filenames;
● the files can be opened and viewed in the IDE's editor;
● no syntax errors are reported when the code is compiled.
Once you have submitted your files, you must demo your work to the TAs for grades before
leaving your lab session.
Remember that your mark will be 0 if the TA did not check your work in the lab or your
submission was not on the system when the TA checked your work. No exceptions!
Note (applicable for all labs): It is your responsibility to keep track of the TA who graded
your work and to check that your grade is on Brightspace. If the grade is not on Brightspace
one day after you demo the work to the TA, contact the TA who graded your lab. Requests made
after the end of your next lab session will not be considered.
Solutions that are emailed to your instructor or a TA will not be graded, even if they are emailed
before the deadline.

You might also like