Thread: help

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Question help

    Using a bubble sort, I need to show the contents of the Grades array after each pass, but I haven't had any luck with figuring out the correct printf statement to get it to print after each pass... any suggestions???? I thought it would need to be printf("%d", the variable )... I thought it would be one of the following variables, but none would give me the correct answer)

    Grades[ k ] = Grades[ k + 1 ];
    Grades[ k + 1 ] = hold;

    Thanks!

    Here's what I have so far :>


    /* Show the contents of the Grades array after each pass. */

    #include <stdio.h>
    #define SIZE 5

    int main()
    {
    int Grades[ SIZE ] = { 86, 93, 87, 99, 78 };
    int k, pass, hold;


    printf( "Data items in original order\n" );

    for ( k = 0; k <= SIZE - 1; k++ )
    printf( "%4d", Grades[k] );

    for ( pass = 1; pass <= SIZE - 1; pass++ ) /* passes */

    for ( k = 0; k <= SIZE - 2; k++ ) /* one pass */

    if ( Grades[ k ] > Grades[ k + 1 ] ) /* one comparison */
    {
    hold = Grades[ k ]; /* one swap */
    Grades[ k ] = Grades[ k + 1 ];
    Grades[ k + 1 ] = hold;

    }


    printf( "\nData items in ascending order\n" );

    for ( k = 0; k <= SIZE - 1; k++ )
    printf( "%4d", Grades[ k ] );

    printf( "\n" );

    return 0;

    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Hmm...looks OK. What does it print on your computer?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    I revised the program

    Is this what I should be seeing as the Grades array after each pass or do I need to so something differently?

    C:\Borland\bcc55\Bin>bcc32 pl2tmp3.c
    Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
    pl2tmp3.c:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

    C:\Borland\bcc55\Bin>pl2tmp3
    Data items in original order
    86 93 87 99 78
    Grades array contents after each pass
    87 93
    Grades array contents after each pass
    78 99
    Grades array contents after each pass
    78 93
    Grades array contents after each pass
    78 87
    Grades array contents after each pass
    78 86

    Data items in ascending order
    78 86 87 93 99

    C:\Borland\bcc55\Bin>

    Thanks again!


    /*Part 5: The following is the code to implement a bubble sort. */
    /* Show the contents of the Grades array after each pass. */

    #include <stdio.h>
    #define SIZE 5

    int main()
    {
    int Grades[ SIZE ] = { 86, 93, 87, 99, 78 };
    int k, pass, hold;


    printf( "Data items in original order\n" );

    for ( k = 0; k <= SIZE - 1; k++ )
    printf( "%4d", Grades[k] );
    printf( "\n" );


    for ( pass = 1; pass <= SIZE - 1; pass++ ) /* passes */

    for ( k = 0; k <= SIZE - 2; k++ ) /* one pass */

    if ( Grades[ k ] > Grades[ k + 1 ] ) /* one comparison */
    {
    hold = Grades[ k ]; /* one swap */
    Grades[ k ] = Grades[ k + 1 ];
    Grades[ k + 1 ] = hold;
    printf("Grades array contents after each pass\n");
    printf( "%4d%4d", Grades[k], Grades[ k + 1 ] );
    printf( "\n" );

    }



    printf( "\nData items in ascending order\n" );

    for ( k = 0; k <= SIZE - 1; k++ )
    printf( "%4d", Grades[ k ] );

    printf( "\n" );

    return 0;

    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I see what you're saying now. I don't think it's possible on each pass. Look:

    Code:
    for ( pass = 1; pass <= SIZE-1; pass++ ) /* passes */
    {
         for ( k = 0; k <= SIZE - 2; k++ ) /* one pass */
        {
           if ( Grades[ k ] > Grades[ k+1 ] ) /* one comparison */
          {
            hold = Grades[ k ]; /* one swap */
            Grades[ k ] = Grades[ k + 1 ];
            Grades[ k + 1 ] = hold;
          }
       }
      printf( "%4d", Grades[ pass-1 ] );
    }
    So the problem is, we can't print the results until they're sorted, which may or not be on each pass...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Here's a better example.


    Code:
    for( pass = 0; pass < SIZE; pass++ ) /* passes */
    {
         for ( k = 0; k < SIZE - 1; k++ ) /* one pass */
        {
           if ( Grades[ k ] > Grades[ k + 1 ] ) /* one comparison */
          {
            hold = Grades[ k ]; /* one swap */
            Grades[ k ] = Grades[ k + 1 ];
            Grades[ k + 1 ] = hold;
          }
       }
    
     printf( "\nPass [%i]: ", pass+1);
    
         for(i = 0; i < SIZE; ++i)
        {
          printf( "%4d", Grades[ i ] );
        }
    
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    10

    Smile

    Thanks so much!!! That was it!!! That's exactly what I was trying to do!!!




    (Will this get easier? I'm "programming " by using examples of programs that I find in the text, etc, and using them as templates to jumpstart my own basic programs.... this is the 2nd semester of programming and logic in C...I would have thought I should have gotten that lightbulb turned on by now :> )

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Sure, learning by example is the best way to learn. Of course it will get easier as time goes by. Just keep at it!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed