Whats the problem with this one???

D

Dating Ninja

#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
 
S

S S

Dating said:
#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");

}
 
H

Howard

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top