Write a program that find the shortest ladder

Assignment Help Programming Languages
Reference no: EM132333613

Assignment - This assignment provides practice with pointers, lists, file I/O, sorting, queues, and recursion.

Introduction - For this assignment, you'll be using a database of English five-letter words. This list has about 5,800 words in it. You can also make your own data files: one 5-letter word per line. You'll write a program to find word-ladders: connections from one word to another formed by changing one letter at a time with the constraint that each time a letter is changed, the resulting string of letters is a word.

For example, to turn stone into money, one possible ladder is (replace 't' by 'h', replace 'o' by 'i', etc.):

stone

shone

shine

chine

chins

coins

corns

cores

cones

coney

money

All of these words can be found in the file and in a dictionary. Your program will find the shortest word ladder between two words you enter when running the program. There may be more than one shortest ladder, your program must find one, but not all such ladders.

You are to write a program named ladderq.cpp that uses a file of 5-letter words to find the shortest ladder from one word to another using a process outlined below. You must develop a class to do this, the class Ladder has been started for you, but you will need to add MORE member functions (both public and private).

Your program should:

1. Read and store the words from a file (specified by the user).

2. Prompt the user to enter two five-letter words (and continue to prompt). For each pair of words, output to the screen AND to an output file, wordladders.txt, a shortest ladder from the first word to the second. If one of the words is not of length 5, your program should halt.

Coding/Algorithm - To find the shortest ladder, you should use the templated Queue class that we discussed in class. First, store all of the words from the file in a vector of type Wnode * (this is done in LoadWords).

struct Wnode

{

string word;

Wnode * prev;

};

The vector is diagrammed as:

A ladder is found by putting the starting word (or rather a pointer to the Wnode that the word is in) on the queue, then putting all words 1 letter away from this word on the queue, then putting all words 2 letters away on the queue, then all words 3 letters away, etc. As each word is taken off the queue, if the last (target) word is found the process can stop (there may be other words on the queue, but they'll be ignored). This process is guaranteed to find the shortest word ladder.

A Word w isn't actually stored on the queue, a pointer to a struct containing w is stored. The other field of the struct is a pointer to the word that is one letter away from w and that caused w to be put on the queue (the word's predecessor). For example, if w is bears, then pointers to structs containing dears, fears, gears, beard (and so on) are enqueued with each struct pointing to bears since this word preceeded the others and caused them to be enqueued. The first word doesn't have a predecessor. It's field cannot be 0/NULL since this is used for another purpose. An easy fix is to make the pointer self-referential, it points to the struct itself (and this will need to be checked when printing ladders).

More Details - The first word (entered by the user) is looked up in the list of words, and a pointer to the struct containing the word is enqueued. Your program should be able to handle a first word even if the word is NOT in the list of words (all other words in the ladder, except perhaps for the last, must be in the list of words).

Put a pointer to the struct containing the first word onto the queue (it's a queue of Wnode pointers). So, let's assume, based on the example above, that we would like to build a word ladder where our starting word is bears and our target word is deals. We begin by first enqueueing the pointer to the Wnode containing the word bears. To be on the safe side, you should change bears Wnode's prev pointer to point to itself. The reason for this will become apparent as you continue. Once you have done so, you must then repeat the 'dequeue/enqueue' process listed below to build the ladder from bears to deals.

1) Dequeue an element from your queue (remember, what you are actually dequeueing is a pointer to a Wnode). This means, we dequeue the pointer to the Wnode containing bears. Why bears? Because so far, it is the only Wnode that has been enqueued! Now, we find each word that is one letter apart from the dequeued word (bears). In the example above, we find that the words which are one letter apart from bears are - dears, beard, fears and gears. Each time you find a word that is one letter apart from bears:

a. Check to see whether the current word is the target word (in this example, if it is the word deals). If one of these is the target word, you're done (or if one of the words is one apart from the target word you're done, you can stop early).

2) Otherwise, if each of the one apart words (dears, beard, fears and gears) isn't the target word deals or one apart from deals, enqueue each of them as long as they have not been queued up before (you can use the prev pointer fields in a Wnode to determine if a word has been enqueued before --- initially all prev fields should be set to 0/NULL, this helps determine if a word has been enqueued before). If a word's Wnode's prev pointer is 0/NULL set that word's Wnode's prev pointer to the address of the word that it is one letter apart from (in this example, it would point to the Wnode containing bears) and then enqueue it. This means each word is enqueued at most once. Continuing from our example, since none of the one-letter-apart words (dears, beard, fears and gears) is our target word deals, we must enqueue all of them and then repeat the process. However, the next time around when you dequeue, the word that will come up will be dears. Note that we could have stopped once we found the word dears because it is one apart of deals. However, continuing would still result in the same ladder because by enqueuing dears, we would end up finding deals which it is one apart from.

When the target word is derived, you'll need to print out the ladder from the first word to the target word. The prev pointer in the Wnode stores information that will allow the ladder to be recreated, you may need to use recursion or a vector since the ladder will be backwards (but should be printed properly). You could also search backwards, so to find a ladder from smart to brain search from brain to smart. OR, YOU CAN USE A STACK AND THEN POP which will simply reverse the items automatically. I prefer if you take the stack or recursive approach.

