Monday, February 2, 2009

My beginnings in C++

This semester (my final semester at VCU), I am taking INFO 450: Advanced Programming, which concentrates in C++. We're learning both native and managed C++. Our first assignment was simple: a console application to calculate the monthly payments on a car loan, based on interest rate, total loan, and loan duration. That was pretty easy.

This assignment was pretty rough. We had to code a fully functional ATM application, which uses a hardcoded bank database, accounts, authentication, withdrawal & deposit simulation, etc. We were told that we could mirror the ATM case study at the end of our Learn to Program C# 2005 by Deitel (the book from intermediate programming).

After mirroring the C# code, I kept getting error after error. It was ridiculous how every error stemmed from how header files were used in my code. Eventually, I worked out the problems with the header files. Then, I kept getting this compiler error: LNK2019. I searched and searched online, and couldn't find a suitable answer. Finally, I retraced all my code, and realized that I had three classes (Withdrawal, Deposit, BalanceInquiry) all overriding the virtual function 'Execute' from class Transaction, but that class wasn't marked virtual. This probably should have been the first place I looked, but keep in mind we were given this assignment the day after we learned about references.

To make a long story short, I was receiving LNK2019 errors because of my virtual functions. The error was caused by declaring the function as "virtual void Execute();" instead of "virtual void Execute() = 0;" Embarrassingly, that little mistake took me forever to figure out. I somewhat hope that whatever path I take in the future uses tools with less cryptic error messages.

C# Insertion Sorting algorithm

Here is a console application to test an Insertion Sorting algorithm adapted from chapter 2 of MIT's Introduction to Algorithms. I thought this might come in handy for any beginners, as well as a fun way to test your computer's performance.
My desktop averages about 24.9 seconds, which, of course, depends on how scattered the integers are in the array.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace InsertionSorting
   7:  {
   8:      class Program
   9:      {
  10:          static void Main(string[] args)
  11:          {
  12:              // int[] A = {5, 2, 4, 6, 1, 3};
  13:              int initializer = 100000;
  14:              int[] A = new int[initializer];
  15:   
  16:              for (int c = 0; c < A.Length; c++)
  17:              {
  18:                  A[c] = c;
  19:              }
  20:   
  21:              // Random shuffle
  22:              Random rand = new Random();
  23:              for (int d = 0; d < A.Length; d++)
  24:              {
  25:                  int firstInt = A[d];
  26:                  int swapIndex = rand.Next(initializer);
  27:                  int secondInt = A[swapIndex];
  28:   
  29:                  A[d] = secondInt;
  30:                  A[swapIndex] = firstInt;
  31:              }
  32:              
  33:              //Console.Write("Array before sorting: ");
  34:              //foreach (int number in A)
  35:              //{
  36:              //    Console.Write("{0} ", number);
  37:              //}
  38:              //Console.WriteLine("");
  39:   
  40:              int key;
  41:              int i;
  42:              int j;
  43:   
  44:              Console.Write("Insertion Sorting array of {0} numbers, please wait...", initializer);
  45:              
  46:              DateTime start = DateTime.Now;
  47:              for (j = 1; j < A.Length; j++)
  48:              {
  49:                  key = A[j];
  50:                  i = j;
  51:   
  52:                  while (i > 0 && A[i - 1] > key)
  53:                  {                    
  54:                      A[i] = A[i-1];
  55:                      i--;
  56:                  };
  57:                  
  58:                  A[i] = key;
  59:              }
  60:              DateTime finish = DateTime.Now;
  61:              
  62:              //Console.Write("Array after sorting: ");
  63:              //foreach (int number in A)
  64:              //{
  65:              //    Console.Write("{0} ", number);
  66:              //}
  67:   
  68:              Console.WriteLine("");
  69:              Console.WriteLine("Duration: {0} seconds", (finish - start).ToString());
  70:          }
  71:      }
  72:  }
  73:   

Archive