Exercise 10
Info

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, and ask questions on Piazza or in office hours.

Learning Objectives

Reinforces concepts learned in today's meeting:

  • Declare and initialize pointer variables
  • Use pointers to pass by reference

Part 1

Pull the starter code for this exercise from the public repo by taking the following steps:

  1. Log into an undergraduate cluster computer. Update the course public repo with a git pull command.

  2. Confirm that you can see the template files for today’s exercise by typing ls exercises/ex10 – you should see a file named passing.c.

  3. Navigate to your personal repo for the course and make a directory for today’s exercise. Copy passing.c from the public class repository (in the /exercises/ex10/ directory) into your personal repo’s directory for this exercise.

Part 2

In this part, you will be working on passing.c. You are going to implement a getDate function that prompts for and collects input that is a date in MM/DD/YYYY format. You will practice how to pass variables by pointers and learn when you should pass by pointers instead of by value. Open passing.c with a text editor. [NOTE: it will not compile until you complete many of the tasks below.] You will need to modify passing.c as follows.

First, add a declaration and definition for a function called getDate. The purpose of the function is to prompt for and collect input that is a date in MM/DD/YYYY form. The function should use three parameters to pass the input values back to the main calling function as integers, storing them in the provided variables. It should also return the number of values that were successfully read by the function. Your main job in this exercise is to figure out how to declare and use the parameters for this function, and how to call it from main.

TODO comments indicate where to add the declaration and definition.

Part 3

After you have implemented the getDate function, you should complete the code in the main function. You will need to declare and initialize an array of month names, called months.

Next, fix the code at the point of the TODO comment in the while loop control so that it calls the getDate function, using the provided variables mon, day and yr.

Once you’ve completed the main function, you should be able to run the program. You should run it with at least three test cases for successful date conversions and three test cases for unsuccessful inputs. Make sure that all of your tests pass.

Reminder

Remember to add and commit to your local repo copy as your work. Push to your remote repo when finished. Also scp and submit passing.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!