Home     Mosaic     CLIP     Octave     R Project     Processing     Contact  
Search
<http://cnfolio.com/IntroComputingPractical07>
Introduction to Computing – B142L

B142L Computing practical – Using functions

Academic year 2009/10


Learning outcomes

  1. Practice using functions for the purposes of code re-use, data conversion and logic design.
  2. Become familiar with function prototypes, return type, parameters and function definitions.
  3. Become familiar with the transfer of control sequence as functions are called within a C program.
  4. Practice using pseudo code and D structures to document program design.



Exercise 1: Multiplication tables


Following is part of a program that displays multiplication tables using a user defined function, multTable().

#include <stdio.h>

/* Add prototype here for multTable() function */

int main( void )
{
   /* Call multTable() function here to display multiplication tables */
}

/* Below is the definition for multTable() function */
void multTable( int number )
{
   int counter;
   
   for ( counter = 1; counter <= 10; counter++ )
      printf( "%2d x %2d = %3d \n", number, counter, number * counter );
}

#include <stdio.h>

void multTable( int );

int main( void )
{
   multTable( 1 );
   multTable( 2 );
   multTable( 3 );
   multTable( 4 );
   multTable( 5 );
   multTable( 6 );
   multTable( 7 );
   multTable( 8 );
   multTable( 9 );
   multTable( 10 );
   multTable( 11 );
   multTable( 12 );
   multTable( 13 );
   multTable( 14 );
   multTable( 15 );
   multTable( 16 );
   multTable( 17 );
   multTable( 18 );
   multTable( 19 );
   multTable( 20 );
}

void multTable( int number )
{
   int counter;
   
   for ( counter = 1; counter <= 10; counter++ )
      printf( "%2d x %2d = %3d \n", number, counter, number * counter );
}



Exercise 2: Pythagoras' theorem


Following is part of a program that uses Pythagoras' theorem to determine whether 3 sides form a right triangle.

Details about Pythagoras' theorem is available online:

#include <stdio.h>

void testTheorem( int, int, int );

int main( void )
{
}


Following is sample output when the input values are 3,4,5 3,5,6 5,12,13 6,12,14 0,1,1.
3, 4 and 5 form a right triangle.
3, 5 and 6 do NOT form a right triangle.
5, 12 and 13 form a right triangle.
6, 12 and 14 do NOT form a right triangle.
0, 1 and 1 form a right triangle.

#include <stdio.h>

void testTheorem( int, int, int );

int main( void )
{
   int n1, n2, n3;

   do
   {
      scanf( "%d,%d,%d", &n1, &n2, &n3 );
      testTheorem( n1, n2, n3 );
   } while ( ( n1 != 0 ) && ( n2 != 0 ) && ( n3 != 0 ) );
}

void testTheorem( int a, int b, int c )
{
   if ( ( ( a * a ) + ( b * b ) ) == ( c * c ) )
      printf( "%d, %d and %d form a right triangle.\n", a, b, c );
   else
      printf( "%d, %d and %d do NOT form a right triangle.\n", a, b, c );
}



Exercise 3: Car parking fees


Write a C program that has at least one user defined function to calculate car parking fees according to the following schedule of rates:

The program should accept integer input values as comma separated pairs:

Following is sample output when the input values are 90,1 90,0 300,1 300,0 302,1 302,0 0,1.
The parking fee is £9.00 for 90 minutes using weekday rates
The parking fee is £8.50 for 90 minutes using weekend rates
The parking fee is £23.50 for 300 minutes using weekday rates
The parking fee is £21.75 for 300 minutes using weekend rates
The parking fee is £28.00 for 302 minutes using weekday rates
The parking fee is £26.00 for 302 minutes using weekend rates 

#include <stdio.h>

double carParkFees( int, int );

int main( void )
{
   int minutes, weekday;

   do
   {
      scanf( "%d,%d", &minutes, &weekday );
      if ( minutes != 0 )
      {
         printf( "The parking fee is £%.2lf for %d minutes ", carParkFees( minutes, weekday ), minutes );
         if ( weekday == 1 )
            printf( "using weekday rates \n" );
         else
            printf( "using weekend rates \n" );
      }
   } while ( minutes != 0 );
}

double carParkFees( int feesMinutes, int feesRate )
{
   double fees = 0.0;
   int hours = feesMinutes / 60;
   
   if ( feesMinutes > ( hours * 60 ) )
      hours = hours + 1;
   
   if ( feesRate == 1 )
   {
      if ( hours > 2 )
      {
         fees = 10.0;
         hours = hours - 2;
      }
      
      fees = fees + ( hours * 4.5 );
   }
   else
   {
      if ( hours > 2 )
      {
         fees = 9.0;
         hours = hours - 2;
      }
      
      fees = fees + ( hours * 4.25 );
   }
   
   return fees;
}