Concordia University
Comp 218 – Winter 2019
Fundamentals of Object Oriented Programming
Assignment 3 – Due by 11:59pm, Thursday March 21, 2019 ______________________________________________________________________________
PURPOSE: The purpose of this assignment is to expose you to problem solving and to practice with various loops and nested loops, reading/writing to a file and writing your own functions.
Specifications for programming:
– Refer to handout for Assignment #1
Programming Exercise #1 – File I/O (6 pts)
Write a program that reads the content of a text file line by line and writes it in reverse order into a new text file.
For example if the original file has the text:
Hello there,
How are you today?
Boy this text will look funny in reverse order!!!
The new text file will contain:
,ereht olleH
?!yadot ouy era woH
!!!redro esrever ni ynnuf kool lliw txet siht yoB
How to create a data file:
Using the C++ editor, open a blank file, type in the text and save the file. You could name your data file A3Q3.dat. Do not give a data file the extension .cpp. Save it in the same folder as the .cpp file containing the program that will read the text file.
What to do:
Here is a general description of what your program is to do.
– Have a Welcome banner explaining to the user the purpose of this program. Make sure
your name appear somewhere in the Welcome Banner.
– Open the two data files: one for reading the other for writing.
– If the input file could not be opened, send an error message and end the program with a
return 0; statement.
– As long as there is another line of text in the text file:
– Read one line of text.
– Output the line read to the screen so the user can see the original text.
– Reverse the line and write it to the output text file only.
– Close both text files.
– Send a closing message so that the user knows the program has terminated normally.
Comp 218/Winter 2019 Page 2 of 7 Assignment #3 How to read an entire line of text:
You will need the command getline(). Refer to your textbook for details if required.
See a sample output screen below. Your output does not need to be formatted in exactly the
same way. Just make sure the required information appears.
Programming Exercise #2 – Nested Loop & User Defined Function (9 pts)
Write a C++ program that prints one of the following patterns based on the user input of an integer value for the size of the pattern, which must be between 1 and 20 inclusive. This question requires the use of functions which are described at the end of the question.
A) If the user enters any invalid number for the size, then the program should display a message indicating that input was invalid and request the user to reenter a correct value. This will however be allowed for a maximum of 6 times. At the 6th invalid number a message should be displayed indicating that the user has exceeded the maximum allowed times to enter a valid number. Your program should then end. Here is a screen shot to illustrate this behaviour.
Figure 1 – Behaviour when invalid numbers 6 times
Comp 218/Winter 2019 Page 3 of 7 Assignment #3
B) Once the size value is correctly entered, the program must find out whether the entered value was even, odd or 7, 13 or 19 which are considered as a special size, rather than just an odd number.
a. If the user enters an odd number (other than 7, 13 or 19), then the program would draw a pattern that is similar to the following: (Note: the example is for user input 5)
*****
****
***
**
*
b. If the user enters an even number, then the program would draw a pattern that is similar to the following: (Note: the example is for user input 8)
*
**
***
****
*****
******
*******
********
c. If the user enters one of the special numbers, 7, 13 or 19 then the program would draw a pattern that is similar to the following: (Note: the example is for user input 7)
1
123
12345
1235567
12345
123
1
Hint: Split the problem into the top half and bottom half.
Here are a few sample screen shots to illustrate the expected behavior of your program. User input is circled in green .
Comp 218/Winter 2019 Page 4 of 7 Assignment #3
Figure 2 – Sample output for odd number user input (other than 7, 13 or 19)
Figure 3 – Sample output for even number user input (other than 7, 13 or 19)
Comp 218/Winter 2019 Page 5 of 7 Assignment #3
Figure 4 – Sample output for special number user input
Note: Your output does not need to be formatted in exactly the same way. Just make sure the
required information appears.
To solve this problem, you are required to write and use the following functions. Here is a list of the functions and their specifications. You can create and use others functions as well if you wish.
1. Function Name: Header
Parameters: None
Return: none
Purpose: This function will display the welcome banner.
2. Function Name: getInput
Parameters: The variable that will hold the value entered by user, passing by
reference
Return: The integer number entered by the user.
Purpose: This function will allow the user to enter the number to be tested.
3. Function Name: validInput
Parameters: The variable that holds the number entered by the user.
Return: boolean
Purpose: This function returns true if the entered value is within the allowed
range and false otherwise.
Comp 218/Winter 2019 Page 6 of 7 Assignment #3
4. Function Name: drawPatternOdd
Parameters: The variable that holds the number entered by the user.
Return: none
Purpose: This function will display the pattern for odd numbers (other than 7, 13
and 19)
5. Function Name: drawPatternEven
Parameters: The variable that holds the number entered by the user.
Return: none
Purpose: This function will display the pattern for even numbers (other than 7,
13 and 19)
6. Function Name: drawPatternSpecial
Parameters: The variable that holds the number entered by the user.
Return: none
Purpose: This function will display the pattern for the special numbers 7, 13 and
19.
– Be sure to test your programs in Visual Studio.
– Zip the two source codes (.cpp files only) for the assignment.
– Naming convention for zip file: Create one zip file, containing all source files for your
assignment using the following naming convention:
The zip file should be called a#_studentID, where # is the number of the assignment studentID is your student ID number. So for the third assignment, student 123456 would submit a zip file named a3_123456.zip
What to Hand in for Assignment 3
Evaluation Criteria for Assignment 3
For Assignment 3 – 3 pts.
Comments – description of variables/ description of the steps in code/ purpose of program; Choice of variable names
1 pt.
Indentation and readability of program
1 pt.
Welcome and closing message ; Clear prompts to user & clear messages with output
1 pt.
Question 1 – 12 pts.
User input validation
1 pt.
Implementation of following functions:
Header
0.5 pts.
Comp 218/Winter 2019 Page 7 of 7 Assignment #3
get
Input
1 pt.
validInput
1 pt.
drawPatterOdd
1.5 pt.
drawPatternEven
1.5 pt.
drawPatternSpecial
2 pts.
Driver
3 pts.
Question 2 – 5 pts.
Reading from file (open, read, close)
1 pt.
Reversing read lines
3 pts.
Writing to a file
1 pt.