Lab #14, Friday, April 27

The purpose of this lab is to give you more practice with regular expressions and C.

Regular Expressions

Write a program that uses regular expressions to validate a string that represents a date in the format MM/DD/YY. The user should be able to type in a string and the program should output if it is a valid date or not. YY must always be two digits, but MM could possibly be a single digit from 1 to 9 (e.g. 1 for January rather than 01) or two digits from 01 to 12. The digits in MM should not exceed 12, for example, 13 is an invalid month. Similarly, DD could also be a single digit from 1 to 9 or two digits from 00 to 31. The digits in DD should not exceed 31, for example, 32 is an invalid day. Your program should test several dates and output whether or not the dates are valid. Don't worry about dates like February 30 which do not exist but would be allowed by the rules presented here.

You can use the first regex program given in the class notes as a guide. You mainly would just need to come up with a new regular expression.

C Programming

Below is a C++ program (without classes) that calculates the future value of a monetary value under compound interest. Re-write the program in C so that it:

  1. Passes parameters by pointer instead of by reference (since there is no reference passing in C)
  2. Uses scanf to read in data from the keyboard
  3. Uses printf to output to the console

#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;

void getValues(double &pv, double &r, int &n)
{
  cout << "Enter current amount (present value). " << endl;
  cin >> pv;
  cout <<  "Enter yearly interest rate." << endl;
  cin >> r;
  cout <<  "Enter number of years of compound interest." << endl;
  cin >> n;
}

int main()
{
   double pv, fv, r;
   int n;

   getValues(pv, r, n);
   fv = pv * pow(1+r, n);
   cout <<  "$" <<  pv <<  " after " <<  n <<
        " years with a compound interest rate of " <<  r <<
        " is $" <<  fv <<  " dollars." <<  endl;
}

If you are working in Linux, you probably need to compile your C program with: gcc program.c -lm

As an exanple, $1000 at a compound interest rate of 0.05 for 12 years would result in a future value of $1795.86.

Show your code/program to the instructor or lab assistant for credit, or email to kjmock@alaska.edu.