2016-09-23 9 views
0

ユーザーの入力文字列を固定文字列変数と比較する関数を実装しています。ユーザーが4文字を入力すると、最初のインデックスから4文字を比較し、最後までループします。私の実装後、変数 "highest_score"に適切な結果を得ることができません。私は、戻り値のすぐ上にあるprint文を追加して、highest_scoreの値をテストしようとしましたが、結果として0が返されました。だから、意図した値がforループによって渡されなかったのかもしれないと思っていたのですが、なぜ誰かが値がforループとそれを修正するヒントを渡さない理由を理解する助けになるのだろうかと思います。どうもありがとうございました。forループによって値が渡されなかった

/********************************************************************************* 
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; 
} 
+1

あなたも)(calcSimilarityのコードを投稿することができますか? – user3286661

+0

ok、ちょうどそれを更新しました! –

+4

'hammer_distance'が初期化されていません –

答えて

1

正しいコード:

/********************************************************************************* 
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; 
} 
+0

ありがとう.... –

関連する問題