Part 1- Compare two sequences to each other float similarityScore (string sequen
ID: 3873277 • Letter: P
Question
Part 1- Compare two sequences to each other float similarityScore (string sequence1, string sequence2) Define the similarityScore function. This function takes two string arguments, calculates their Hamming distance, and returns their similarity score as a floating-point number. This function should only calculate the similarity if the two strings are the same length, otherwise return 0. You should implement your main function to repeatedly ask to the user for 2 strings (sequence1 and sequence2) such that you can pass them as parameters to your similarityScore function. Writing your own main function will allow you to test and debug every function you write. Example sequence! = CCGCCGCCGA sequence2 CCTCCTCCTA Matches = 7, Mismatches = 3 Hamming distance 3, Length 10 Similarity (0-3) /10 -.7 result = similarityScore(CCGCCGCCGA", coutExplanation / Answer
#include <iostream>
using namespace std;
float similarityScore(string sequence1, string sequence2)
{
int matches = 0, mismatches = 0;
// printing first strand
cout << endl << sequence1 << endl;
// looping through each character
for(int i=0;i<sequence1.length();i++)
{
// checking if they are equal
if(sequence1[i]==sequence2[i])
{
matches++;
cout << "|";
}
// if not, increasing count by 1 and printing X
else
{
mismatches++;
cout << "*";
}
}
// printing second strand
cout << endl << sequence2 << endl;
// printing output
cout << endl << "Matches = " << matches << ", Mismatches = " << mismatches<< endl;
cout << "Hamming distance = " << mismatches << ", Length = " << sequence2.length()<<endl;
// calculating similarity
float sim = (sequence2.length()-mismatches)*1.0/sequence2.length();
cout << "Similarity = (" << sequence2.length() << " - " << mismatches << ") / " << sequence2.length() << " = " << sim << endl;
return sim;
}
int main() {
// declaring variables
string a, b;
// taking input repeatedly
while(1)
{
// taking user input
cout << "Enter DNA 1: ";
cin >> a;
cout << "Enter DNA 2: ";
cin >> b;
// calling function and printing output of similarity
float result = similarityScore(a,b);
cout << result << endl << "-----" << endl << endl;
}
}
#include <iostream>
using namespace std;
float similarityScore(string sequence1, string sequence2)
{
int matches = 0, mismatches = 0;
// printing first strand
cout << endl << sequence1 << endl;
// looping through each character
for(int i=0;i<sequence1.length();i++)
{
// checking if they are equal
if(sequence1[i]==sequence2[i])
{
matches++;
cout << "|";
}
// if not, increasing count by 1 and printing X
else
{
mismatches++;
cout << "*";
}
}
// printing second strand
cout << endl << sequence2 << endl;
// printing output
cout << endl << "Matches = " << matches << ", Mismatches = " << mismatches<< endl;
cout << "Hamming distance = " << mismatches << ", Length = " << sequence2.length()<<endl;
// calculating similarity
float sim = (sequence2.length()-mismatches)*1.0/sequence2.length();
cout << "Similarity = (" << sequence2.length() << " - " << mismatches << ") / " << sequence2.length() << " = " << sim << endl;
return sim;
}
int main() {
// declaring variables
string a, b;
// taking input repeatedly
while(1)
{
// taking user input
cout << "Enter DNA 1: ";
cin >> a;
cout << "Enter DNA 2: ";
cin >> b;
// calling function and printing output of similarity
float result = similarityScore(a,b);
cout << result << endl << "-----" << endl << endl;
}
}
Enter DNA 1: CCG Enter DNA 2: CGC CCG |** CGC Matches = 1, Mismatches = 2 Hamming distance = 2, Length = 3 Similarity = (3 - 2) / 3 = 0.333333 0.333333 ----- Enter DNA 1: CCGCCGCCGA Enter DNA 2: CCTCCTCCTA CCGCCGCCGA ||*||*||*| CCTCCTCCTA Matches = 7, Mismatches = 3 Hamming distance = 3, Length = 10 Similarity = (10 - 3) / 10 = 0.7 0.7 -----