for loop - C - error after for() cicle -
i've been coding in c solve problem in rosalind's website. code simple, , because it's simple, it's hard correct error.(i guess) here code:
/* string ordered collection of symbols selected alphabet , formed word; length of string number of symbols contains. example of length 21 dna string (whose alphabet contains symbols 'a', 'c', 'g', , 't') "atgcttcagaaaggtcttacg." given: dna string s of length @ 1000 nt. return: 4 integers (separated spaces) counting respective number of times symbols 'a', 'c', 'g', , 't' occur in s. */ #include <stdio.h> int main(){ char nt; int na, nc, ng, nt; for(int = 0, na = nc = ng = nt = 0; < 1000; i++){ nt = getchar(); if(nt == 'a'){ na++; }else if(nt == 'c'){ nc++; }else if(nt == 'g'){ ng++; }else if(nt == 't'){ nt++; }else{ break; } } printf(" %d %d %d %d", na, nc, ng, nt); }
and when test code:
agcttttcattctgactgcaacgggcaatatgtctctgtgtggattaaaaaaagagtgtctgatagcagc
it should give:
20 12 17 21
but computer gives:
4200624 12 17 21
i've put printf() functions find error located. i've seen in moment right before getting out of cicle na = 20, moment right after na = 4200624 . can do?
i believe due fact redeclaring variables in header, set variable 0. since declare na right after i, have created new variable same name, different scope. 1 visible in loop itself, destroyed after ends. other variables initialised due chain of assignments. i.e. initialised, not redeclared. initialising variables in same line have declared them, solve issue.
Comments
Post a Comment