简体   繁体   中英

My counter is not properly being divided by str.length(). C++

I just need some help dividing my counter of the Gs and Cs in the string/DNA strand and divide those by the length of the DNA strand to get a double representing the percentage of Cs and Gs in the string. However, when I divide counter by DNA.length() it returns 0. it doesn't give me the actual answer/ by product. Please help!!

    #include <iostream>
    #include<string>
    using std::string; using std::cout; using std::endl; using std::cin;

    double get_gc_content(const string& dna)
    {
      int counter = 0;

        for(std::size_t i = 0; i < dna.size();++i)
        {
            if(char(dna[i]=='C') || char(dna[i]=='G'))
            {
                counter++;
                // cout<<counter<<endl;     
            }

        }
      cout<<counter<<endl;

      double gc_content = counter / dna.length();

      return gc_content;
    }


    int main()
    {
      std::string dna = "ACCGCAAATT";

      double gc_count;

      gc_count = get_gc_content(dna);
      
      cout<<gc_count<<endl;

      return 0;
    } 

This line:

double gc_content = counter / dna.length();

performs integer division since both numerator and denominator are integer types. Thus the result will be truncated to an integer and stored in gc_content .

It doesn't matter if gc_content is a double -- the result of the division already occurred before the value is stored in gc_content . In your case, probably dna.lengh() is less than counter , thus you are getting a value of 0.0 .

The solution is to ensure that either counter or dna.length() is casted to a double , so that floating point division actually occurs.

double gc_content = static_cast<double>(counter) / dna.length();  // For example.

Look carefully at the following expression:

double gc_content = counter / dna.length();

You are dividing the integer value counter by another integer dna.length() and the result of this operation will be an integer value too. Since counter < dna.length() in your example, the result of this operation is 0 . Following, this result is converted to a double and assigned to gc_content .

What you need to correctly compute the gc_content is to cast the integer values to double prior to the operation. That is, you could write:

double gc_content = double(counter) / double(dna.length());

Now, the result of the operation will be a double value which will then be assigned to gc_content .

There is another flaw in your code in the if condition. When you evaluate `

char(dna[i]=='G')

you are computing a bool value, which results from the expression dna[i]=='G' , then casting this value to a char . Following, this value is cast to bool again to evaluate the condition of the if statement.

You should simply use:

if(dna[i] == 'C' || dna[i] == 'G')

because the overloaded operator[] in the std::string class already returns a char type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM