//-------------------------------------------------------------------- // // Lab
ID: 3799924 • Letter: #
Question
//--------------------------------------------------------------------
//
// Laboratory 3, In-lab Exercise 1 test3dna.cpp
//
// Test program for the countbases function
//
//--------------------------------------------------------------------
// Reads a DNA sequence from the keyboard, calls function countBases
// countBases (which uses a list to represent a DNA sequence), and
// outputs the number of times that each base (A, G, C and T) occurs
// in the sequence.
#include <iostream>
#include "ListArray.cpp"
using namespace std;
//--------------------------------------------------------------------
//
// Function prototype
//
void countBases ( List<char> &dnaSequence,
int &aCount,
int &cCount,
int &tCount,
int &gCount );
//--------------------------------------------------------------------
int main ()
{
List<char> dnaSequence(25); // DNA sequence (25 bases max.)
char base; // DNA base
int aCount, // Number of A's in the sequence
cCount, // Number of C's in the sequence
tCount, // Number of T's in the sequence
gCount; // Number of G's in the sequence
// Read the DNA sequence from the keyboard.
cout << endl << "Enter a DNA sequence: ";
cin.get(base);
while ( base != ' ' )
{
dnaSequence.insert(base);
cin.get(base);
}
// Display the sequence.
cout << "Sequence: ";
if( dnaSequence.isEmpty() )
cout << "list is empty" << endl;
else
{
dnaSequence.gotoBeginning();
do
{
cout << dnaSequence.getCursor() << " ";
} while ( dnaSequence.gotoNext() );
cout << endl;
}
// Count the number of times that each base occurs.
countBases(dnaSequence,aCount,cCount,tCount,gCount);
// Output the totals.
cout << "Number of A's : " << aCount << endl;
cout << "Number of C's : " << cCount << endl;
cout << "Number of T's : " << tCount << endl;
cout << "Number of G's : " << gCount << endl;
}
//--------------------------------------------------------------------
//
// Insert your countBases function below.
//
Explanation / Answer
The countBase function can be defined as below:
Code:
void countBases ( List<char> &dnaSequence, int &aCount, int &cCount, int &tCount, int &gCount)
{
dnaSequence.gotoBeginning();
aCount = 0;
cCount = 0;
tCount = 0;
gCount = 0;
char c;
do
{
c = dnaSequence.getCursor();
switch(c)
{
case 'A' : aCount++; break;
case 'C' : cCount++; break;
case 'T' : tCount++; break;
case 'G' : gCount++; break;
}
}
while(dnaSequence.gotoNext() );
}
Description:
You can implement this function the way displaying the list is done here, even if you are not given the header file, still you can do it by just seeing implementation.
You have to implement a method which counts number of A,C,T and G bases present in the string input by user. You already have that sequence in dnaSequence. You need to traverse it by every element and check it and accordingly need to increase the counter right?
Here, in the function, all the variables are passed as reference, so whatever we change in the function arguments, they are directly reflected in the variables passed in the function.
I have initialized every counter with 0 because we don't know whether they were initialized or not. So, this is needed. c is used to get the base entry from dnaSequence.
To display the list, what is used here? A do-while loop. So, by a method gotoBeginning() we can go to the starting of the sequence. I am taking the base from sequence using c = dnaSequence.getCursor();
After that, a switch-case code is checking what is the value in c and based on that, it increments the appropriate counter by 1.
Now, this switch block doesn't require a default statement because the value of 'c' is going to be among these 4 only.
After 1 character is performed, the next character is get by dnaSequence.gotoNext(). This continues till the dnaSequence has some entries.
This how, we have got through every character and counted its value and as they are passed using reference, no need to return anything because modifying the function parameters is the same as actual parameters when passed using & i.e. reference.
As we have to return nothing, the return type is void.
Do comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.