CMPT 225 Assignment 2


Start by downloading the assignment files. This zipfile contains a makefile, a test script and inputs/ground truths, and stubs for all of the .cpp files you need. Please do not create any additional .h and .cpp files.


Part 1 - Words

Consider the function shown below. Roughly, the function returns true if the letters in the string s are in alphabetical order, false otherwise.

//  PRE: s is composed of letters from the English alphabet, with no other characters.


bool isInAlphabeticalOrder(string s) {
	int length = s.size();
	for (int i = 0; i < length - 1; ++i) {
		if (s[i] > s[i+1]) {
			return false;
		}
	}
	return true;
}

ASCII

An exact description is that the function isInAlphabeticalOrder returns true if the characters in the string s are in ASCII order; this differs from alphabetical order in that in ASCII order, all capital letters come before all lower-case ones.

ASCII is the character encoding we most often use for storing and manipulating text on computers. It assigns a 7-bit code to the common characters used in English writing, including numbers, basic math symbols, and punctuation. Inside memory, we commonly store the 7-bit ASCII code inside the lower bits of an 8-bit byte. Ask a search engine about "ASCII" to find a chart of which codes (numbers) are assigned to which characters.

Your task

We will be concerned with how many times the character comparison (s[i] > s[i+1]) is executed. First, implement the isInAlphabeticalOrder function in C++ in the file words.cpp. The makefile contains a definition for words. You can build the executable words for this part of the assignment by running "make words". ("make" or "make all" will build both words and the executable for the second part of the assignment.)

Determine the following for the English words listed in file wordlist (do not convert to lowercase).

  1. The average length of a word
  2. The average number of character comparisons performed by isInAlphabeticalOrder
  3. The average number of character comparisons as a function of the word length.

You will need to add code to isInAlphabeticalOrder (or create a new function) to help you determine these values.

Note that words.cpp contains code for reading wordlist.

Submit word_answers.txt and the gnuplot output average_comps.png with your C++ files from Part 2.

Part 2 - Mode

Write a C++ function that obtains the mode of a set of integers stored in an array. Recall that the mode of a set is the most frequently occurring element.

Please use the provided file mode.cpp, and fill in the function mode. Note: you must write any auxialliary functions you use, and may not include any external libraries to help (other than iostream and fstream). The makefile contains a definition for mode. You can build the executable mode for this part of the assignment by running "make mode".

Testing

The zipfile contains a testing script, test.py. You should run this, and other test cases, to verify correctness of your mode function.


Grading

The assignment is worth 4% and marks are allocated to the assignment as follows:

Submission

You should submit your assignment online to the CourSys submission server. You should submit the following:

The assignment is due at 11:59pm on February 23.