RadiationX said:
How does this program work?
// fig06_07.c -- COP2220 Example -- 040209 (sjd)
// Student poll program
Single-line comments are not found implementations prior to C99, and
so are less portable than multiline comments. They also tend not to
work so well on Usenet, as there can be issues with line wrapping.
#include <stdio.h>
#include <stdlib.h>
#define RESPONSE_SIZE 40 // number of survey responses
int main( )
This should be:
int main(void)
They don't mean the same thing (the first one means "main() takes an
unspecified number of arguments and returns int"; the second means
"main() takes no arguments and returns int").
{
int k;
int totals[ 11 ] = { 0 };
int responses[ RESPONSE_SIZE ] =
{ 1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
6, 5, 7, 6, 8, 6, 7, 5, 6, 6,
5, 6, 7, 5, 6, 4, 8, 6, 8, 10
};
This appears to be hardcoded data that probably originally came from
somewhere else.
for( k = 0; k < RESPONSE_SIZE; k++ )
++totals[ responses [ k ] ];
This runs through all the elements in responses[], and counts how many
times each particular number occurs. Note that if one of the elements
of responses were mistyped and ended up being less than 0 or greater
than 10, then bad, bad things could happen. Also, if the number of
elements hard-coded into responses were different from the value of
RESPONSE_SIZE.
Note that, if anyone wants to change the number of
elements in responses[], they have to remember to change the value of
RESPONSE_SIZE as well. This makes it far too easy to make mistakes.
My preference would be to define responses[] as:
int responses[] = { 1, 2, ... };
^^
letting responses[] automatically take exactly the right size. Then
you can find out this size by:
const int response_size = (sizeof responses / sizeof responses[0]);
(I usually #define a macro to do this more generally).
I would also put a sanity check within the for loop to make sure that
the value of responses[k] never falls outside of what I
expect. Personally, I'd use assert() for this (which requires that you
#include <assert.h>):
for (k=0; k < response_size; k++) {
int resp = responses[k];
assert(resp >= 0 && resp <= 10);
++totals[resp];
}
Or perhaps even better:
assert(resp >= 0 && resp <= (sizeof totals/sizeof totals[0]));
so that the expression will track with changes to the size of the
totals[] array.
printf( "%s%17s\n", "Rating", "Frequency" );
for( k = 1; k < 11; k++ )
printf( "%6d%17d\n", k, totals[ k ] );
This prints the frequency count out that we collected earlier.
I would prefer
putchar('\n');
it requires a bit less work at runtime. Just a style thing, though.
The above line is not portable. getc() would be a bit more portable;
but much better would be to configure your command window not to exit
automatically upon program termination.
If you'd like some programming practice, you should implement the
improvements I've outlined above, and resubmit the program to the
group for review (taking into account any useful comments made by
others in this group as well).
-Micah