Home CPSC 305

Structure Exercise

Objective

To get practice writing C programs, including structs.

Task

For this lab, you will create a simple structure and a few functions to operate on it. The structure will represent a high score in a video game and will contain two fields:

You should write three functions which work on these structures:


Sample Run

You should test your structure and functions with the following main function:


int main() {
    /* create an array of scores */
    Score scores[10];

    /* put in some test data */
    score_set(&scores[0], "IAN", 750);
    score_set(&scores[1], "BOB", 1200);
    score_set(&scores[2], "ADA", 3500);
    score_set(&scores[3], "SUE", 900);
    score_set(&scores[4], "EVA", 500);
    score_set(&scores[5], "BEN", 1500);
    score_set(&scores[6], "ROY", 3000);
    score_set(&scores[7], "KIM", 1250);
    score_set(&scores[8], "VIC", 2500);
    score_set(&scores[9], "DAN", 1800);

    /* sort them using the compare function above */
    qsort(scores, 10, sizeof(Score), 
        (int (*) (const void*, const void*)) &score_compare);

    /* display them */
    int i;
    for (i = 0; i < 10; i++) {
        score_print(&scores[i]);
    }

    return 0;
}

This program would output the following:

ADA 3500
ROY 3000
VIC 2500
DAN 1800
BEN 1500
KIM 1250
BOB 1200
SUE 900
IAN 750
EVA 500

Submitting

When your program works, email the code to ifinlay@umw.edu.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.