CMPT 130 Assignment 3 – Printing Numbers

 

For this assignment you are to implement functions to convert integers to their written form.  You will notice that this assignment is written somewhat differently to the first two assignments in that you are given less guidance – this is intentional! In this assignment you are responsible for writing two functions, one that converts integers to strings and one that prints sequences of these strings. You are also expected to write subsidiary functions that will be used by them. You do not have to write a main function. Please read the requirements and look at the assessment section to get an idea of how we are going to mark your work, pay particular attention to the marks for use of functions and the penalties for incorrect submissions. This assignment is worth 5% of your grade.

 

Your functions will be marked by compiling your .cpp file using g++ on Linux; therefore you should test your functions with this compiler and in the Linux OS before submitting it. If you fail to do this, and if your program does not compile it will not be marked. This assignment is worth 5% of your final grade.

 

Description

You are to write functions that convert integers in the range of 0 to 99999 (inclusive) to words. Numbers are to be displayed in their natural English written form. Generally, that is the string x thousand, y hundred and z – where x, and y are the English representations for the number of thousands and hundreds respectively and z is the English representation for values between zero and ninety-nine. Here are some examples of how numbers should be represented.

 

0 – zero

15 – fifteen

23 – twenty-three

80 – eighty

254 – two hundred and fifty-four

400 – four hundred

1,208 – one thousand, two hundred and eight

21,034 – twenty-one thousand and thirty-four

30,000 – thirty thousand

 

You must follow these rules for representing numbers. Note that these rules are not exhaustive. If you have any questions about rules for number strings please post them on the discussion forum for this assignment; my answers to such questions are considered as part of the assignment specification.

1.      All letters must be lower case

2.      Thousands and hundreds should be separated by a comma and a space (e.g. twelve thousand, two hundred …)

3.      Hundreds and tens should be separated by a space, the word and and another space (e.g. four hundred and thirteen)

4.      If there are thousands and tens, but zero hundreds, the thousands and tens should be separated as described in 3 (e.g. two thousand and twenty)

5.      The tens and ones should be separated by a dash, but no spaces (e.g. forty-six)

6.      Exception to 5 – the numbers 11 to 19 should be represented by the appropriate words (eleven, twelve, …)

7.      If a number is zero it should not be included in the string (e.g. two hundred – not two hundred and zero)

8.      Exception to 7 – if the entire integer is 0 it should be represented by the word zero

9.      Strings should not contain newline (endl) characters, or any other character that is not discussed in these rules, that is not part of a word representing a number

 

Requirements

 

intToString Function

You must implement a function called intToString. This function should return a string and have an integer as its only parameter. The function should convert its integer parameter into a string that is returned by the function as discussed above. See Use of Functions below for additional requirements.

 

printIntStrings Function

In addition to the intToString function you must implement a function called printIntStrings that prints the string representation of integers in a given sequence. Each number string should be printed on a separate line. This function should be void and should have three integer parameters. These parameters represent the start, end and interval of the sequence in that order. See the examples below.

 

printIntStrings(0, 500, 100) output – start at 0, end at 500, intervals of 100

zero

one hundred

two hundred

three hundred

four hundred

five hundred

 

printIntStrings(118, 123, 1) output – start at 118, end at 123, intervals of 1

one hundred and eighteen

one hundred and nineteen

one hundred and twenty

one hundred and twenty-one

one hundred and twenty-two

one hundred and twenty-three

 

Use of Functions

You are responsible for sensibly decomposing this problem into a number of additional functions. This is particularly applicable to the intToString function which can be naturally decomposed into several functions, each responsible for different parts of the number. If you choose not to do this – and write the intToString function as one large function that does not call subsidiary functions – you will lose many or all the marks assigned to use of functions in the marking scheme.

                  

Input Testing

Your intToString function should return the string “error” if its parameter is out of the given range of integers to be converted to a string (i.e. not in the range from 0 to 99,999 inclusive). You are not responsible for dealing with any other input errors.

 

Main Function

Your submitted file must not contain a main function. You will have to write a main function to test your functions but you should delete or comment this out before submitting your assignment.

 

Assessment

The assignment is out of 46.  Marks are assigned as follows:

§  String correctness – 20

§  printIntStrings function – 4

§  Input checking – 2

§  Function design1 – 12

§  Variable and function naming – 4

§  Comments – 2

§  Indentation – 2

§  Penalty – failure to write intToString and printIntStrings headers exactly as described under Requirements will result in a 4-mark penalty for each function incorrectly specified. This includes function name, parameter list (including order) and return type.

§  Penalty – if your file contains a main function (that is not commented out) you will receive a 2-mark penalty.

 

Note 1 –the assignment description does not tell you exactly which functions to write – however you are responsible for decomposing your program into functions, which should have input parameters and return values as appropriate.

 

Submission

Your submission should consist of the following, in this order:

1.      include statements for iostream, string and cmath

2.      using namespace std

3.      function prototypes for all your functions

4.      definitions of all your functions

 

You should submit your assignment online to the CoursSys submission server.  You must submit a single .cpp file named numbers.cpp, please read the documentation on site for further information.  The assignment is due by 11:59 pm on Monday the 29th of October.

 

 

CMPT 130 Home

 

John Edgar (johnwill@sfu.ca)