Advanced C Programming

Autumn 2015 :: ECE 264 :: Purdue University

This is for Fall 2015 (2 years ago)
Due 10/7

Structs

Goals

This assignment has the following objectives:
  1. Learn how use structures for data encapsulation
  2. Practice programming with dynamic memory (i.e., malloc)
  3. Practice debugging memory errors
  4. Learn how use structures for data encapsulation
  5. Practice putting a semicolon (";") after your struct statement :)

Overview

Scenario: Your team will soon choose a leader. You will be given a list of names and a separate list of each person's "favorite" person on the team. You must create software that can calculate the new leader.

Prepare

The following will help you get started:

  1. Write some tiny programs using the following, to make sure you understand each completely. Do not simply read existing examples. If you see something online that you want to try, type it instead of copy-pasting it in.
    1. struct declarations
    2. address of struct
    3. operators for accessing struct members: "." and "->"
    4. malloc() and free()
    5. operators for working with addresses: "*" and "&"
  2. Read the Code Quality Standards. Note the changes with regard to struct initialization, braces for blocks, and multiple statements on one line.
  3. As always… read this assignment description carefully.

There are no starter files.

Doing the assignment

Here is the overview of what you are creating.

Note: Type the declarations into your file manually. Do not copy-paste. (You learn more from typing code manually.)

When you are finished, it will look like this.

initial state of project files
The test shown in the screenshot is not adequate by itself. Your test must be your own, and must be more thorough. Your test should cover edge cases that this doesn't cover (e.g., tie, empty team, all choose same person).

Requirements

  1. Your submission must contain the following files: team.c, team.h, and teamtest.c, with the functions and definitions as shown in the screenshot.
  2. Code must meet the code quality standards, including the new requirements regarding struct initialization, braces for if/for/while/do/switch blocks, and multiple statements.
  3. Do not use dynamic arrays (C99).
  4. Do not use nested for loops.This requirement has been eliminated.
  5. Your team.c may not include any header file except stdlib.h
  6. Your team.c may not call any function or refer to any external symbol except malloc and free.
  7. Your code may not make any assumptions about the size of any type.
  8. Code must compile without warnings or errors on ecegrid using the installed version of gcc installed there and the flags given in the provided .bashrc file (-std=c99 -pedantic -g -Wall -Wshadow).
  9. Code must run without crashing, for your test file and for any other valid test file.
  10. Your teamtest.c must be your own. The one shown is inadequate by itself. You must test the entire functionality of your code.
  11. If there is a tie in pick_leader, pick the one with the smallest index.
  12. A person may be their own favorite.
  13. Your code must work with the example code shown in the screenshot. You are welcome and encouraged to test with it, but give it a different name (e.g., example_teamtest.c) and do not copy-paste from that into your own teamtest.c.
  14. In create_team, you may assume that names and favorites are both valid arrays of size population.

How much work is this?

This assignment is difficult. It won't require a lot of code, but expect to spend some time debugging and understanding the concepts.

Updates

10/2: Added topics to practice, more description of files to be turned in, requirements 12-14; added reminder to requirement #2 about new code standards; fixed typo in screenshot (create_team(4, …))

10/3: Correction: favorite field of struct Person should be the address of another struct Person object

10/5: Eliminated requirement #4 ("Do not use for loops").

10/6: Clarified role of free_team(…).