From: marwk@levels.unisa.edu.au
Newsgroups: unisa.cis.prog.c.05286
Subject: PRACTICAL 6: Answers
Date: 15 Oct 92 14:48:26 +0930
Organization: University of South Australia

PRACTICAL 6: Solutions

1 mark off for any incorrect

Section  4.1  (3 marks)

 2. T
 4. F
 6. F
 8. in standard C: T, but F is acceptable if a good reason is given
10. e.g. fun2() prints a heading or sets up the printer or intialises global
                variables
    e.g. fun2() outputs results.

Section  4.2  (2 marks)

2. F
4. F
6. F
8. F

Section  4.3  (5 marks)

 2. T
 4. Yes
 6. F
 8. T
10. No parantheses
12. F
14. Declaration:  used to declare the return type and parameter types but does
                  not have to specify the names of the parameters.
    Definition:   includes the header and the body of the function and must
                  specify the names of the parameters

Section  4.4  (3 marks)

 2. F
 4. Depends on the number of arguments you want to pass to it
 6. F (prototypes take care of the conversion!)
 8. F
10. There is no rule regarding the order of evaluation

Section  4.7  (1 mark)

2. There is no relationship between them

Section  4.8   (1 for correct answer, 1 for correct reason)

2. No.  The pre-processor replaces all symbolic constants before the compiler
        gets its turn.

Section  4.11  (1 mark)

4. dcba


/* NEWTON.C */

Mark scheme

For 3 function prototypes:   3
For f()                      2
For fd()                     2
Description of solution()    3
Correct main()               2
Correct solution printed     2   (2.482)

To 12-digit accuracy
  change 1E-4 to 1E-12       1
  ALL float to double        2
  In printf() 8.3 --> 15.12  1
                            --
                            18

#include <stdio.h>

float f(float x);
float fd(float x);
float solution(float x_old, float x_new);

void main(void)
{
    float x0 = 1.0;
    float x1 = x0 - f(x0)/fd(x0);

    printf("The solution is: %8.3f\n", solution(x0,x1));
}

float solution(float x_old, float x_new)
{
    if (abs((x_new - x_old)/x_old) < 1E-4)  /* 4-digit accuracy */
    {
        return x_new;
    }
    else
    {
        x_old = x_new;
        x_new = x_old - f(x_old) / fd(x_old);

        return solution(x_old, x_new);
    }
}

float f(float x)
{
    return 3*x*x*x + x*x - 52;
}

float fd(float x)
{
    return 9*x*x + 2*x;
}

/* COMB.C */

Mark scheme

Two prototypes;   1
main()            2
comb()            1
fact()            1
                 --
Total             5

#include <stdio.h>

int c(int, int);
int fact(int);

void main(void)
{
    int n, r;
    int f1, f2;

    printf("Enter n: "); scanf("%d", &n);
    printf("Enter r: "); scanf("%d", &r);

    f1 = fact(n)/fact(r)/fact(n-r);
    f2 = c(n,r);

    printf("\nC(%d,%d) = %d and %d\n\n", n, r, f1, f2);
}

int c(int n, int r)
{
    if (r == 0 || r == n)
    {
        return 1;
    }
    else
    {
        return c(n-1, r) + c(n-1, r-1);
    }
}

int fact(int num)
{
    if (num <= 1)
    {
        return 1;
    }
    else
    {
        return num * fact(num - 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 380