Introduction to Computer Science I by James Tam Return to the course web page

CPSC 231: Assignment 6 (Worth 5%)

 

New Concepts to be applied for the assignment

  1. One-dimensional arrays

 

Implementing a string

A string is a collection of characters that include a set of built in operations.  Common string functions include:

You are to implement the first two string functions for this assignment.  The string or strings must be implemented as a one-dimensional character array (for feature #2 you will need two character arrays).   You cannot use the 'string' type nor can you use the string functions that are provided with the string type.                                                                                                                                                                       

 

Grading: Working submissions

Assignments that receive a 'C'

Your program implements the string as an 81 element character array.  The string can store up to 80 characters.  The last character in the array will be followed by a special character (ASCII value 32) that marks the end of the array.  If the user typed in 80 characters then the 81st element would contain a character with an ASCII value of 32.  Your program starts by introducing the functions that you actually implemented and it ends with brief parting message.   In between it will prompt the user to enter in a string of characters which is to be eventually stored in the character array.  (JT's note: to make it easier to complete extra feature #1, you may want to read the information one character at a time into a temporary character variable with the 'read' procedure before you actually copy the information into each successive element of the array to avoid overflowing the bounds of the array if the user types in more than 80 characters - any extra characters beyond the 80th won't be read into the array).   As usual the user signals to your program that he or she has finished inputting the information by hitting the 'enter' key so the last character will be the end-of-line marker (EOL) which this particular compiler stores as ASCII 32 (if displayed onscreen it appears as a space).  This character is to be read into the string but your program should not display it onscreen.  

Here's how to picture the process:

Supposed that the user typed in 'abc' and then hit enter.  The stream of characters that were typed in will be stored in some temporary memory location (typically computers have a keyboard buffer to store this information):

First character Second character Third character Fourth character
'a' (ASCII 97) 'b' (ASCII 98) 'c' (ASCII 99) EOL/SPACE (ASCII 32)
 

 

 

Successively read each of these characters up to and including the character with an ASCII value of 32 into a temporary character variable.  Successively copy the character from the temporary variable into each element of your character array.  (When you implement feature #1 below, you will stop the copy process when the array is already full to avoid overflow).

 
 

 
Array element[1] Array element[2] Array element [3] Array element[4] Array element [5] ... Array element [81]
'a' 'b' 'c' EOL/SPACE Not occupied ... Not occupied

When your program displays the array onscreen it should display all the characters except for the character containing the EOL/SPACE (to do this your program must explicitly check for and avoid displaying this value)  Thus the EOL/SPACE character is used to mark the end of the input.  You can assume that the strings entered by the user will not explicitly contain a space (and it is acceptable if your program treats any spaces entered by the user as the end of input so it won't read in any values that follow the space).  Finally you must implement another procedure that will display the integer ASCII value for each character element in the string up to and including the EOL/SPACE so in the previous example the program would display 97, 98, 99, 32.  Note: The display of the ASCII values not a function that is typically implemented when writing a string but it is a useful debugging tool for you and it allows your TA to mark your assignment more quickly.  If you need to see the table of ASCII values at the UNIX command line type 'man ascii'.

Missing the above features:

Extra features:

Each of the extra features below are worth an additional two letter 'steps' (e.g., 'C' to 'B-').   These features can be implemented in whatever combination that you wish.  Implementing the base assignment and all three extra features makes you eligible to receive an 'A' grade.   The other submission requirements (shown below) and coding style requirements must also be fulfilled.  

1)   Checking the input: Your program must check that the number of characters that will be stored in the array doesn't exceed the size of the array.  If the user types in more characters than can be stored in the array your program should ignore any additional characters after the 80th and also it must mark the end of the array by putting the end-of-line marker in the 81st element.  (JT's hint: When testing if this feature works you may want to reduce the size of the array from 80 to a more manageable size like 6 so you don't have to keep typing 81 plus characters each time that want to run a test).
               
Before you can get credit for the features below your program must run in an iterative fashion (loop until the user quits the program in the same way that the sample executable works) and it must display a menu of functions.  Two of the menu functions will correspond to the string functions below (as you implement them) and the third will allow the user to quit the program.  If you implement one or both of the features below but miss the requirement for looping the program and displaying the menu then your program will lose a letter step.
     
2)   Implementing a string length feature: This feature will take the form a function that takes as input a string and it returns an integer value that corresponds to the length of the string.  This excludes the marker for the end-of-line which means that if the user simply hits enter when prompted to type in a string then the function will count the length of the string as zero.  Again your program should display the ASCII values for the string that was stored in memory.  After determining the length of the string your program should display onscreen the characters in the string and it's length.
     
3)   Implementing a string compare feature: When this feature is selected from the main menu will prompt the user to enter in two strings.  This feature will take the form of a function that takes as input the two strings to be compared and returns one of three integer values depending upon the result of an alphabetical comparison of the two strings: -1 if the first string comes before the second, +1 if the first string comes after the second string or 0 if the strings are identical.  Your program will then display onscreen both strings as well as the ordering of both strings. Finally your program should display the ASCII values for both strings.  Note: You must do the comparison by manually checking each element of the character arrays yourself.  You won't get credit if you simply use a logical operator like '<' to compare the two arrays (e.g., if (array1 < array2) then...) because you are supposed to figure out how to do it yourself.

Grading: Non-working submissions

D submissions

D- submissions

Other submission requirements

In addition to having fulfill the generic assignment requirements the requirements specific to this assignment include:

  1. Good coding style and documentation:  They will play a role in determining your final grade for this assignment.  Your grade can be reduced by a letter step or more (e.g., 'A' to 'A-' for poor programming style such as employing poor naming conventions for identifiers, insufficient documentation or the use of global variables).  For additional details see the marking guide for coding style.

  2. Include a README file in your submission:  For this assignment your README file must indicate what grade level has been completed (e.g., 'A') and which features have been implemented.  This will allow your marker to quickly determine what he or she must look for and speed up the marking process.  Also you should include your contact information: your name, university identification number and UNIX login name so that your marker knows whose assignment that he or she is marking.  Omitting the necessary information from this file will result in the loss of a letter 'step' (assuming that the marker can actually figure out who the assignment belongs to).

  3. Assignments (source code/'dot-p' file and the README file) must be electronically submitted via submit.  In addition a paper print out of the source code and README must be handed into the assignment drop box (located on the second floor of the Math Sciences building) for the tutorial that you are registered in.  Electronically submitting the assignment allows your marker to run the code in order to quickly determine what features were implemented.  Providing a paper printout makes it easier for your marker to read and flip through your source code.  Omitting the paper printout of your assignment will result in the loss of a letter 'step'.  Omitting the electronic submission of your program will only allow you to receive a maximum grade of D-.

  4. As a reminder, you are not allowed to work in groups for this class.   Copying the work of another student will be regarded as academic misconduct (cheating).  For additional details about what is and is not okay for this class please refer to the following link.

 

Sample Executable

You can run a sample executable called 'str' which can be found in Unix in the directory: /home/231/assignments/assignment6.  In addition there is a Pascal program called 'manipulateAscii.p' that shows you how you can go from an ASCII value to a character and vice versa.

1 What does and doesn't constitute a sufficient amount of time and effort?  It's a judgment call on the part of your marker.  More often than not if you put in a reasonable amount of effort into your assignment and for some reason you just couldn't get it to work then you will receive some credit for your work.   An example of when you wouldn't receive credit is when you simply handed someone else's work.  This latter case assumes that you properly cited the other person's work, if you didn't cite your source and tried to claim that it was your own work then it would be an example of academic misconduct (cheating).