From: marwk@levels.unisa.edu.au
Newsgroups: unisa.cis.prog.c.05286
Subject: PRACTICAL 7: Answers
Date: 5 Nov 92 00:59:33 +1030
Organization: University of South Australia

PRACTICAL 7: Answers

Written
-------
5.1 Q 6    1 mark for:  6       5       1
           2 marks for: Nothing! it will not compile as z is not defined

5.4 Q 1    true      (1 mark)
      2    false     (1 mark)
      3    false     (1 mark)
      4    true      (1 mark)

      5    c    main() in file A                     (7 marks)
           d    all functions in file B
           v    f2() in file B
           x    print_prompt() & far_out() in file B
                main() in file A
           y    f1() in file A
                far_out() in file B.

      6    Advantages     (3 marks for any 3 of the following 5 statements)
               - don't have to pass as a parameter
               - saves storage space
               - increases execution time

           Disadvantages
               - may be difficult to follow changes to the variable
               - makes functions dependent on variables other than parameters

5.5  Q1  true      (7 marks)
      2  true
      3  true
      4  false
      5  false
      6  false
      7  2
         1
         -999

5.6 Q3   declare fun5() as static in a module which only contains the other
         4 functions.  (2 marks)

     4   false (either before or after)    (1 mark)

6.1 Q4   () is an operator with the highest order of precedence.   (1 mark)

6.2 Q2   false    (1 mark each)
     4   true
     6   26
     8   true
    10   they are the same
    12   should be int numbers[100];
    14   assigns 5 numbers to a 3-element array
    16   26, -1003, 0, 0, 0
    18   Rams        (note that the \0 terminator is not included)
    20   1000
    22   2

Coded, commented, tested and debugged.
--------------------------------------
Programming Exercise 6.10 (page 310)

/* P7.C - Programming Exercise 6.10 (page 310) */

#include <stdio.h>

#include "isbn.h"

void main(void)
{
    char valid[]   = "0-8065-0959-7";
    char invalid[] = "0-8065-0959-4";

    if (is_isbn(valid))
    {
        printf("%s is a valid ISBN\n", valid);
    }
    else
    {
        printf("%s is NOT a valid ISBN\n", valid);
    }

    if (is_isbn(invalid))
    {
        printf("%s is a valid ISBN\n", invalid);
    }
    else
    {
        printf("%s is NOT a valid ISBN\n", invalid);
    }
}

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

/* ISBN.C - for P7.C - Programming Exercise 6.10 (page 310) */

#include <ctype.h>
#include "isbn.h"

int is_isbn(char *s)
{
    int i;
    int sum;
    int factor;

    if (strlen(s) != 13)  /* not the correct length */
    {
        return 0;
    }

    for (i = 0; i < 13; ++i)  /* check for valid contents */
    {
        if (i == 1 || i == 6 || i == 11)
        {
            if (s[i] != '-')  /* must be a dash here */
            {
                return 0;
            }
        }
        else
        {
            if (!isdigit(s[i]))  /* should be a digit here */
            {
                return 0;
            }
        }
    }

    sum = 0;
    factor = 0;

    for (i = 0; i < 12; ++i)
    {
        if (i == 1 || i == 6 || i == 11)
        {
            continue;
        }

        factor += 1;
        sum    += factor * (s[i] - '0');  /* convert to an integer */
    }

    return sum % 11 == s[12] - '0';
}

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

/* ISBN.H */

int is_isbn(char* s);

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

P7.PRJ

p7.c    (isbn.h)
isbn.c  (isbn.h)

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

Marking scheme:

ISBN header file included: 2
TURBO C project file:      2
TURBO C output             2
UNIX output                2
VAX output                 2

main()
    valid isbn             1
    invalid isbn           1
    if test/printf()       2

is_isbn()
    length test            1
    valid characters test  2
    calculate remainder    3
    return value           1
--
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 419