Finish Functions¶

show_results¶

At the end of the code, if we are choosing to show results, we print out the final counts of susceptible, infected, immune, and dead people. We also print the actual contagiousness and actual deadliness of the disease. To perform these two calculations, we use the following code (using the contagiousness as the example):

```		100.0 * (stats->num_infections / (stats->num_infection_attempts
== 0 ? 1 : stats->num_infection_attempts)),
100.0 * (stats->num_deaths / (stats->num_recovery_attempts
== 0 ? 1 : stats->num_recovery_attempts)));
```

In this code, the ternary operators (? and :) are used to return one value if something is true and another value if it isn’t. The thing we are checking for truth is num_infection_attempts == 0. If this is true, i.e. if we didn’t attempt any infection attempts at all, then we say there was actually 1 infection attempt (this is to avoid a divide by zero error). Otherwise, we return the actual number of infection attempts. This value becomes the dividend for num_infections; in other words, we divide the number of actual infections by the number of total infections. This will give us a value less than 1, so we multiply it by 100 to obtain the actual contagiousness factor of the disease. A similar procedure is performed to calculate the actual deadliness factor.

cleanup¶

If X display is enabled, then Rank 0 destroys the X Window and closes the display

```    close_display(dpy);
```

Since we allocated arrays dynamically, we need to release them back to the heap using the free function. We do this in the reverse order than we used malloc, so environment will come first and x_locations will come last.

```    // free text display environment
#ifdef TEXT_DISPLAY
int current_location_x;
for(current_location_x = constant->environment_width - 1;
current_location_x >= 0; current_location_x--)
{
free(dpy->environment[current_location_x]);
}
free(dpy->environment);
#endif

// free arrays allocated in global struct
free(global->x_locations);
free(global->y_locations);
free(global->infected_y_locations);
free(global->infected_x_locations);
free(global->states);
free(global->num_days_infected);
```