pa******@gmail.com said:
>
Hi, I've sent you an e-mail of my code and supporting information.
Presumably, then, you have a follow-up question, but it's not easy to see
what that is. Here are the results of my compilation:
bio1.c: In function `main':
bio1.c:53: warning: double format, different type arg (arg 2)
bio1.c:14: warning: unused parameter `argc'
bio1.c:56: warning: control reaches end of non-void function
The principal line it is complaining about is this:
printf("%f %s\n", acids[i].count, acids[i].name);
where you try to print an int value as if it were a double. The format
specifier for double is %f, but acids[i].count has type int. What is the
correct printf format specifier for int? (Use one side of the paper only. 3
marks. The use of calculators is not permitted.)
The other problems it is complaining about are these: you define, but don't
use, a parameter called argc. The solution is to use it, and there's an
excellent reason why you should: if argc is 2 or less, then your second
fopen call will fail. If it's 1 or less, your first fopen call will fail.
So it's a good idea to check argc before dooming your program to disaster.
Also, note that you have - perfectly correctly - defined main to return int,
as is right and proper and well-defined (unlike any other return type for
main). When we say a function returns a value of a given type, it's good to
return a value of that type from that function. So, just after your final
printf("\n"); but before your closing brace, why not add this line?
return 0;
to indicate (perhaps a trifle optimistically) that your program succeeded in
executing correctly and producing the desired results?
Once you have made these corrections, however, you will notice that your
program continues to produce incorrect results. Just because a program
compiles cleanly (at a decent warning level), that doesn't mean it's
correct! The most obvious problem is that you carefully count (using the
object known as i) how much data you read in - how many records, how many
lines, whatever you want to call it - but then you discard that
information, re-using i as a counter in the next loop. So your program
forgets how many of the 100 available array elements it actually used, and
instead tries to print all 100, most of which are likely to contain
indeterminate values.
After modifying your program to store that value (the number of lines read)
instead of discarding it, and using it in the conditional expression of
subsequent loops, I was able to run your program with what look like
correct results. I suggest, therefore, that you do the same.
One last thing: in future, if you wish to use me as a private consultant, it
might be wiser to find out my fee scale in advance.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.