Help for CSCI 127 Class

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.
do something like
for(int i=0; i <str.len[]; i++)
{
if
{ (str[i]=a && str[i]=c && str[i]=g && str[i]=t)
count++;
}
}
Last edited on
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.

That's all.
The sequence will be randomly written by my professor when he tests it and the program should be able to count the number of As Ts Cs Gs.
do as above it will work i just did that in csci127 in the spring semester i hope its not the same professor gud luck
Last edited on
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.
so basically I assign each base to an integer, string the integers, and then set a count command so it will count how many As Ts Cs and Gs there are?
Yes.
Although assigning an integer to each base is not necessary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>
using namespace 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.
Last edited on
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.
please forget this post, for some reason I can't delete it
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
using namespace 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.
Last edited on
Topic archived. No new replies allowed.