Whats the problem with this one???

Discussion in 'C++' started by Dating Ninja, Sep 21, 2006.

  1. Dating Ninja

    Dating Ninja Guest

    #include<stdio.h>
    #include<conio.h>
    int main()
    {
    int arr[50];
    int y,a,pass,hold;
    printf("Enter # of Items: ");scanf("%d",&y);
    for(a=1;a<=y;a++)
    {
    printf("Num %d: ",a);scanf("%d",&arr[y]);
    }

    for( pass = 1 ; pass <= y - 1 ; pass++ )
    for( a=0 ; a <= y - 2 ; a++ )
    if( arr [ a ] > arr [ a + 2 ] )
    {
    hold = arr [ a ];
    arr [ a ] = arr [ a + 1 ];
    arr [a + 1] = hold;

    }
    printf("\nData Items In Ascending Order\n");
    for(a = 0 ; a <= y - 1 ;a++)
    printf(" %5d ", arr[ y ] );
    printf("\n");
    getch();
    }


    bubble sort my every inputs
     
    Dating Ninja, Sep 21, 2006
    #1
    1. Advertisements

  2. Dating Ninja

    S S Guest

    Dating Ninja wrote:
    > #include<stdio.h>
    > #include<conio.h>
    > int main()
    > {
    > int arr[50];
    > int y,a,pass,hold;
    > printf("Enter # of Items: ");scanf("%d",&y);
    > for(a=1;a<=y;a++)
    > {
    > printf("Num %d: ",a);scanf("%d",&arr[y]);


    replace &arr[y] by &arr[a-1]

    > }
    >
    > for( pass = 1 ; pass <= y - 1 ; pass++ )
    > for( a=0 ; a <= y - 2 ; a++ )
    > if( arr [ a ] > arr [ a + 2 ] )
    > {
    > hold = arr [ a ];
    > arr [ a ] = arr [ a + 1 ];
    > arr [a + 1] = hold;
    >
    > }
    > printf("\nData Items In Ascending Order\n");
    > for(a = 0 ; a <= y - 1 ;a++)
    > printf(" %5d ", arr[ y ] );


    arr[a]

    > printf("\n");
    > getch();
    > }
    >
    >
    > bubble sort my every inputs


    besides the code will not work, you should know the algorithm before
    implementation.

    here is the correct code
    #include<stdio.h>
    int main()
    {
    int arr[50];
    int y,a,pass,hold;
    printf("Enter # of Items: ");scanf("%d",&y);
    for(a=0;a<y;a++)
    {
    printf("Num %d: ",a+1);scanf("%d",&arr[a]);
    }

    for( pass = y-1 ; pass > 0 ; pass-- )
    for( a=0 ; a < pass ; a++ )
    if( arr [ a ] > arr [ a + 1 ] )
    {
    hold = arr [ a ];
    arr [ a ] = arr [ a + 1 ];
    arr [a + 1] = hold;

    }
    printf("\nData Items In Ascending Order\n");
    for(a = 0 ; a <= y - 1 ;a++)
    printf(" %5d ", arr[ a ] );
    printf("\n");

    }
     
    S S, Sep 21, 2006
    #2
    1. Advertisements

  3. Dating Ninja

    Howard Guest

    "Dating Ninja" <> wrote in message
    news:...

    You already got someone to answer you, but for future reference, please read
    the FAQ at http://www.parashift.com/c++-faq-lite/, especially section 5.
    (The whole thing is a great reference, really.)

    Posting "what's the problem with this?" in the subject and then posting some
    code in the message doesn't tell anyone what problem you're trying to
    resolve. Are you getting compiler errors? Link errors? Run-time errors?
    Are the results not what you expect? Are we supposed to guess what you're
    trying to fix? Help us understand the problem, and you'll always get better
    results here.

    -Howard
     
    Howard, Sep 21, 2006
    #3
    1. Advertisements

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. Vaidas Gudas

    whats problem in asp.net 2.0

    Vaidas Gudas, Jan 9, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    4,707
    john smith
    Jan 9, 2006
  2. Gaurav
    Replies:
    9
    Views:
    613
    Ross Bamford
    May 3, 2005
  3. news.frontiernet.net
    Replies:
    6
    Views:
    1,431
    news.frontiernet.net
    Apr 16, 2004
  4. loveNUNO
    Replies:
    2
    Views:
    1,210
    loveNUNO
    Nov 20, 2003
  5. David
    Replies:
    2
    Views:
    419
    jeffc
    May 27, 2004
  6. yogesh
    Replies:
    1
    Views:
    545
    Victor Bazarov
    Mar 14, 2007
  7. Merciadri Luca
    Replies:
    4
    Views:
    1,051
  8. aa

    Whats problem with IIS

    aa, Oct 15, 2004, in forum: ASP General
    Replies:
    2
    Views:
    163
    Ray Costanzo [MVP]
    Oct 15, 2004
Loading...