program not working properly. book example. program included.

Discussion in 'C Programming' started by G G, Sep 27, 2013.

  1. G G

    G G Guest

    The program is an Example from Deitel and Deitel, chapter 6.
    I typed it in carefully, I believe.

    The program seems to freeze in the function median, where
    median calls printArray(). printArray() prints only 80 elements
    and then it seems to be in a loop of some type running
    the cpu at 100%. well, freeze may not be the right terminology.
    really it happens in the printArray(), because printArray()
    does not finish printing.


    output i get:

    ********
    Mean
    ********
    The mean is the average value of the data
    items. The mean is equal to the of
    all the data items divided by the number
    of data items ( 99 ). The mean value for
    this run is: 681 / 99 = 6.8788


    *********
    Median
    *********
    The unsorted array of responses is
    6 7 8 9 8 7 8 9 8 9 7 8 9 5 9 8 7 8 7 8
    6 7 8 9 3 9 8 7 8 7 7 8 9 8 9 8 9 7 8 9
    6 7 8 7 8 7 9 8 9 2 7 8 9 8 9 8 9 7 5 3
    5 6 7 2 5 3 9 4 6 4 7 8 9 6 8 7 8 9 7 8

    seems to be in a loop here. it's still running.
    after some time i get an exit code 9.

    thanks everyone,

    g.

    /* Fig. 6.16: fig06_16.c
    This program introduces the topic of survey data analysis.
    It computes the mean, median and mode of the data */

    #include <stdio.h>

    #define SIZE 99

    /* functtion prototypes */
    void mean( const int answer[] );
    void median( int answer[] );
    void mode( int freq[], const int answer[] );
    void bubbleSort( int a[] );
    void printArray( const int a[] );

    /* function main begins program execution */
    int main( void )
    {
    int frequency[ 10 ] = { 0 }; /* initialize array frequency */

    /* initialize array response */
    int response[ SIZE ] =
    { 6, 7, 8, 9, 8, 7, 8, 9, 8, 9,
    7, 8, 9, 5, 9, 8, 7, 8, 7, 8,
    6, 7, 8, 9, 3, 9, 8, 7, 8, 7,
    7, 8, 9, 8, 9, 8, 9, 7, 8, 9,
    6, 7, 8, 7, 8, 7, 9, 8, 9, 2,
    7, 8, 9, 8, 9, 8, 9, 7, 5, 3,
    5, 6, 7, 2, 5, 3, 9, 4, 6, 4,
    7, 8, 9, 6, 8, 7, 8, 9, 7, 8,
    7, 4, 4, 2, 5, 3, 8, 7, 5, 6,
    4, 5, 6, 1, 6, 5, 7, 8, 7 };

    /* process reponses */
    mean( response );
    median( response );
    mode( frequency, response );

    return 0; /* indicates successful termination */

    } /* end main */


    /* calculate average of all response values */
    void mean( const int answer[] )
    {
    int j; /* counter for totaling array elements */
    int total = 0; /* variable to hold sum of array elements */

    printf ( "%s\n%s\n%s\n", "********", " Mean", "********" );

    /* total response values */
    for ( j = 0; j < SIZE; j++ )
    total += answer[ j ];

    printf ( "The mean is the average value of the data\n"
    "items. The mean is equal to the of\n"
    "all the data items divided by the number\n"
    "of data items ( %d ). The mean value for\n"
    "this run is: %d / %d = %.4f\n\n",
    SIZE, total, SIZE, ( double )total / SIZE );

    } /* end function mean */


    /*sort array and determine median element's value */
    void median( int answer [] )
    {
    printf( "\n%s\n%s\n%s\n%s",
    "*********", " Median", "*********",
    "The unsorted array of responses is" );

    printArray( answer ); /* output unsorted array */

    bubbleSort( answer ); /* sort array */

    printf( "\n\nThe sort array is" );
    printArray( answer ); /* output sorted array */

    /* display median element */
    printf( "\n\nThe median is element %d of \n"
    "the sorted %d element array.\n"
    "For this run the median is %d\n\n",
    SIZE / 2, SIZE, answer[ SIZE / 2 ] );

    } /* end function median */


    /* determine most frequent response */
    void mode( int freq[], const int answer[] )
    {
    int rating; /* counter for accessing elements 1-9 of array freq */
    int j; /* counter for summarizing elements 0-98 of array answer */
    int h; /* counter for displaying histograms of elements in array freq */
    int largest = 0; /* represents largest frequency */
    int modeValue = 0; /* represents most frequenct response */

    printf( "\n%s\n%s\n%s\n",
    "*********", " Mode", "********" );

    /* initalize frequencies to 0 */
    for ( rating = 1; rating <= 9; rating++ )
    freq[ rating ] = 0;

    /* summarise frequencies */
    for ( j = 0; j < SIZE; j++ )
    ++freq[ answer[ j ] ];

    /* output headers for result columns */
    printf( "%s%11s%19s\n\n%54s\n%54s\n\n",
    "Response", "Frequency", "Histogram",
    "1 2 2 2", "5 0 5 0 5");

    /* output results */
    for ( rating = 1; rating <= 9; rating++ )
    {
    printf( "%8d%11d ", rating, freq[ rating ] );

    /* keep track of mode value and largest frequency value */
    if ( freq[ rating ] > largest )
    {
    largest = freq[ rating ];
    modeValue = rating;
    } /* end if */

    for ( h = 1; h <= freq[ rating ]; h++ )
    printf( "*" );

    printf( "\n" );
    } /* end outer for */

    /* display the mode value */
    printf( "The mode is the most frequent value.\n"
    "For this run the mode is %d which occured"
    " %d times.\n", modeValue, largest );
    } /* end function mode */

    /* function that sorts an array with bubble sort algorithm */
    void bubbleSort( int a[] )
    {
    int pass; /* pass counter */
    int j; /* comparison */
    int hold; /* temporary location used to swap elements */

    /* loop to control number of passes */
    for ( pass = 1; pass < SIZE; pass++ )
    {
    for ( j = 0; j < SIZE - 1; pass++ )
    {
    /* swap elements if out of order */
    if ( a[ j ] > a[ j + 1 ] )
    {
    hold = a[ j ];
    a[ j ] = a[ j + 1 ];
    a[ j + 1 ] = hold;
    } /* end if */

    } /* end inner for */

    } /* end outer for */

    } /* end function bubbleSort */


    /* output array contents (20 values per row) */
    void printArray( const int a[] )
    {
    int j; /* counter */

    /* output array contents */
    for ( j = 0; j < SIZE; j++ )
    {
    if ( j % 20 == 0 )
    printf( "\n" );

    printf( "%2d", a[ j ] );

    } /* end for */

    } /* end function printArray */
     
    G G, Sep 27, 2013
    #1
    1. Advertising

  2. G G

    Guest

    On Friday, 27 September 2013 12:23:53 UTC+1, G G wrote:
    > The program is an Example from Deitel and Deitel, chapter 6.
    > I typed it in carefully, I believe.


    Possibly not carefully enough.

    > The program seems to freeze in the function median, where
    > median calls printArray(). printArray() prints only 80 elements
    > and then it seems to be in a loop of some type running
    > the cpu at 100%. well, freeze may not be the right terminology.
    > really it happens in the printArray(), because printArray()
    > does not finish printing.


    printArray() outputs all 99 values, but does not issue a final
    newline, so the last 19 values are still buffered in stdout,
    waiting to be flushed out.

    The problem lies in bubbleSort(). Have another look at the
    loop variables: what they're initialised with, what their end
    conditions are and, in particular, which variables are incremented
    in which loop.
     
    , Sep 27, 2013
    #2
    1. Advertising

  3. G G

    Maxim Fomin Guest

    On 09/27/2013 02:23 PM, G G wrote:
    > The program is an Example from Deitel and Deitel, chapter 6.
    > I typed it in carefully, I believe.
    >
    > The program seems to freeze in the function median, where
    > median calls printArray(). printArray() prints only 80 elements
    > and then it seems to be in a loop of some type running
    > the cpu at 100%. well, freeze may not be the right terminology.
    > really it happens in the printArray(), because printArray()
    > does not finish printing.
    >


    It hangs in bubbleSort() actually.


    > /* function that sorts an array with bubble sort algorithm */
    > void bubbleSort( int a[] )
    > {
    > int pass; /* pass counter */
    > int j; /* comparison */
    > int hold; /* temporary location used to swap elements */
    >
    > /* loop to control number of passes */
    > for ( pass = 1; pass < SIZE; pass++ )
    > {
    > for ( j = 0; j < SIZE - 1; pass++ )
    > {
    > /* swap elements if out of order */
    > if ( a[ j ] > a[ j + 1 ] )
    > {
    > hold = a[ j ];
    > a[ j ] = a[ j + 1 ];
    > a[ j + 1 ] = hold;
    > } /* end if */
    >
    > } /* end inner for */
    >
    > } /* end outer for */
    >
    > } /* end function bubbleSort */
    >


    In second for loop you should increment 'j', not 'pass'.
     
    Maxim Fomin, Sep 27, 2013
    #3
  4. G G

    BartC Guest

    "G G" <> wrote in message
    news:...


    > for ( pass = 1; pass < SIZE; pass++ )
    > {
    > for ( j = 0; j < SIZE - 1; pass++ )


    Make that j++ instead of pass++, and try again.

    (I've never liked C's for statement with having to write so much extra stuff
    that you can get wrong. I now use a simple macro FOR(i,a,b).)

    --
    Bartc
     
    BartC, Sep 27, 2013
    #4
  5. G G <> writes:

    > The program is an Example from Deitel and Deitel, chapter 6.
    > I typed it in carefully, I believe.
    >
    > The program seems to freeze in the function median, where
    > median calls printArray(). printArray() prints only 80 elements
    > and then it seems to be in a loop of some type running
    > the cpu at 100%. well, freeze may not be the right terminology.
    > really it happens in the printArray(), because printArray()
    > does not finish printing.


    No, it finishes. The problem is in the bubble sort function. On many
    systems, output only appears when the line is complete (or the stream is
    flushed) and that's what's confusing you. Print a final \n in
    printArray and you'll see the last 19 elements.

    The error in bubbleSort is easy to spot once you know to look there
    (I'll leave you pleasure of finding it!).

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Sep 27, 2013
    #5
  6. G G

    James Kuyper Guest

    On 09/27/2013 07:23 AM, G G wrote:
    > The program is an Example from Deitel and Deitel, chapter 6.
    > I typed it in carefully, I believe.
    >
    > The program seems to freeze in the function median, where
    > median calls printArray(). printArray() prints only 80 elements
    > and then it seems to be in a loop of some type running
    > the cpu at 100%. well, freeze may not be the right terminology.
    > really it happens in the printArray(), because printArray()
    > does not finish printing.


    Actually, that's not the case. C streams can be either unbuffered, line
    buffered, or fully buffered. Which of theses applies to a given stream
    is up to the implementation, unless you override it by calling setbuf()
    or setvbuf(). The standard prohibits stderr from being fully buffered,
    and says that "When opened, a stream is fully buffered if and only if it
    can be determined not to refer to an interactive device". It says the
    same thing about stdin and stdout.

    It would appear that on your system, as on mine, stdout is line
    buffered. Since printArray() doesn't print a final newline, the last
    line of output is incomplete, so the buffer never flushes, and that
    incomplete line never appears on the screen. You should add printf("\n")
    at the end of printArray().

    Your program is actually stuck somewhere in bubbleSort(), which was my
    first guess - that's the trickiest part of your program. I'll let you
    have the fun of actually figuring out what the actual problem is.
    --
    James Kuyper
     
    James Kuyper, Sep 27, 2013
    #6
  7. G G

    G G Guest

    thanks everyone, i see the problem.
    ! how could i have missed that? !
    yes, not careful enough.

    i did not know about the buffering that takes place.
    that's important to know. thanks.

    you guys are great help.

    g.
     
    G G, Sep 27, 2013
    #7
    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. Aaron Powell
    Replies:
    0
    Views:
    1,217
    Aaron Powell
    Jul 24, 2003
  2. David Williams
    Replies:
    2
    Views:
    491
    David Williams
    Nov 29, 2005
  3. wylbur37
    Replies:
    2
    Views:
    205
    Lasse Reichstein Nielsen
    Jan 2, 2004
  4. wylbur37
    Replies:
    12
    Views:
    197
    Michael C.
    Jun 5, 2004
  5. mem
    Replies:
    0
    Views:
    88
Loading...

Share This Page