From: marwk@levels.unisa.edu.au
Newsgroups: unisa.cis.prog.c.05286
Subject: PRACTICAL 8: Answer
Date: 6 Nov 92 02:35:09 +1030
Organization: University of South Australia

PRACTICAL 8: Answers.

Marks:

Prototypes in files  5
main()               5
read()               8
write()              5
sort()               7
get_loc()            5
search()             5
                    ==
                    35
                    --

/* P8.C

It stops when a blank name is entered.  Use gets() to get the name in so that
blanks can be easily read in.

Use 5 source files for this, separating the main, reading in, writing out,
searching and sorting routines into separate source files.
*/

#include <stdio.h>
#include <string.h>

#include "p8.h"

#include "read.h"
#include "write.h"
#include "sort.h"
#include "search.h"

void main(void)
{
    char names[MAX_NAMES][MAX_LENGTH + 1];
    int  ages [MAX_NAMES];

    int n;
    char name[MAX_LENGTH + 1];

    read_data(  names, ages, &n);
    sort_data(  names, ages, n);
    write_data( names, ages, n);
    search_data(names, ages, n);
}

********************************************************************************

/* READ.H */

#include "p8.h"
void read_data(char names[][MAX_LENGTH + 1], int ages[], int* n);

********************************************************************************

/* READ.C */

#include <stdio.h>
#include "read.h"

void read_data(char names[][MAX_LENGTH + 1], int ages[], int* n)
{
    char* ifname = "p8.dat";
    FILE* ifp;
    char buffer[101];  /* more than enough room */
    int i;

    if ((ifp = fopen(ifname, "r")) == NULL)
    {
        fprintf(stderr, "Error: the file %s is not available!\n", ifname);
        exit(1);
    }

    i = 0;

    while (fgets(buffer, 101, ifp) != NULL)
    {
        /* note that students are expected to have used fixed-length fields
           in their data so it is easy to extract the names and ages */

        strncpy(names[i], buffer, MAX_LENGTH);
        sscanf(buffer + strlen(names[i]), "%d", &ages[i]);

        ++i;
    }

    fclose(ifp);

    *n = i;
}

********************************************************************************

/* WRITE.H */

#include "p8.h"
void write_data(char names[][MAX_LENGTH + 1], int ages[], int n);

********************************************************************************

/* WRITE.C */

#include <stdio.h>
#include "write.h"

void write_data(char names[][MAX_LENGTH + 1], int ages[], int n)
{
    FILE* ofp;
    int i;

    ofp = fopen("p8.new", "w");

    for (i = 0; i < n; ++i)
    {
        fprintf(ofp, "%-*s%3d\n", MAX_LENGTH, names[i], ages[i]);
    }

    fclose(ofp);
}

********************************************************************************

/* SORT.H */

#include "p8.h"
void sort_data(char names[][MAX_LENGTH + 1], int ages[], int n);

********************************************************************************

/* SORT.C */

#include "sort.h"

void sort_data(char names[][MAX_LENGTH + 1], int ages[], int n)
{
    int i, j;
    int loc;
    char temp[MAX_LENGTH + 1];

    for (i = 0; i < n - 1; ++i)
    {
        loc = i;
        for (j = i + 1; j < n; ++j)
        {
            if (strcmp(names[j], names[loc]) < 0)
            {
                loc = j;
            }
        }

        if (loc != i)
        {
            strcpy(temp, names[i]);
            strcpy(names[i], names[loc]);
            strcpy(names[loc], temp);
        }
    }
}

********************************************************************************

/* SEARCH.H */

#include "p8.h"
void search_data(char names[][MAX_LENGTH + 1], int ages[], int n);
int get_loc(char names[][MAX_LENGTH + 1], int n, char* name);

********************************************************************************

/* SEARCH.C */

#include <stdio.h>
#include "search.h"

int get_loc(char names[][MAX_LENGTH + 1], int n, char* name)
{
    int i;

    for (i = 0; i < n; ++i)
    {
        if (!strncmp(names[i], name, strlen(name)))  /* or could strip blanks */
        {
            return i;
        }
    }

    return -1;
}

void search_data(char names[][MAX_LENGTH + 1], int ages[], int n)
{
    char name[101]; /* more than enough */
    int loc;

    printf("Age display for a given name\n\n");
    printf("Enter a name (blank to stop): ");

    while (gets(name), strlen(name) > 0)
    {
        if ((loc = get_loc(names, n, name)) == -1)
        {
            printf("%s is not in the list\n", name);
        }
        else
        {
            printf("%s is %d year(s) old\n", names[loc], ages[loc]);
        }

        printf("Enter a name (blank to stop): ");
    }
}
--
Raymond Kennington
LECTURER
University of South Australia   | Act in haste and repent at leisure
P.O. Box 1                      | Code too soon and debug forever
Ingle Farm                      | Knobs, knobs everywhere,
South Australia                 |              just vary a knob to think!
#! rnews 656