May 14, 2013 at 3:33am UTC
In this project, my input will be 2 strings consisting of the characters A, G, C, and T. For instance:
agatgctagatttcg
agcctcccgatagcc
and Count the number of positions at which 2 sequences (of equal length) differ.
truly appreciated .
May 14, 2013 at 3:42am UTC
inner_product works great for this kind of thing:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
#include <numeric>
#include <functional>
int main()
{
std::string s1 = "agatgctagatttcg" ;
std::string s2 = "agcctcccgatagcc" ;
int diffs = std::inner_product(s1.begin(), s1.end(), s2.begin(), 0,
std::plus<int >(), std::not_equal_to<char >());
std::cout << "The number of pairwise mismatches: " << diffs << '\n' ;
}
online live demo:
http://ideone.com/SV4MK5
(although, depending on what you're studying you may be expected to write a loop, for example,
for (size_t n = 0; n < s1.size(); ++n)
, which increments a counter whenever
s1[n] != s2[n]
)
Last edited on May 14, 2013 at 3:45am UTC
May 14, 2013 at 3:48am UTC
cubbi, you re the best!! thank you so much for the help.
May 14, 2013 at 3:55am UTC
hey, cubby ,
one more question,
how do I Allow the strings to be of different lengths. In this case, it works as follows:
GCCGTAA
GCCG
These have a Hamming Distance of 3, because the "TAA" at the end is not matched in the 2nd string.
TGGC
TAGCAGG
These have a Hamming Distance of 4: Positions 1,4,5,6 don't match.