We recently started learning C++ in my class and we have a project where we have to make a program in C++ that can count all the As Ts Gs and Cs in any given DNA sequence. I am not sure where to start though.
The DNA sequence is simply a sequence of characters.
The first thing to do is to get this sequence into your program. The way you do that depends on how the sequence will be given (in a text file, or input by an user in the console ?).
Since you want two count 4 characters, the next thing to do is to create 4 variables that will hold the count of each character. Obviously, these variables will be initialized to 0.
Then, inspect each character of the sequence. For each character, find which it is among A/T/G/C and add 1 to the associated variable.
All you have to do is what I described in my first post.
If you don't know how to do any of these steps, just ask and I'll explain.
But I won't code it for you.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
int numC = 0;
int numG = 0;
char string[C];
char string[G];
cout << “Enter a DNA sequence”;
if ((character == 'C') (character == 'G'));
++numC;
++numG;
}
{
cout <<”\n\tTotal number of C:” <<string[C]<<endl;
cout <<”\n\tTotal number of G:”<<string[G]<<endl;
return 0;
}
So this is what I basically have so far but I have a strong feeling I messed up in multiple places. I also looked back and it says that I only need to find 2 of the bases so I chose to count Cs and Gs.
Please use <>code<> tags when pasting your code so we can read it easily.
In your code, you prompt the user to enter a sequence but never actually take input. You want to look into std::cin.
Your if() condition (and routine) is also odd.
Step back from your code and ask yourself, "What do I want my program to do?"
Focus on that. Your program is supposed to take input for some DNA sequence and somehow process it, right? Well, take the first step to do that. How are you going to store this input? As a string? Well then, use a string object and insert the string into that.
How is it going to read elements from the string? Will it be going iteratively to process each character? If it will, that involves a lot of repetition. Loops happen to be perfect for that. Take the next step and so on.
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{
int C = 0
int G = 0
string seq = “ “;
char c = ' ';
cout << “Enter a DNA sequence”;
cin >> sequence;
while (seq >> c) { if(c != “A” || c ! = “T” || c ! = “C” || c ! = “G”)
cout << “Not a DNA sequence”;
return 0;
if (c == “C”) c++;
if (c == “G”) g++;
}
{
cout << c;
cout << g;
return 0;
}
this is what I have so far, what else am I missing? Sorry for all this, the hurricane here set us back quite a bit.