Assessment Tool

Lecture 20:  Nested Data Structures

Content Tested:  Writing functions using nested data structures

Lecture Content:

Goals:

Assessment Technique:  Design Activity

Purpose:

Instructors can find out if students can manipulate nested data structures.

Activity:

Now that you have been programming for a few weeks, you decide that you should create a program to store the addresses of your friends.  You have already completed the data structure design which is listed below.  You want to keep track of the address information plus your friends' birthdays.

#define ADD_BOOK_SIZE 100
#define MAX_NAME 20
#define MAX_STREET 50
#define MAX_CITY 25
#define MAX_EMAIL 30

typedef struct{
  int month;           /* 1 - 12 representing month */
  int day;             /* 1 - 31 representing day */
  int year;
} date;

typedef struct{
  char first_name[MAX_NAME + 1];
  char last_name[MAX_NAME + 1];
  date birthday;
  char street[MAX_STREET + 1];
  char city[MAX_CITY + 1];
  char state[3];                 /* use postal code */
  int  zip[9];                   /* standard US zip code */
  int  phone[10];                /* phone numbers have 10 digits */
  char email[MAX_EMAIL + 1];
} address;

1.  Your job now is the write some useful functions for your address book program.  The first thing you want to do is print out an address in postal code form.  Here is the prototype of the function:

void print_address (address add);

Please complete the function, bearing in mind that an address written is postal form is like the following:
Ann Smith
123 Main St.
Sunshine, FL 12345-6789
 
 
 
 
 
 
 
 
 
 
 

2.  Now you want to filter some addresses and only print addresses for a certain state.  (You are having a birthday party and want to invite your friends that live in your state).  This function takes an array of addresses, the number of addresses in the array, and the state that you want to use for filtering.

Here's the prototype:

void print_address_w_state (address add[], int num, char st[3]);
 
 
 
 
 
 
 
 
 
 
 
 
 

3.  You also included birthdays in your address book.  In order to be reminded to send your friends birthday cards, you write a function that lists the names along with the birthdays that have a birthday in a given month.  A listing should look like this:
Charles Johnson birthday: 6/1
Ann Smith birthday: 6/20
Bob Jones birthday: 6/4

Here's the function prototype:
void print_name_birthdays (address add[], int num, int b_month);
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Possible Solutions:

1.

void print_address (address add){

  int i;

  printf("%s ", add.first_name);
  printf("%s\n", add.last_name);
  printf("%s\n", add.street);
  printf("%s, %s ", add.city, add.state);
  for(i = 0; i < 5; i++){
    printf("%d", add.zip[i]);
  }
  printf("-");
  for(i=5; i < 9; i++){
    printf("%d", add.zip[i]);
  }
  printf("\n");
}

2.

void print_address_w_state (address add[], int num, char st[3]){

  int i;
  address current;

  for (i = 0; i < num; i++){
    current = add[i];

    if(strcmp(current.state, st) == 0){
      print_address(current);
    }
  }
}

3.

void print_name_birthdays (address add[], int num, int b_month){

  int i;
  address current;

  for (i = 0; i < num; i++){
    current = add[i];
    if (current.birthday.month == b_month){
      printf("%s %s birthday: %d/%d\n", current.first_name, current.last_name,
      current.birthday.month, current.birthday.day);
    }
  }
}

Possible Uses of Activity: