Week 7 (CS 200)

Table of Contents

Functions, recursion, testing

🏠 Back to home page

1. Class schedule

  • Tuesday: Functions, Testing
  • Thursday: Recursion

2. Assignment links

Assignment Reading link Canvas link
📚 Intro: Functions Read Canvas quiz
📚 Intro: Testing Read Canvas quiz
📚 Intro: Recursion Read Canvas quiz
💾 Lab: Functions and recursion Read Canvas assignment

3. Example code

3.1. Functions

3.1.1. ✅ Inputs ✅ Output

// HAS OUTPUT (int)
// HAS INPUTS (a, b)
int Sum( int a, int b )
{
   int result = a + b;
   return result;
}

3.1.2. ❌ Inputs ✅ Output

// HAS OUTPUT (float)
// NO INPUTS
float GetTax()
{
   return 0.09;
}

3.1.3. ✅ Inputs ❌ Outputs

// NO OUTPUT
// HAS INPUTS (float)
void DisplayMoney( float cash )
{
   cout << "$" << cash << endl;
}

3.1.4. ❌ Inputs ❌ Outputs

// NO OUTPUT
// NO INPUTS
void DisplayMenu()
{
   cout << "1. New game" << endl;
   cout << "2. Load game" << endl;
   cout << "3. Quit" << endl;
}

3.1.5. GetChoice input validator function

int GetChoice( int minVal, int maxVal )
{
   int choice;
   cout << "INPUT: ";
   cin >> choice;

   // Make sure that minVal <= choice <= maxVal
   while ( choice < minVal || choice > maxVal )
   {
       cout << "Invalid choice. Select an option between "
           << minVal << " and " << maxVal << endl;
       cout << "INPUT: ";
       cin >> choice;
   }

   return choice;
}

3.1.6. Slope calculation

#include <iostream>
using namespace std;

float Slope( float x1, float y1, float x2, float y2 )
{
   return ( y2 - y1 ) / ( x2 - x1 );
}

int main( int argCount, char* args[] )
{
   if ( argCount != 5 )
   {
       cout << "MISSING ARGUMENTS! Expected form: "
           << args[0] << " x1 y1 x2 y2" << endl;
       return 1;
   }

   float x1 = stof( args[1] );
   float y1 = stof( args[2] );
   float x2 = stof( args[3] );
   float y2 = stof( args[4] );

   float slope = Slope( x1, y1, x2, y2 );

   cout << "POINT 1: (" << x1 << "," << y1 << "), "
       << "POINT 2: (" << x2 << "," << y2 << "), "
       << "SLOPE: " << slope << endl;

   return 0;
}

3.2. Recursion

3.2.1. Count consonants

//! Helper function to find whether something is a consonant or not.
bool IsConsonant( char letter )
{
   if (    tolower( letter ) == 'a' || tolower( letter ) == 'e' ||
           tolower( letter ) == 'i' || tolower( letter ) == 'o' ||
           tolower( letter ) == 'u'
       )
   {
       return false;
   }


   return true;
}

//! Count the amount of consonants in a string and return the count.
int CountConsonants_Iter( string text )
{
   int count = 0;
   for ( int i = 0; i < text.size(); i++ )
   {
       if ( IsConsonant( text[i] ) ) { count++; }
   }
   return count;
}

//! Count the amount of consonants in a string and return the count.
int CountConsonants_Rec( string text, unsigned int pos /* = 0 */ )
{
   // Terminating case: No more letters to look at.
   if ( pos == text.size() ) { return 0; }

   // Recursive case: Still more letters to inspect.
   int count = 0;
   if ( IsConsonant( text[pos] ) ) { count++; }

   return count + CountConsonants_Rec( text, pos+1 );
}

3.2.2. Get first uppercase character

//! Helper function to figure out if letter is upper-case
bool IsUppercase( char letter )
{
   return ( letter != ' ' && toupper( letter ) == letter );
}

//! Returns the first uppercase letter found, or ' ' if none are found.
char GetFirstUppercase_Iter( string text )
{
   for ( int i = 0; i < text.size(); i++ )
   {
       if ( IsUppercase( text[i] ) )
       {
           return text[i];
       }
   }
   return ' ';
}

//! Returns the first uppercase letter found, or ' ' if none are found.
char GetFirstUppercase_Rec( string text, unsigned int pos /* = 0 */ )
{
   // Terminating case: No more letters to look at, OR First uppercase letter found.
   if ( pos == text.size() ) // Ran out of letters
   {
       return ' ';
   }
   else if ( IsUppercase( text[pos] ) ) // Found first upper-case character
   {
       return text[pos];
   }

   // Recursive case:  Still more letters to investigate
   return GetFirstUppercase_Rec( text, pos+1 );
}

3.3. Testing

3.3.1. GetArea program with unit tests

  1. Functions.h
    #ifndef _FUNCTIONS // file guards
    #define _FUNCTIONS
    
    // FUNCTION DECLARATIONS
    
    float GetArea( float width, float length );
    
    void Test_GetArea();
    
    #endif
    
  2. Functions.cpp
    #include "Functions.h"
    
    #include <iostream>
    using namespace std;
    
    // FUNCTION DEFINITIONS
    float GetArea( float width, float length )
    {
      return width * length;
    }
    
    void Test_GetArea()
    {
      { // Test 1
        float width = 4;
        float length = 3;
        float expectedArea = 12;
        float actualArea = GetArea( width, length );
    
        if ( actualArea == expectedArea )
        {
          cout << "PASS - Test passed" << endl;
        }
        else
        {
        cout << "FAIL - Test failed" << endl;
        cout << "Inputs: width =  " << width << endl;
        cout << "        length = " << length << endl;
        cout << "Expected result: " << expectedArea << endl;
        cout << "Actual result:   " << actualArea << endl;
        }
      }
    
      { // Test 2
        float width = 5;
        float length = 5;
        float expectedArea = 25;
        float actualArea = GetArea( width, length );
    
        if ( actualArea == expectedArea )
        {
          cout << "PASS - Test passed" << endl;
        }
        else
        {
        cout << "FAIL - Test failed" << endl;
        cout << "Inputs: width =  " << width << endl;
        cout << "        length = " << length << endl;
        cout << "Expected result: " << expectedArea << endl;
        cout << "Actual result:   " << actualArea << endl;
        }
      }
    }
    
  3. main.cpp
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    #include "Functions.h"
    
    int main()
    {
      // -- TESTS -------------------------
      cout << "TESTS" << endl;
      Test_GetArea();
    
      // -- PROGRAM -----------------------
      cout << endl << "PROGRAM" << endl;
      float width, length, area;
    
      cout << "Get width: ";
      cin >> width;
    
      cout << "Get length: ";
      cin >> length;
    
      area = GetArea( width, length );
    
      cout << "Area: " << area << endl;
    
      return 0;
    }
    

Author: Moose (R.W.) Singh

Created: 2026-10-14 Wed 09:04

Validate