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
  • 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 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,...

  • 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...

  • /** * 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...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • Hello, I am trying to write this program and have received a "Segmentation Fault" error but...

    Hello, I am trying to write this program and have received a "Segmentation Fault" error but cannot seem to figure out where. I haven't been able to find it and have been looking for quite a while. The goal is to basically create a version of Conway's Game of Life. I am able to generate a table if the input values for rows and columns are equal, but if they are not I receive a segmentation fault and cannot continue...

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
ADVERTISEMENT