Homework 2


Due : 9.30am, 7 July

Split up the following program into three parts (5 files) using the standard technique of creating Source files and Header files so that the following conditions are satisfied.

Use the minimum necessary include statements, prototypes and external references to accomplish integration among the various modules.

I recommend trying out your code on a compiler to make sure it all works properly before you submit the homework. Clearly label each module (by its given filename) in your submitted version.


// This is a little program to calculate the golden ratio

#include <iostream.h>

const float square_root_5 = float (2.236);

float add ( float a, float b )
{
   return a+b;
}
   
float divide ( float c, float d )
{
   return c/d;
}
      
float goldenratio ()
{
   return divide (add (square_root_5, 1), 2);
}
      	
int main ()
{
   cout << "And the golden ratio number is : " << goldenratio () << "\n";
   return 0;
}

Solution

main.cpp

#include <iostream.h>
#include "goldenratio.h"

int main ()
{
   cout << "And the golden ratio number is : " << goldenratio () << "\n";
   return 0;
}

goldenratio.cpp

#include "operations.h"

const float square_root_5 = float (2.236);

float goldenratio ()
{
   return divide (add (square_root_5, 1), 2);
}

goldenratio.h

float goldenratio ();

operations.cpp

float add ( float a, float b )
{
   return a+b;
}
   
float divide ( float c, float d )
{
   return c/d;
}

operations.h

float add ( float a, float b );
float divide ( float c, float d );

Last updated : 7 July 2000 11.15am