Question

I need help modifying this program. How would I make sure that the methods is being...

I need help modifying this program. How would I make sure that the methods is being called and checked in my main method?

Here is what the program needs to run as:

 GDVEGTA GVCEKST
The LCS has length 4
The LCS is GVET

This is the error that I'm getting:

The LCS has length 4 // I got this right

The LCS is   //the backtrace is not being called for some reason

c++ code:

the cpp class:

/**
* calculate the optimum edit distance between two strings
* @author Jerry Lin
* @version 13 April 2017
*/
#include <climits>
#include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include "matrix.h"
using namespace std;
typedef unsigned int uint;

// set infinity to one less than the maximum so we can add one to
// infinity and not overflow around to zero
const uint INFTY = UINT_MAX - 1;

/**
* find the minimum of three values
* @param a one of the three values
* @param b one of the three values
* @param c one of the three values
* @return the smallest value
*/

uint max3(uint a, uint b, uint c)
{
   uint result = a > b ? a:b;
   return result > c ? result : c;
}

/**
* the recursive, memoized optimum function for computing the edit distance
* between two strings
* @param s the first string
* @param i the index of the first string
* @param t the second string
* @param j the index of the second string
* @param memo the memo table
*/

uint opt( const string & s, uint i, const string & t, uint j,
          Matrix< uint > & memo )
{
   if(i==0 || j==0)
   {
      memo.at(i,j)=0;
   }
// base cases are built into the memo table with entries of INFTY
if( memo.at(i, j) == INFTY )
{
    if( s.at(i) == t.at(j) )
      memo.at(i, j) = opt( s, i - 1, t, j - 1, memo )+1;
    else
      memo.at(i, j) = max3( opt( s, i - 1, t, j, memo ) ,
                            opt( s, i, t, j - 1, memo ) ,
                            opt( s, i - 1, t, j - 1, memo ) );
}
return memo.at(i, j);
}


/**
* this is the backtrace method
*/

string backtrace(const string & s, const string & t, Matrix<uint> & memo,uint i, uint j)
{
    string longstring;

cout<<"hw"<<endl; //
while(i!=0 && j!=0)
{
    if(s.at(i) == t.at(j))
    {

       longstring.append(s,i,1);
       i--;
       j--;
    }

    else if(s.at(i) > t.at(j))
    {
       i--;
   
    }
    else
    {
       j--;
     
    }
  
}

//return the string for backtrack
//it returns the characters of the longest common sequence
//everyone of the characters of i and j are = so add that character to a string
//create a string then add characters to string
return longstring;

  
}

/**
* get the two strings from the command line
* set up the memo table
* print the results
*/
int main( int argc, char * argv [] )
{
if( argc != 3 )
{
    cerr << "Usage: " << argv[0] << " s1 s2 where s1 and s2 are the strings"
         << endl;
    return 1;
}

// add a space to the beginning of each string so the string indices
// will match the memo indices
string s = argv[1];
s = ' ' + s;
string t = argv[2];
t = ' ' + t;

// fill the memo table with infinities
Matrix< uint > memo( s.size() + 1, t.size() + 1 );
for( uint row = 1; row <= s.size(); row++ )
    for( uint col = 1; col <= t.size(); col++ )
      memo.at( row, col ) = INFTY;

// hard-code the base cases
for( uint row = 0; row <= s.size(); row++ )
    memo.at( row, 0 ) = row;
for( uint col = 0; col <= t.size(); col++ )
    memo.at( 0, col ) = col;

//uint result = opt( s, s.size() - 1, t, t.size() - 1, memo );
cout << "The LCS has length " << opt( s, s.size() - 1, t, t.size() - 1, memo ) <<endl;
cout << "The LCS is " << backtrace(s,t,memo,s.size()-1,t.size()-1) << endl;
string longstring = backtrace(s,t,memo,s.size()-1,t.size()-1);
cout<<longstring<<endl;
return 0;
}

matrix class-matrix.h:

/**
* A generic 2-dimensional array class
* @author Jon Beck
* @version 4 April 2017
*/
#ifndef MATRIX_H
#define MATRIX_H

