Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given two DNA strands of equal length, write a complete C++ program to compare t

ID: 3873125 • Letter: G

Question

Given two DNA strands of equal length, write a complete C++ program to compare the corresponding bases, and report mismatches. For example, suppose the input to your program are the following two DNA strands: CCATGGTC CCAGTGAC then your program should produce the following output: CCATGGTC CCAGTGAC **Differences: 3 In other words, your program should output the first strand, then on the next line output a space if the corresponding bases are the same and an X' if not, followed by the second strand, followed by a summary of the # of differences if the strands are identical. the #of differences reported should be 0. Don't forget to #include so you have access to the string functionality in C++ [Hint: this is not as hard as it looks. Input the 2 DNA strands, output the first, then loop over the strings like you have done in the other exercises. But instead of comparing the ith character to a known letter such as C'or G', compare to the ith character in the other string. Count and output as you loop, then after the loop output the 2nd DNA strand and the # of differences. ]

Explanation / Answer

#include <iostream>
using namespace std;

int main() {
// declaring variables
string a, b;
int count = 0;
  
// taking user input
cout << "Enter DNA 1: ";
cin >> a;
cout << "Enter DNA 2: ";
cin >> b;
  
// printing first strand
cout << endl << a << endl;
  
// looping through each character
for(int i=0;i<a.length();i++)
{
// checking if they are equal
if(a[i]==b[i])
cout << " ";
// if not, increasing count by 1 and printing X
else
{
count++;
cout << "X";
}
}
  
// printing second strand
cout << endl << b << endl;
cout << "**Differences: " << count << endl;
}

SAMPLE OUTPUT

#include <iostream>
using namespace std;

int main() {
// declaring variables
string a, b;
int count = 0;
  
// taking user input
cout << "Enter DNA 1: ";
cin >> a;
cout << "Enter DNA 2: ";
cin >> b;
  
// printing first strand
cout << endl << a << endl;
  
// looping through each character
for(int i=0;i<a.length();i++)
{
// checking if they are equal
if(a[i]==b[i])
cout << " ";
// if not, increasing count by 1 and printing X
else
{
count++;
cout << "X";
}
}
  
// printing second strand
cout << endl << b << endl;
cout << "**Differences: " << count << endl;
}

SAMPLE OUTPUT

  Enter DNA 1:  CCATGGTC  Enter DNA 2:  CCAGTGAC    CCATGGTC     XX X   CCAGTGAC  **Differences: 3  
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote