This is a cache of https://vccplus.brianholman.com/manuals/programming-in-c/cargo/. It is a snapshot of the page as it appeared on 2026-11-21T00:01:23.897+0000.
Count cargo for two worlds · Programming in C on the CoCo 3 — CMOC and Its APIs · VCC Plus
← Manual LibraryVCC Plus 3.7.1 · Reading on the web

Try the exercises in VCC Plus. Example links download files here; open them in the app to work beside the emulator.

The complete book · PDF editionBook 6: Programming in C on the CoCo 3 — CMOC and Its APIs

198 pages · VCC Plus 3.7.1 · Keep a copy to read or print.

Download PDF
Contents

Chapter 3 of 9

Count cargo for two worlds

Earth ordered twelve crates with twenty meals in each. Mars ordered eight crates with fifteen meals in each. The ship has one manifest and two stops to make. How many meals should the cook set aside before departure?

Load a copy of the manifest

Choose New → Hello World in C Development and create a separate project named CargoCount. Save the Cargo Count source in your practice folder. Open that saved source in a text editor and copy its contents. In the project's main.c editor select all the starter text and replace it with the Cargo Count source. Keep the name main.c because the project's Makefile builds that name. Choose Save All, leave Debug selected, then Build and Run. This program needs no floppy and asks for no keys.

The real guest has printed EARTH: 240 MEALS, MARS: 120 MEALS and TOTAL: 360 MEALS, then returned to OK.

Read one row as a shipment

struct Shipment
{
    const char *destination;
    byte crates;
    byte mealsPerCrate;
};

static const struct Shipment manifest[] =
{
    { "EARTH", 12, 20 },
    { "MARS", 8, 15 }
};

A structure groups different pieces of information about one thing. A Shipment has a destination, a count of crates and a count of meals per crate, in that order. Each initializer provides those three values. The double quotes make each destination a string ending with a zero byte. The const char * member holds its address so the program can find the letters to print.

The square brackets in manifest[] make an array of shipments. CMOC counts the two initializer rows to determine its length. Index zero is Earth and index one is Mars. A byte in this program is an unsigned 8-bit value, large enough for 0 through 255. Each crate count fits. An unsigned word has 16 bits and can hold 0 through 65,535. That's the type chosen for a shipment's meal count and the ship's running total. Even though 240 and 120 fit in single bytes, their sum 360 exceeds 255.

Work the loop by hand

int main(void)
{
    word total = 0;
    byte row;

    width(40);
    for (row = 0; row < sizeof(manifest) / sizeof(manifest[0]); ++row)
    {
        word meals = mealsFor(&manifest[row]);
        printf("%s: %u MEALS\n", manifest[row].destination, meals);
        total += meals;
    }
    printf("TOTAL: %u MEALS\n", total);
    return 0;

Read the three parts inside for in order. Start with row = 0. Check that the index is less than the number of shipments. After processing a row, ++row advances to the next one and the check happens again. The expression sizeof(manifest) / sizeof(manifest[0]) divides the storage of the entire array by the storage of one element. It works here because the array itself is in scope. Using the size of a pointer in its place would answer a different question.

For the first pass, mealsFor gives back 240. The two placeholders in printf use the two arguments after the format string. %s follows the destination string to its zero terminator. %u prints the meal count as an unsigned decimal number. The shortcut total += meals adds this shipment to the running count. Fill in the second row before looking at the screen.

Row Destination Crates × meals per crate Total after this row
0 EARTH 12 × 20 = 240 240
1 MARS 8 × 15 = 120 360

Send one more crate

Change 8 to 9 in the Mars initializer, and nothing else. Predict the two destination counts and the ship's total before you choose Save All → Build → Run. Earth still needs 240. Mars now needs 135, so the final count is 375. The addition to the grand total was exactly one crate's worth. If your screen still says 360, check which copy of main.c you edited and that the build status says current.

Set Mars back to 8, save, and build in Debug again. Keep this project open. You are about to look at one delivery before the program prints it.

Original VCC Plus manuals © 2026 Brian K. Holman. Third-party references retain their authors’ copyrights and licenses. Artwork notices. Edition 3.7.1; source and export identity. Printable edition: PDF source and export identity.

A closer look

Open original