Yu Zhou Yu Zhou - 2 months ago 5
C++ Question

Value didn't get passed by the for loop

We are implementing a function where it would compare user's input string to a fixed string variable. If the user inputs 4 letters, then we would compare these 4 letters from the very first index and loop up until the end. After my implementation, I have trouble getting the right result for variable "highest_score". I tried adding a print statement right above the return value to test out the value for highest_score, and I received 0 as the result. So I was thinking maybe the intended value didn't get pass by the for loop, and I wonder if someone could help me understand why the value didn't get pass by the for loop, and tips of fixing it! Thank you very much.

/*********************************************************************************
calcSimilarity() function will take two arguments that are both strings. The
function calculates the Hamming distance and returns the similarity score. This
function should only calculate the similarity if the two strings are the same length,
otherwise return 0.
**********************************************************************************/
float calcSimilarity (string str_1,string str_2){
float hammer_distance;

/*Lenth check*/
if (str_1.length() != str_2.length()){
cout << "String length not equal, please enter again: " << endl;
}

/*Hammer distance*/
for (int i = 0; i < str_1.length(); i++)
{
if (str_1[i] != str_2[i]){
hammer_distance += 1;
}
}

/*Similarity score*/
float similarity_score = (str_2.length() - hammer_distance)/str_2.length();


return similarity_score;
}

/***********************************************************************************
compareDNA() function should take two arguments that are both strings. The
function should calculate the similarity score for each substring of the DNA
(substring should be same length as user_input) and return the best similarity
score found across all the possible substrings. Use the calcSimilarity() function
described above.
**********************************************************************************/
float compareDNA(string DNA, string user_input){
float current_score = 0;
float highest_score = 0;
float final_score;
string substring;
/*Loop through each segment and calculating for the highest score*/
for (int i = 0; i < DNA.length()-user_input.length(); i++){
substring = DNA.substr(i,user_input.length());
current_score = calcSimilarity(user_input,substring);
if (current_score > highest_score){
highest_score = current_score;
}
}
cout << highest_score << endl;
return highest_score;
}

Answer

Correct code:

/*********************************************************************************
calcSimilarity() function will take two arguments that are both strings. The 
function calculates the Hamming distance and returns the similarity score. This 
function should only calculate the similarity if the two strings are the same length, 
otherwise return 0.
**********************************************************************************/
float calcSimilarity (string str_1,string str_2){
    float hammer_distance = 0; // Initialize this variable

    /*Lenth check*/
    if (str_1.length() != str_2.length()){
        cout << "String length not equal, please enter again: " << endl;
    }

    /*Hammer distance*/
    for (int i = 0; i < str_1.length(); i++)
    {
        if (str_1[i] != str_2[i]){
            hammer_distance += 1;
        }
    }

    /*Similarity score*/
    float similarity_score = (str_2.length() - hammer_distance)/str_2.length();  


    return similarity_score;
}

/***********************************************************************************
compareDNA() function should take two arguments that are both strings. The 
function should calculate the similarity score for each substring of the DNA
(substring should be same length as user_input) and return the best similarity 
score found across all the possible substrings. Use the calcSimilarity() function 
described above.
**********************************************************************************/
float compareDNA(string DNA, string user_input){
    float current_score = 0;
    float highest_score = 0;
    float final_score;
    string substring;
    /*Loop through each segment and calculating for the highest score*/

    // Use i <= instead of i < 
    for (int i = 0; i <= DNA.length()-user_input.length(); i++){
        substring = DNA.substr(i,user_input.length());
        current_score = calcSimilarity(user_input,substring);
        if (current_score > highest_score){
            highest_score = current_score;
        }
    }
    cout << highest_score << endl;
    return highest_score;
}
Comments