This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting, and submit on Gradescope to check your solution. You are encouraged to work at home to complete what you do not get through today.
Reinforces concepts learned in today's meeting:
- Use gdb for debugging code.
- Use gdb to analyze the code and correct the code so that it performs correctly.
- Use 2-dimensional arrays.
Part 1
Pull the starter code for this exercise from the public repo by taking the following steps:
-
Log into an undergraduate cluster computer. Update the course public repo with a
git pull
command. -
Confirm that you can see the template files for today’s exercise by typing
ls exercises/ex09
– you should see a file namedtranspose.c
. -
Navigate to your personal repo for the course and make a directory for today’s exercise. Copy
transpose.c
from the public class repository (in the /exercises/ex09/ directory) into your personal repo’s directory for this exercise.
Part 2
Open transpose.c
with a text editor. The purpose of this program is to transpose a small array, meaning that values in each row of the original become the values in the corresponding column in the transposed version.
To compile the code, use this command:
gcc -std=c99 -pedantic -Wall -Wextra transpose.c -g -o transpose
or use our alias:
gccc transpose.c -g -o transpose
Note the use of -g
, which enables debugging. To run the program using gdb:
gdb ./transpose
Use gdb to step through the code (placing breakpoints as necessary), and print out the variable values, to determine why the code is not printing all of the 2nd table, or transposing correctly. Given what you discovered using gdb, fix the code so that it behaves correctly.
Remember to add and commit to your local repo copy as your work. Push to your remote repo when finished. Also scp and submit transpose.c
to Gradescope to check your solution. Use exit
to logout from your ugrad account when finished. If you continue to work on the program after class, make sure to keep your repo updated as well!