How does this program work?
// fig06_07.c -- COP2220 Example -- 040209 (sjd)
// Student poll program
#include <stdio.h>
#include <stdlib.h>
#define RESPONSE_SIZE 40 // number of survey responses
int main( )
{
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
};
for( k = 0; k < RESPONSE_SIZE; k++ )
++totals[ responses [ k ] ];
printf( "%s%17s\n", "Rating", "Frequency" );
for( k = 1; k < 11; k++ )
printf( "%6d%17d\n", k, totals[ k ] );
printf( "\n" );
system( "pause" );
return 0;
} 2 3938
RadiationX wrote: How does this program work?
// fig06_07.c -- COP2220 Example -- 040209 (sjd) // Student poll program
#include <stdio.h> #include <stdlib.h>
#define RESPONSE_SIZE 40 // number of survey responses
int main( ) { 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 };
for( k = 0; k < RESPONSE_SIZE; k++ ) ++totals[ responses [ k ] ];
printf( "%s%17s\n", "Rating", "Frequency" );
for( k = 1; k < 11; k++ ) printf( "%6d%17d\n", k, totals[ k ] );
printf( "\n" ); system( "pause" ); return 0; }
----------------------------------------------------------------------------------------------------------------------
Very Simple !!!!. If you look at the array responses[] it has
elements in the set {1,2,3,4,5,6,7,8,9,10}.The array totals[] maintains
the frequency of occurence of each element present in array
responses[]. The array totals[] maintains the number of occurences of,
say the number '5' at position totals[5]. Similarly it maintains number
of occurences of the number '8' at position totals[8].
-----------------For Example------------------
Assume that while in the first for loop, k=4. Then
++totals[responses[4]];
gets executed.
Looking at the array responses[], we see that
respones[4] = 8;
Thus, in effect we are executing ++totals[8];
This means that we are increasing the value of totals[8] by 1 (in this
case totals[8] is 0 prior to statement execution). As we read the
elements of the array responses[] we, encounter the element '8' seven
times. So totals[8] is incremented by one each time. Finally we have
totals[8] = 7;
Similarly for other elements.
The second for loop just prints the value of occurences stored in array
totals[].
If you are still not clear, try to trace the program , You can use GDB
in Linux or the debugger present Microsoft VC++ in windows.
"RadiationX" <he*********@gmail.com> writes: 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. printf( "\n" );
I would prefer
putchar('\n');
it requires a bit less work at runtime. Just a style thing, though.
system( "pause" );
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.
return 0; }
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Christian Seberino |
last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python?
Python's design is godly. I'm wondering if Ruby's is godly too.
I've heard it has solid OOP design but then...
|
by: carl bloc |
last post by:
Have this much done but need help with this bit of code:
allow user to modify existing draw data. I think I need a counter to
give week numbers so the user can select a week number rather than a...
|
by: David Mathog |
last post by:
If this:
int i,sum;
int *array;
for(sum=0, i=0; i<len; i++){
sum += array;
}
is converted to this (never mind why for the moment):
|
by: Garry Freemyer |
last post by:
I'm trying to convert this macro to a c# function but I have a big problem.
It's on the LEFT side of an assignment statement and I am extremely
flustered over this one because I'm a little rusty...
|
by: Daniel Rudy |
last post by:
Hello,
I have a peice of code that I'm making an attempt to code. The problem
is that I need to return an arbitrary number of char strings.
int function(char *fname, int *dcount, char *data)...
|
by: Tony Sinclair |
last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on
WinXP) to combine several files into one (HKSplit is a popular
freeware program that does this, but it requires all input and...
|
by: Anoop |
last post by:
Hi,
I am new to this newsgroup and need help in the following questions.
1. I am workin' on a GUI application. Does C# provides Layout Managers the
way Java does to design GUI? I know that it...
|
by: valerij |
last post by:
Please help. I have been on this problem for a week now.
I am using Windows 98SE, Microsoft Visual C++ 6.0
The following program only works when the function is not
called, BUT the function does...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |