How does this (program) array work?

Discussion in 'C Programming' started by RadiationX, Mar 7, 2006.

  1. RadiationX

    RadiationX Guest

    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;
    }
     
    RadiationX, Mar 7, 2006
    #1
    1. Advertising

  2. RadiationX

    imad Guest

    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.
     
    imad, Mar 7, 2006
    #2
    1. Advertising

  3. RadiationX

    Micah Cowan Guest

    "RadiationX" <> 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
     
    Micah Cowan, Mar 7, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    7
    Views:
    334
  2. Sheldon

    does a program work in all cases?

    Sheldon, Dec 17, 2006, in forum: C Programming
    Replies:
    42
    Views:
    937
    Default User
    Dec 18, 2006
  3. Tony Johansson
    Replies:
    4
    Views:
    382
    David Harmon
    Dec 30, 2005
  4. Pat
    Replies:
    2
    Views:
    632
    Göran Andersson
    Nov 6, 2008
  5. Phi!
    Replies:
    1
    Views:
    180
Loading...

Share This Page