472,985 Members | 2,797 Online

# How does this (program) array work?

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;
}

Mar 7 '06 #1
2 3938
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.

Mar 7 '06 #2
How does this program work?
// fig06_07.c -- COP2220 Example -- 040209 (sjd)
// Student poll program
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
others in this group as well).

-Micah
Mar 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 30 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... 1 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... 24 by: David Mathog | last post by: If this: int i,sum; int *array; for(sum=0, i=0; i{ 2 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... 2 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... 0 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... 4 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 :... 1 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... 0 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... 3 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... 4 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...