Assignment 6 Code Review

Advanced Programming II - 10 April 2001


Algorithms


Algorithm Estimates


File Counts

FileCount
  
Avg Lines
  
Avg Semicolons
Countsper Fileper File
115141104
249570
319348
416939
516026


Code Size, Newlines

000100200300
031
103
203
3031
40
502
602
7012
80
901
Totals11461


Code Size, Semicolons

000100200
03
103
2011
301
401
5012
6022
70
802
903
Totals8131


Procedure Statistics

totalpages per procedure
procs12345678910
11
11
1*41
211
2*211
211
211
3111
3111
312
321
761
77
11101
12111
12111
14131


How People Lost Points

Not handling null input - 2

Not handling the empty prefix - 5

Not handling big commands - 4

Not handling many commands - 2

Not handling duplicates - 2

Not doing prefix matching - 3

Not doing storage management - 10


Be Straightforward

while( (input=getchar()) != EOF )
  switch (input) {
    case SPACE:
      if (last_digit == COMMAND) {
	command_list = realloc(command_list, strlen(command_list) + 2);
	if (command_list == 0) {
	  cout << "Error: Not enough memory." << endl; 
	  exit(-1);
	  }
	strcat (command_list, " ");		
	}
      last_digit = EMPTY;
      break;

    case ENTER:
      break;

    default:
      command_list = realloc(command_list, strlen(command_list) + 2);
      if (command_list == 0) {
	cout << "Error: Not enough memory." << endl; 
	exit(-1);
	}

      if (input>='A' && input <= 'Z')
	input = input+'a'-'A';
      token[0] = input;

      strcat (command_list, token);
      if (last_digit==EMPTY) cmd_number ++;
      last_digit = COMMAND;
      break;
    }


Don't Use char * Strings

int MAX_LEN_OF_CMD = 300;
char inputCmd[MAX_LEN_OF_CMD];

cin >> inputCmd;
while (!cin.eof()) {
  if (noOfCmd > 0)
    cout << "," << endl;
  cout <<  "                      \"" << inputCmd << "\"";
  noOfCmd++;
  cin >> inputCmd;
  }


Always Know Where Array Writes Go

string words[4000];
int counter = 0;
int l;

cin >> words[counter];
while(!cin.eof()) {
  l = words[counter].length();
  for(int m = 0; m < l; m++) {
    if (isupper(words[counter][m]))
      words[counter][m] = static_cast(tolower(words[counter][m]));
    }
  counter++;
  cin >> words[counter];
  }


Know Your Tools

string convert_to_uppercase_and_quotes(string input_command) {

  // converts string from lower to upper case
  // adds \ for quotes 

 int size = input_command.length();
 static string output_command;
 
 for(int x=0; x<=size; x++)
  if(input_command[x] >= 'a' && input_command[x] <= 'z')
     output_command += (input_command[x]-TO_UPPER);
  else {
    if (input_command[x] == '\"')
       output_command += '\\';
    output_command +=input_command[x];
    }

 return output_command;
 }


Know Your I-O


Know Your Limits

int size = cmd_in.length();
static string cmd_out;

for (int x = 0; x <= size; x++) {
 if (cmd_in[x] >= 'a' && cmd_in[x] <= 'z')
   cmd_out += (cmd_in[x] - TO_UPPER);
 else{
   if (cmd_in[x] == '\"')
     cmd_out += '\\';
   cmd_out += cmd_in[x];
   }

cout << "for (unsigned x = 0; x <= cmd.length(); x++) {\n";


Design Intelligently


This page last modified on 27 February 2001.