Reputation:
I am working with DNA strands, so an input string will look something like: ATGC (with the possible bases A, T, G, and C)
I have to utilize this function: void updateGCCount(char s[], int * gc, int * at) to calculate the percentage of "GC" content in the input string.
The function, updateGCCount, sweeps through the contents of the input string, s, and updates the "GC" and "AT" counts appropriately.
What I don't understand is, if this function doesn't return anything, if it's void, then how do I use this to calculate the percentage of "GC" content?
Here's my code for the updateGCCount function:
void updateGCCount(char s[], int * gc, int * at){
int i;
for(i=0;i!='\0';i++){
if(s[i]=='G' || s[i]=='C'){
(*gc)++; /*Updated with the help of people who answered!*/
}
if(s[i]=='A' || s[i]=='T'){
(*at)++; /*Updated with the help of people who answered!*/
}
}
}
And now here is my main function with the call to the above function (added this code on after receiving some help from the answers below):
int main(){
char s[400];
int gc, at;
double percentage;
scanf("%s", s);
gc = 0;
at = 0;
updateGCCount(s, &gc, &at);
percentage = (gc * 100.0)/(strlen(s) - 1);
printf("Sequence : %s\n", s);
printf("GC-content: %.2f\n", percentage);
return 0;
}
PROBLEMS I'M HAVING! When I enter an input string of "ATCG", the percentage is 0 which isn't right and I can't figure out why it's giving me this problem! Help is greatly appreciated!
Thanks!
Upvotes: 0
Views: 398
Reputation: 755010
You need to increment what gc
and at
point at:
(*gc)++;
(*at)++;
As it stands, you keep incrementing the pointers themselves, which may lead to problems (though, since you don't actually dereference the pointers, it may not lead to major problems).
You might call it with:
char *dna = ...;
int gc = 0;
int at = 0;
updateGCCount(dna, &gc, &at);
Now you just have to determine what the percentage means. Is it the number of pairs of GC out of the total number of adjacent pairs, or some other specification (like the total number of either G or C compared to the total length of the DNA strand)?
It is likely that you count not pairs but single characters, so the percentage you're after becomes:
double percentage_gc = (gc * 100.0) / strlen(dna);
You also need to fix the test for end of string:
for (i = 0; s[i] != '\0'; i++)
Upvotes: 3
Reputation: 46435
You have fixed the problem with the pointers, but it's still not working.
You are not comparing "pairs", but individual characters; and your loop is all wrong. If you want to count "GC" (that is a G followed by a C), I think the following would work:
void updateGCCount(char s[], int * gc, int * at){
int i, Length;
Length = strlen(s); /* new terminating condition for the loop */
for( i = 0; i < Length - 1; i++ ) {
if(s[i]=='G' && s[i+1] =='C') {
(*gc)++; /* Updated with the help of people who answered! */
i++; /* if you found G followed by C, skip one */
}
if(s[i]=='A' && s[i+1]=='T') {
(*at)++; /* Updated with the help of people who answered! */
i++; /* if you found A followed by T skip one */
}
}
}
Upvotes: 0
Reputation: 5083
int * gc
is a pointer to a variable outside the function where the person calling the function wants the answer stored. You want to increment the value at that pointer:
(* gc) ++ ;
instead of
gc ++ ;
Also you are not looking at the string passed in, so this loop is never going to exit. Instead do:
int i; char c ;
for ( i= 0 ; ((c= s[i])) ; i ++ ) {
if ( c == 'G' || c == 'C' ) { (* gc) ++ ; }
etc.
Upvotes: 1