Unformatted Attachment Preview
EGR125
Introduction to Engineering Methods (C++)
File: N125P4B
Due date: _______________________
Programming Assignment #4: Text Messaging
Background:
Text messaging on cell phones is sometimes configured so that when the user
types a sequence of number keys, possible words appear. The numbers on the
keypad correspond to the following letters:
2 – abc
3 – def
4 – ghi
5 – jkl
6 – mno
7 – pqrs
8 – tuv
9 – wxyz
For example, if the user enters the sequence 368253 the word “double” should appear.
However, if the user enters the sequence 4663, several possible words could be formed, including “home”,
“good”, “gone”, “hood”, “hone”, “hoof”, and “goof.” The user might be given a way to select from this list of
potential words. To keep this assignment from becoming too complicated, we will avoid punctuation and
special symbols.
US Dictionary:
A text file named USDictionary.txt is provided on the instructor’s web page. This file contains over 118,000
words representing the English language. This dictionary is not arranged like most dictionaries, but instead lists
words as follows:
- All 1-letter words (in alphabetical order)
- All 2-letter words (in alphabetical order)
- All 3-letter words (in alphabetical order)
- Etc
Program Description
Write a C++ program that begins by giving the user three options:
1) Enter a word and convert it to a sequence of numbers (this should be easy!)
Example: User input: Buffalo
Output: 2833256
2) Enter a sequence of numbers and display a list of all possible words in USDictionary.txt that match. Note
that since the longest word in USDictionary.txt is 45 letters in length, the input number sequence must be a
string (since no integers in C++ can handle 45 digits).
Example 1: User input:
368253
Output:
double
– no parentheses are needed if there is only one word that matches
Example 2: User input:
Output:
4663
(good, gone, hood, hone, hoof, good)
– use parentheses and separate the possible words by commas if two or
more words match
Page 2
3) Read a message from a text file (a sequence of numbers separated by white spaces) and display the message.
Give the user the choice of one of the 7 sample messages found on the instructor’s web site (plus two of
your own messages).
Example 1:
User Input: User selects Message 4 which contains 4 6333 2 639 7932874478
Output: I need a new sweatshirt
Example 2:
User Input: User selects Message 5 which contains 4 268448 2 8733 87825
Output: I bought a (used, tree) (truck, usual)
The program must also satisfy a number of specific requirements as listed below.
Program Requirements:
1) Write and use class KeyPad as described below:
Data members: Include data members for
o Number (a string consisting of a series of numbers)
o Word (a string consisting of a series of letters)
Member functions: Include member functions for
o Reading Numbers from the keyboard and checking if they are valid (2-9)
o Reading Words from the keyboard, converting to lowercase, and checking if they are valid
(lowercase a-z)
o Converting Words to Numbers
o Converting Numbers to Words and displaying all possible words from USDiscitionary.txt
o You can add additional member functions if you wish
Non-member functions: Include non-member functions for
o Converting words to all lowercase letters
o You can add additional non-member functions if you wish.
Main function:
o The main function should use class KeyPad and should give the user the three options as described
in the Program Description section.
o The user should be able to enter words with any combination of uppercase and lowercase letters.
o In addition to the 7 messages on the instructor’s web page (Message1.txt – Message7.txt), add two
messages of your own. Each message should have 6 or more words and should form a meaningful
sentence. One message should have only one possible solution. The other message should have
multiple solutions.
o Give an error message if a word is specified that is not in the dictionary
o Give the user the option of re-running the program
File structure: Use separate header and implementation files for class KeyPad.
Page 3
Output:
Turn in printouts for the following cases:
Option 1) Convert at least 5 words to numbers, including the words “baseball”, “engineer”, and three words of
your choice containing at least 5 letters
Option 2) Convert the following numbers into words (listing all possible words)
2429253
52925
776337767
4663
9355
5668
4653
Option 3) Convert all 9 messages to sentences (7 messages on the instructor’s web page and two of your own
as previously described).
Extra Credit Suggestions: (for a maximum of 10 additional points)
1.
If a word is not in the dictionary, give the user the option of adding it to a personal dictionary. Rather than
modifying USDictionary.txt, create a new dictionary (PersonalDictionary.txt) that words can be added to
each time the program is run. Be sure to search both dictionaries for valid words.
2. Add the use of punctuation in messages to the program.
3. Use your imagination!
EGR 125 - Introduction to Engineering Methods (C++)
File: N125-Ch13L
Due date: __________________
Chapter 13 Homework – Data Files
Reading Assignment:
Read Chapter 13in Introduction to Programming with C++, 3rd Edition by Liang
Problem Assignment:
Submit each of the following by the assigned due date.
1. (20 pts) Work Programming Exercise 13.3 (Process Scores in a Text File) in the textbook on page 518.
Instead of displaying the results on the screen, send the results to an output named using your last name.
Example:
Contents of Exercise13_3.txt:
100 95 88 97 71 67 80 81 82
Contents of YourLastName.txt:
Your first and last name
Total = 761
Average = 84.6
Run the program for the example above and for another example with 15 scores.
Turn in a printout of the program as well as printouts of the two input data files and the two output data files.
2. (21 points) Create a data file (using Notepad, perhaps) containing the values shown below (plus add 4 more lines of
your choice) and then write a C++ program as follows:
Read the data file (you can assume that it has exactly 8 lines with 3 numbers per line)
Calculate the three angles using the law of cosines (assume that the sides are valid)
Write the results to an output data file. The results should include headings and 2 digits after the decimal point
for all values. An example is shown below.
Print the program and the two data files.
Input data file:
20
5
10
50
15
12
10
60
10
13
14.142
70
Sides a, b, and c for Triangle 1
Pick values for 4 more valid triangles
Output data file:
Side a
20.00
Side b
15.00
Side c
10.00
Angle A(deg)
104.48
Angle B(deg) Angle C(deg)
46.57
28.96
3. (20 points) Download the file BigData.dat from the course Blackboard site. Write a C++ program to read the file
(assume that you do not know the number of values in the file) and determine (and display) each of the following:
The number of values in the file
The largest value in the file
The smallest value in the file
The number of negative numbers in the file
The number of values in the file that are between 100 and 200
4. (21 points) Create a data file containing around (but not exactly) 100 integers. The numbers should be on multiple
lines in the file, but do not include the same number of integers on each line of the file. Write a C++ program to read
the integers into an array with a max size of 200 (i.e., search for eof marker). The program should then determine and
display (on the computer screen) the average value of the numbers (a real value), the number of integers greater than
the average, and the number of integers less than the average. Print the program, the data file, and the output.
(Problem 5 is on the following two pages)
5. (18 pts) Determine by hand the output of each program listed on the following pages using the data files
shown below.
Contents of E:dat1.in:
Contents of E:dat2.in:
Contents of E:dat3.in:
2.0 3
4.0 5
6.0 7
9-15-20
acrtjprltoiu
owhfdrtnfres
dnzzpogquse
C++ Program
// Problem 5A
#include
#include
using namespace std;
int main ( void )
{
double x,y,z;
ifstream infile("E:dat1.in");
infile >> x >> y >> z;
cout c2>> c3 >> c4 >> c5;
cout > c1 >> c2>> c3 >> c4 >> c5;
cout > c1 >> c2>> c3 >> c4 >> c5;
cout c1 >> c2>> c3 >> c4 >> c5;
cout > c1 >> c2>> c3 >> c4 >> c5;
cout > c1 >> c2>> c3 >> c4 >> c5;
cout x >> y >> z;
cout