Skip to main content
SciNet
  • Home
  • All Courses
  • Calendar
  • Certificates
  • SciNet
    Main Site Documentation my.SciNet
  • CCDB
  • More
Close
Toggle search input
English
English Français
You are currently using guest access
Log in
SciNet
Home All Courses Calendar Certificates SciNet Collapse Expand
Main Site Documentation my.SciNet
CCDB
Expand all Collapse all
  1. PHY1610 - Winter 2026
  2. 2. Modularize Ants

2. Modularize Ants

Completion requirements
Opened: Thursday, 22 January 2026, 12:00 AM
Due: Thursday, 29 January 2026, 11:59 PM

In this assignment, you're given a C++ code called antsontable.cpp along with a Makefile and a README file.  You can get these by cloning the following repo on the teach cluster:

git clone /home/l/lcl_uotphy1610/lcl_uotphy1610s1466/antsontable

This code simulates ants walking on a table. The table is subdivided into squares. A number of ants is initially put on the table, divided as evenly as possible over the squares. Ants can move to a neighbouring square or stay put at every time step. How many of a square's ants move in each of the 9 different directions is random (using the C++ random library). To be precise, the number of ants in each square is randomly partitioned into 9 sets corresponding to the 9 possible moves (up-left, up, up-right, down-left, down, down-right, left, right, stay). Ants can fall of the edges of the table. The program prints the number of ants left on the table after every time step, as well as the minimum number of ants on a square and the maximum. It also writes the number of ants on each square at every timestep to a file called ants.txt.

The addition files are:

  • teachsetup: a script to load all necessary modules on the teach cluster (gcc and rarray)
  • README.md: which explains what the code is about and how to get it to work
  • Makefile: to help compile the code and run it.

You should:

  • Compile and run the code.
  • Save the original output, to check against as you change the code.
  • Modularize this code. There should at least be separate modules for
    • Initialization of the ants
    • The rand_partitioning function
    • The single time step (i.e., the code block following the loop body of the for(int timestep...) loop, but excluding that for statement)
    • Output, i.e., reporting the number of ants to screen and writing to ants.txt
    The fiie antsontable.cpp with the main function is then essentially reduced to just a 'driver' that puts it altogether.

    Please recall that a proper "modularization" implies separating your functions into separate files, i.e., headers with declarations and preprocessor guards and any necessary include statements, and source files with the implementation of the functions.

  • Adapt the existing Makefile to be able to compile this modular code.
Ensure the code remains clearly commented and documented.
Use git version control as you are modularizing the code; you probably want to do one module at a time.

 

To submit your assignment, you should pack up your repository into a zip file using the "git2zip" command that is available on the Teach cluster.  To use this command, simply type "git2zip" on the command line from the directory which contains your git repo (See https://education.scinet.utoronto.ca/mod/page/view.php?id=4284for more info). Then copy the resulting .zip file over to your computer and submit it here.

The deadline for the assignment is January 29, 2026 at 23:59 PM. The late policy can be found in the syllabus. Do NOT submit the output of your program (it's too large).

A clarification: You may have tried to move the declaration of the 'moves' array to a global namespace and noticed that its content then does not get set properly. This is due to a bug/feature/undefined behaviour of rarray in this context. Rather than waiting for the bug to be fixed, a work- around is to create the moves array in the following way:

const rarray<std::pair<int,int>,1> moves = ra::make_rarray<std::pair<int,int>>({{-1,-1},{-1,0},{-1,1}, { 0,-1},{ 0,0},{ 0,1}, { 1,-1},{ 1,0},{ 1,1}}); 

Semantically, this does what the original was supposed to do.


You are currently using guest access (Log in)
Data retention summary


All content on this website is made available under the Creative Commons Attribution 4.0 International licence, with the exception of all videos which are released under the Creative Commons Attribution-NoDerivatives 4.0 International licence.
Powered by Moodle