Programming Problem * Edit Nov. 23rd: SameGene is allowed to take 3 parameters (
ID: 3634254 • Letter: P
Question
Programming Problem* Edit Nov. 23rd: SameGene is allowed to take 3 parameters (2 char arrays and 1 int for the array size)
DNA analysis is an important part of medical exploration and
discovery. In this assignment^*, we examine both hemoglobin genes for
each of four people, with each gene consisting of 444 DNA bases
(A,C,T,G). These are the four bases (letters) that make up DNA.
Sickle-cell anemia is a disorder that is caused by a single mutation
in the hemoglobin gene.
A sickle hemoglobin gene has a T in the 20th position.
A person is anemic if he/she has two sickle hemoglobin genes.
A person is a carrier if he/she has one sickle hemoglobin gene.
Otherwise, the person is normal.
The input file, called dna.txt and found on my website, contains the DNA of
the two hemoglobin genes of four people. The file contains the gene DNA
information in this order:
gene1A
gene2A
gene1B
gene2B
gene1C
gene2C
gene1D
gene2D
where 1 and 2 denote the first and second gene, and A,B,C,D denote people.
The required information is found in the first 3552 characters of dna.txt,
that is, there is no whitespace before or between the eight genes.
Create a report, called DNAanalysis.txt, that indicates whether each person
is anemic, a carrier, or normal. For example,
Person X is anemic.
Person Y is normal.
Person Z is a carrier.
Two individuals are related if they share at least one gene.
Determine whether any of the above four individuals are related
and indicate this in DNAanalysis.txt. For example,
Person X is related to person Y.
Include a subfunction, called SameGene, that takes two character arrays
and returns true if they are the same gene; and returns false, otherwise.
Hand in your C++ program and output, making sure to use a staple and
to clearly indicate your name and student number on the cover sheet.
Hint: you may want to use eight arrays of type char and of size 444.
* Thanks to Dr. John Stavrinides in the Biology Department for creating this question.
"What's so beautiful about DNA is that its turned Biology
into a kind of branch of Computer Science"
Richard Dawkins, a biologist at Oxford University and a fellow of the Royal Society, in The Genius of Charles Darwin.
Explanation / Answer
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
void get_genes(char a1[], char a2[], char b1[], char b2[], char c1[],
char c2[], char d1[], char d2[]);
void results(char a1[], char a2[], char b1[], char b2[], char c1[],
char c2[], char d1[], char d2[]);
bool has_mutation(char data[]);
bool same_gene(char data1[], char data[]);
//--------------------------------------------------------------------
int main()
{
char a1[444], a2[444], b1[444], b2[444], c1[444], c2[444],
d1[444], d2[444];
get_genes(a1, a2, b1, b2, c1, c2, d1, d2);
results(a1, a2, b1, b2, c1, c2, d1, d2);
return EXIT_SUCCESS;
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
bool same_gene(char data1[], char data2[])
{
int i;
for(i = 0; i < 444; ++i)
{
if(!(data1[i] == data2[i]))
{
return(false);
}
}
return(true);
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
bool has_mutation(char data[])
{
cout << data[19] << endl;
return(data[19] == 'T');
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
void get_genes(char a1[], char a2[], char b1[], char b2[], char c1[],
char c2[], char d1[], char d2[])
{
int i;
ifstream in_stream;
char all[3552];
in_stream.open("dna.txt");
if (in_stream.fail())
{
cout << "Input file opening failed. ";
exit(1);
}
in_stream.getline(all, 3552);
for(i = 0; i < 444; ++i)
{
a1[i] = all[i];
a2[i] = all[i + 444];
b1[i] = all[i + 888];
b2[i] = all[i + 1332];
c1[i] = all[i + 1776];
c2[i] = all[i + 2200];
d1[i] = all[i + 2644];
d2[i] = all[i + 3108];
}
in_stream.close();
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
void results(char a1[], char a2[], char b1[], char b2[], char c1[],
char c2[], char d1[], char d2[])
{
ofstream out_stream;
out_stream.open("DNAanalysis.txt");
if (out_stream.fail( ))
{
cout << "Output file opening failed. ";
exit(1);
}
if(has_mutation(a1) && has_mutation(a2))
{
out_stream << "Person A is anemic." << endl;
}
else if(has_mutation(a1) || has_mutation(a2))
{
out_stream << "Person A is a carrier." << endl;
}
else if(!(has_mutation(a1) && has_mutation(a2)))
{
out_stream << "Person A is normal." << endl;
}
if(has_mutation(b1) && has_mutation(b2))
{
out_stream << "Person B is anemic." << endl;
}
else if(has_mutation(b1) || has_mutation(b2))
{
out_stream << "Person B is a carrier." << endl;
}
else if(!(has_mutation(b1) && has_mutation(b2)))
{
out_stream << "Person B is normal." << endl;
}
if(has_mutation(c1) && has_mutation(c2))
{
out_stream << "Person C is anemic." << endl;
}
else if(has_mutation(c1) || has_mutation(c2))
{
out_stream << "Person C is a carrier." << endl;
}
else if(!(has_mutation(c1) && has_mutation(c2)))
{
out_stream << "Person C is normal." << endl;
}
if(has_mutation(d1) && has_mutation(d2))
{
out_stream << "Person D is anemic." << endl;
}
else if(has_mutation(d1) || has_mutation(d2))
{
out_stream << "Person D is a carrier." << endl;
}
else if(!(has_mutation(d1) && has_mutation(d2)))
{
out_stream << "Person D is normal." << endl;
}
if(same_gene(a1, b1) || same_gene(a1, b2) || same_gene(a2, b1) ||
same_gene(a2, b2))
{
out_stream << "Person A is related to Person B." << endl;
}
if(same_gene(a1, c1) || same_gene(a1, c2) || same_gene(a2, c1) ||
same_gene(a2, c2))
{
out_stream << "Person A is related to Person C." << endl;
}
if(same_gene(a1, d1) || same_gene(a1, d2) || same_gene(a2, d1) ||
same_gene(a2, d2))
{
out_stream << "Person A is related to Person D." << endl;
}
if(same_gene(b1, c1) || same_gene(b1, c2) || same_gene(b2, c1) ||
same_gene(b2, c2))
{
out_stream << "Person B is related to Person C." << endl;
}
if(same_gene(b1, d1) || same_gene(b1, d2) || same_gene(b2, d1) ||
same_gene(a2, d2))
{
out_stream << "Person B is related to Person D." << endl;
}
if(same_gene(c1, d1) || same_gene(c1, d2) || same_gene(c2, d1) ||
same_gene(c2, d2))
{
out_stream << "Person C is related to Person D." << endl;
}
out_stream.close();
}
//-------------------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.