#include <cassert>
typedef unsigned int uint;

template <class Object>
class Matrix
{
public:
Matrix( uint rows, uint cols );
Object & at( uint row, uint col );
const Object & at( uint row, uint col ) const;
~Matrix();
Matrix( const Matrix<Object> & m ); // Copy constructor
Matrix & operator= ( const Matrix<Object> & m ); // Assignment operator
uint numrows() const;
uint numcols() const;

private:
uint rows;
uint cols;
Object* data;
};

template <class Object>
Matrix<Object>::Matrix( uint rows, uint cols )
: rows( rows ), cols( cols )
{
data = new Object[ rows * cols ];
}

template <class Object>
Matrix<Object>::~Matrix()
{
delete[] data;
}

template <class Object>
Object & Matrix<Object>::at( uint row, uint col )
{
assert( row < rows && col < cols );
return data[ cols * row + col ];
}

template <class Object>
const Object & Matrix<Object>::at( uint row, uint col ) const
{
assert( row < rows && col < cols );
return data[ cols * row + col ];
}

template <class Object>
uint Matrix<Object>::numrows() const
{
return rows;
}

template <class Object>
uint Matrix<Object>::numcols() const
{
return cols;
}
#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI, I have fixed all the issue.

Program is working correctly.

/**
* calculate the optimum edit distance between two strings
* @author Jerry Lin
* @version 13 April 2017
*/
#include <climits>
#include <iomanip>
#include <iostream>
#include <vector>
#include <string>
#include "matrix.h"
using namespace std;
typedef unsigned int uint;
// set infinity to one less than the maximum so we can add one to
// infinity and not overflow around to zero
const uint INFTY = UINT_MAX - 1;
/**
* find the minimum of three values
* @param a one of the three values
* @param b one of the three values
* @param c one of the three values
* @return the smallest value
*/
uint max3(uint a, uint b, uint c)
{
uint result = a > b ? a:b;
return result > c ? result : c;
}

// Function to reverse a string
void reverseStr(string &str)
{
int n = str.length();

// Swap character starting from two
// corners
for (int i=0; i<n/2; i++)
swap(str[i], str[n-i-1]);
}

/**
* the recursive, memoized optimum function for computing the edit distance
* between two strings
* @param s the first string
* @param i the index of the first string
* @param t the second string
* @param j the index of the second string
* @param memo the memo table
*/
uint opt( const string & s, uint i, const string & t, uint j,
Matrix< uint > & memo )
{
if(i==0 || j==0)
{
memo.at(i,j)=0;
}
// base cases are built into the memo table with entries of INFTY
if( memo.at(i, j) == INFTY )
{
if( s.at(i) == t.at(j) )
memo.at(i, j) = opt( s, i - 1, t, j - 1, memo )+1;
else
memo.at(i, j) = max3( opt( s, i - 1, t, j, memo ) ,
opt( s, i, t, j - 1, memo ) ,
opt( s, i - 1, t, j - 1, memo ) );
}
return memo.at(i, j);
}

/**
* this is the backtrace method
*/
string backtrace(const string & s, const string & t, Matrix<uint> & memo,uint i, uint j)
{
string longstring;
//cout<<"hw"<<endl; //
while(i!=0 && j!=0)
{
if(s.at(i) == t.at(j))
{
  
//cout<<s[i];
longstring.append(s,i,1);
i--;
j--;
}

else if(memo.at(i-1, j) > memo.at(i, j-1))
{
i--;

}
else
{
j--;

}
  
}

// reversing string to
reverseStr(longstring);

//return the string for backtrack
//it returns the characters of the longest common sequence
//everyone of the characters of i and j are = so add that character to a string
//create a string then add characters to string
return longstring;

  
}
/**
* get the two strings from the command line
* set up the memo table
* print the results
*/
int main( int argc, char * argv [] )
{
if( argc != 3 )
{
cerr << "Usage: " << argv[0] << " s1 s2 where s1 and s2 are the strings"
<< endl;
return 1;
}
// add a space to the beginning of each string so the string indices
// will match the memo indices
string s = argv[1];
s = ' ' + s;
string t = argv[2];
t = ' ' + t;
// fill the memo table with infinities
Matrix< uint > memo( s.size() + 1, t.size() + 1 );
for( uint row = 1; row <= s.size(); row++ )
for( uint col = 1; col <= t.size(); col++ )
memo.at( row, col ) = INFTY;
// hard-code the base cases
for( uint row = 0; row <= s.size(); row++ )
memo.at( row, 0 ) = row;
for( uint col = 0; col <= t.size(); col++ )
memo.at( 0, col ) = col;
//uint result = opt( s, s.size() - 1, t, t.size() - 1, memo );
cout << "The LCS has length " << opt( s, s.size() - 1, t, t.size() - 1, memo ) <<endl;
cout << "The LCS is " << backtrace(s,t,memo,s.size()-1,t.size()-1) << endl;
return 0;
}

? thirdweek thirdweek thirdweek g++ matrix.cpp ? thirdweek ./a . out GDVEGTA GVCEKST The LCS has length 4 The LCS is GVET thirdweek

Add a comment
Know the answer?
Add Answer to:
I need help modifying this program. How would I make sure that the methods is being...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • I need help with the last method listed in the problem: Implement a class Grid that...

    I need help with the last method listed in the problem: Implement a class Grid that stores measurements in a rectangular grid. The grid has a given number of rows and columns, and a description string can be added for any grid location. Supply the following constructor and methods: public Grid(int numRows, int numColumns) public void add(int row, int column, String description) public String getDescription(int row, int column) public ArrayList getDescribedLocations() Here, Location is an inner class that encapsulates the...

  • I wrote program that have to block all moves for player 1, for game TicTacTOE AI,...

    I wrote program that have to block all moves for player 1, for game TicTacTOE AI, but it is not working. Can someone check what I have not eliminated? #include using namespace std; const int ROWS = 3; const int COLS = 3; int counter = 0; void aITurn(char b[][COLS]); void fillBoard(char board[ROWS][COLS]); void getChoice(char b[][COLS], bool playerToggle); bool gameOver(char b[][COLS]); void showBoard(char board[ROWS][COLS]); int main() {    ///main is complete, nothing to do here    char board[ROWS][COLS];    bool playerToggle =...

  • This program is in C++ which reserves flight seats. It uses a file imported to get...

    This program is in C++ which reserves flight seats. It uses a file imported to get the seat chart called "chartIn.txt" which looks like this 1 A B C D E F 2 A B C D E F It continues until row 10. Sorry unable to provide the chartIn.txt file. I am having issues with function displaySeats, it displays then but when it comes to 10 the row looks like this 1 0 A B C D E ,...

  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • Can I get help with adding the following to this code please? 1. When buying multiple...

    Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...

  • I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<i...

    I just need a help in replacing player 2 and to make it unbeatable here is my code: #include<iostream> using namespace std; const int ROWS=3; const int COLS=3; void fillBoard(char [][3]); void showBoard(char [][3]); void getChoice(char [][3],bool); bool gameOver(char [][3]); int main() { char board[ROWS][COLS]; bool playerToggle=false; fillBoard(board); showBoard(board); while(!gameOver(board)) { getChoice(board,playerToggle); showBoard(board); playerToggle=!playerToggle; } return 1; } void fillBoard(char board[][3]) { for(int i=0;i<ROWS;i++) for(int j=0;j<COLS;j++) board[i][j]='*'; } void showBoard(char board[][3]) { cout<<" 1 2 3"<<endl; for(int i=0;i<ROWS;i++) { cout<<(i+1)<<"...

  • Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise...

    Need C++ coding for this lab exercise given below :) 4. Lab Exercise — Templates Exercise 1 - Function Templating For this part of the lab make a template out of the myMax function and test it on different data types. Copy maxTemplate.cpp to your Hercules account space. Do that by: Entering the command:cp /net/data/ftp/pub/class/115/ftp/cpp/Templates/maxTemplate.cpp maxTemplate.cpp Compile and run the program to see how it works. Make a template out of myMax. Don't forget the return type. Modify the prototype appropriately. Test your myMax template on int, double,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT