CMPT130 2021-02 J. Ye


Assignment 2 (100 marks)

Due date: Monday, August 9, 10:00pm PT (Late submission is not accepted.)


General instructions for submission:

Include your Part I function definitions (you don't need to include the function declarations) and your Part II code in one single .cpp file and use your first name to name this file.

Include your name, your FIC ID and your class number as comments at the beginning of your file.

Include the following declaration as comments at the beginning of your cpp file:

I certify that the following code is my own original work and I didn’t use any

material drawn from other sources except for my cmpt130 course materials

Do not comment out any part of the code in your submitted file.

Follow the link under Week 13: "Assignment 2 submission" to upload your file.

You need to thoroughly test all your code before submitting them. However, do not include any testing code for Part I functions in your submission. Once you finished testing your Part I functions, remove the testing code.

Follow good coding style: appropriate indentation, good variable names, sufficient spaces, appropriate C++ program format. Coding style will also be graded.

I use Visual C++ 2010 Express to mark your assignment. If you did this assignment using other software, such as Xcode, make sure it complies with Visual C++ standard


Coding requirements:

Read each question carefully. If a function does not deal with I/O, do not include cin or cout statements in its definition.

You must name the function exactly as specified in the question. Otherwise, you will lose marks.

Any non-constant global variable declared outside functions is not allowed.

Do not use anything we have not covered in the course.

If a function has a precondition, you must include it.

For anything that is not specified, make your best choice.


Part I: define functions (30 marks)

1. For this question, you need to use C++ string. Add #include at the beginning of your .cpp file. string is a C++ (object) type.

Define a function named is_palindrome1 that takes one argument of string type and checks (return true or false) whether a given C++ string is a palindrome. A palindrome is a string/word that reads the same way forward and backward, such as "radar", "noon", or "rats live on no evil star". An empty string "" (length 0) and a single character string such as "M" are both considered to be palindromes. "levels", on the other hand, is not a palindrome.

More Coding requirements for this question:

You cannot use any string library functions or operators other than .length() and the indexing operator []; you cannot use recursion; you can use only one return statement.

2. For this question, you need to use C-string.

Define a function named is_palindrome2 that takes one argument of C-string and checks (return true or false) whether it is a palindrome.

More Coding requirements for this question:

You cannot use any cstring library functions; You cannot use recursion; you can use only one return statement.

3. For this question, you need to use C++ string.

Define a function named compareStrings that takes two C++ strings as its arguments, say s1 and s2, and returns 1 if s1 is greater than s2, returns -1 if s1 is less than s2, returns 0 if they are identical strings. See the following sample testing and the expected output.


More Coding requirements for this question:

You cannot use any string library functions or operators other than .length() and the indexing operator []; you cannot use recursion.


Part II: write a program that uses functions (60 marks)

Write a C++ program to prompt the user to enter a number of names, then print out all the names in alphabetical order of the last names. If the last names are the same, then the first names are compared. A sample run is attached as follows (the text in bold face is user input):


More program specifications and requirements:

a. We only deal with names consisting of two parts: the first name and last name.

b. When user enters a name, it is always the first name first, followed by a space, and followed by the last name. The initials are in capital, other characters are in lower-case. This is considered as valid input. You do not need to worry about invalid user inputs for this program.

c. The required output is the last name first, followed by a comma, and a space, then the first name.

d. There are different approaches to this question. Do not rush typing the code. First, think carefully about the algorithm you will use. And, do not use anything we didn’t cover in the course.

e. The strings we are using in this program are C-strings. You cannot use C++ . And, you cannot use any library function other than the overloaded operator << and operator >>

f. Whenever you think is helpful and/or appropriate, break the program into tasks that you can place into functions. For this program, you must create at least two functions that are NOT responsible for any input/output. And do not use any non-constant global variables inside your function(s).

g. For each function, you must provide a one-sentence (short) comment describing what it does. If you cannot find a good descriptive function name or you cannot come up with a short descriptive sentence about what the function does, chances are you put too much stuff in the function. You may need to break it into two or even more functions, or, you may need to reconsider/redesign your algorithm

h. Not only the correctness (whether your program works), the design of functions and the overall program will also be graded.