Ladder Member Functions -

You must implement the functions described below. You'll find it useful to implement other member functions. Sometimes the functions should be private. This is the case when a member function is a helper function for other member functions, but shouldn't be called by the user. Making a helper function private ensures that only other member functions can access the helper function, but client programs cannot.

  • Clear(), sets all prev fields to 0/NULL.
  • FindLadder(), tries to find a ladder between two words that are parameters to the function. FindLadder can return a boolean (and set some state in the Ladder class) or it can return a pointer. It makes more sense to return a boolean and store the beginning of the ladder (if one was found) in a private variable that can be accessed by the function Print. Pass the strings to FindLadder as const reference parameters.
  • Print prints the word ladder. Private data may be needed to store the last node of the ladder (the prev field of the last node can be used to access all other ladder nodes, the first node in the ladder has a self-referential pointer).

You'll probably find it useful to write a function IsOneApart that is used to determine if two strings are one letter apart. To do this, count the letters that are equal. If this is one less than the total number of letters in the words, the words are one apart. This function does NOT need to be a member function, it has two strings as parameters (const reference) and returns true if the strings are one letter apart. You can just define this function in ladder.cpp and use it there.

You may want to write a separate function to find a word in the vector of Wnode * read and constructed using the function Ladder::LoadWords. The function can be useful in debugging and developing the program.

You may find it useful to write a function that gives back all the words that are one letter apart from a given word. This list of words could be stored in a vector or the List class (random access isn't needed). It's not at all necessary to do this, and there is no grade improvement, but it may be helpful.

Attachment:- Topics Combined Assignment Files.rar

Reference no: EM132333613

Questions Cloud

Write the infix-to-postfix conversion algorithm : You will write a C++ version of the infix-to-postfix conversion algorithm. You will discover that code you write to do this exercise
Explain the features of effective team performance : LM1c-H/602/3171-Lead and Manage a Team within a Health and Social Care or Children and Young People’s Setting-Pearson Edexcel Level 5 Diplomas.
Research interspecific and intraspecific interactions : Research interspecific and intraspecific interactions using the module readings, the Argosy University online library resources, and the Internet.
How the frameworks are used to improve the life chances : P4-A/602/3175-Lead and Manage Group Living for Children-Pearson Edexcel Level 5 Diplomas in Leadership for Health and Social Care and Children and Young People.
Write a program that find the shortest ladder : You are to write a program named ladderq.cpp that uses a file of 5-letter words to find the shortest ladder from one word to another
The relationship between humans and their environment : Which began in the eighteenth century, has had an ongoing influence on society as well as the relationship between humans and their environment.
Explain how the processes used by own work setting comply : MU5.4-A/601/9370-Develop and Implement Policies and Procedures to Support the Safeguarding of Children and Young People.
Write the method how can we calculate it : Hypothesis - You just have to write the method how can we calculate it - Interpret the result of the test of null hypothesis
Intertidal organisms for coping with low temperatures : BIOLOGY 181-All of these are adaptations of intertidal organisms for coping with low temperatures. The skeleton of hagfish, lampreys, and sharks is composed of.

Reviews

Write a Review

Programming Languages Questions & Answers

  Design and write payroll program-employee-s hourly pay rate

Design and write a payroll program that will prompt the user to enter an employee's hourly pay rate and the number of hours worked.

  Card generator program by linked list

Address Book-Card Generator Program Using a Linked List. This program will have names and addresses saved in a linked list. In addition, a birthday and anniversary date will be saved with each record.

  Express javascript code that called to validate text-field

Write a JavaScript function to validate a text-field on a form that is to hold an email address.

  Create ajax-based product catalog

Create an AJAX-based product catalog which obtains its data from JSON files located on the server. The data should be separated into four JSON files.

  What graphical element appears on a shortcut icon

Which of the following locations is not a valid place from which to delete a file and send it to the Recycle Bin?

  Prepare a logic model for a program of your choice

Topic: Francis-hill. Prepare a logic model for a program of your choice

  Define class that contains member variable of type string

Define a class named Document that contains a member variable of type String named text that stores any textual content for the document.

  Create a database with a table called "tblprofiles"

Use a RegularExpressionValidator control on the start page that displays an error message if the user does not enter a properly formatted social security number.

  Write organized sequential code with no errors

Real application Assignment - Write organized, sequential code with no errors for dataset comparisons. The results to be generated on main screen

  Applyfunction that receives an array and a function

Write a function called applyFunction that receives an array (arr) and a function (func) as a parameter.

  Provide unoptimized-optimized prefix using recursive rule

Where the recursive rule uses concatenation of strings, so F2 is "ab", F3 is "aba". Note that the length of Fn is the nth Fibonacci number. Provide unoptimized and optimized ‘prefix' (fail) function for F7.

  Write a secure peer-to- peer file-sharing system

CS-452 - Project - implement a system which enables a group of users to chat securely. All users are registered with the chat server. When the user wants

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd