473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 3963
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.

Mar 7 '06 #2
"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
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<len; i++){ sum += array; } is converted to this (never mind why for the moment):
4
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...
7
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)...
51
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...
14
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.