dna:Pls help for this programing problem in c++ (standard IO without file read/w
ID: 3915197 • Letter: D
Question
dna:Pls help for this programing problem in c++ (standard IO without file read/write),better with some comment about why coding that way,thanks
Problem C: The DNA Sequence time limit: 2 sec Problem Description DNA of a creature can be thought of as a sequence of 4 characters of A, T, C and G. Because the length of the sequence of DNA is too long to be studied, we hope to find some more important segments of the sequence. Some segments that have the maximal density of C or G are interesting to biologists but the segment can't be too short. We request you write a program to find a segment that has the most density of C or G. A substring of the string is a consecutive segment of the string. If the length of substring is L and the number of C or G is w. The CG-density of the substring is defined as wIL. The input of the program has an integer L (between 5 and 40) and a string of DNA. The program must find a substring of length at least L and the CG-density is maximal. The string of the input is less than 120 and this string consists of only A, T, C and G but mixed with upper or lower cases (e.g. ATCGatcg). Input File Format The input has several lines, the first line contains an integer which indicates the number of test cases. In each case, the first integer is L, and followed by the string of DNA. Output Format For each case, output the maximal density to three digits after decimal point. Example Sample Input: Sample Output: 833 414 5 agGCTGCAatGACAGTTGGG 20 AaggctatacagtactaatCtTtTgcatggcExplanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
input file: dna.txt
-------------
2
5 agGCTGCAatGACAGTTGGG
20 AaggctatacagtactaatCtTtTgcatggc
main.cpp
--------
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>
#include <iomanip>
using namespace std;
double CG_density(string s); //get the CG density of input string s
double CG_maximal(int len, string s); //get maximal CG density for substring of length len in input string s
int main(){
string filename;
int n;
cout << "Enter input filename: ";
cin >> filename;
ifstream infile(filename.c_str());
if(infile.fail())
{
cout << "ERROR: could not open input file " << filename << endl;
return 1;
}
infile >> n; // get no. of test cases
int len;
string dna;
cout << fixed << setprecision(3);
for(int i = 0; i < n; i++){
infile >> len >> dna;
cout << CG_maximal(len, dna) << endl;
}
infile.close();
}
double CG_density(string s) //get the CG density of input string s
{
double W = 0;
int L = s.length();
char ch;
for(int i = 0; i < s.length(); i++){
ch = toupper(s[i]);
if(ch == 'C' || ch == 'G')
W = W + 1;
}
double cgDen = W/L;
return cgDen;
}
double CG_maximal(int minLen, string s) //get maximal CG density for substring of length len in input string s
{
double maxCGdensity = 0;
double density;
int lastIndex;
for(int len = minLen; len <= s.length(); len++){
lastIndex = s.length() - len;
for(int i = 0; i <= lastIndex; i++){
density = CG_density(s.substr(i, len));
if(density > maxCGdensity)
maxCGdensity = density;
}
}
return maxCGdensity;
}
output
======
Enter input filename: dna.txt
0.833
0.414
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.