Bubble Sort in C, help to find bug

reLoadreFresh

Solid State Member
Messages
11
Location
India
I have created this program.

[C] #include<stdio.h> void main() { int i,j,temp,no,type,a[10]; printf(" - Pastebin.com

surprisingly I found that some times it works, some times not. Using Code::Blocks GCC as compiler

Case of Failure


nowork.png


Successful Case
workr.png

I can't figure out whats wrong here, help please
 
Just off the top of my head and based on your fail/success cases, it appears your program can't handle ints with more than 3 digits. Does it ever fail with smaller numbers?

A couple of suggestions regarding your code, why do you check the sort type (ascend/descend) for every sort step? You should be able to write your code where you only need to do that check once. Instead of prompting the user for how many numbers will be entered, why don't you write it so that the user enters as many numbers as they need the program could keep looping until the user enters a null (presses enter without typing in a number).
 
Just off the top of my head and based on your fail/success cases, it appears your program can't handle ints with more than 3 digits. Does it ever fail with smaller numbers?

A couple of suggestions regarding your code, why do you check the sort type (ascend/descend) for every sort step? You should be able to write your code where you only need to do that check once. Instead of prompting the user for how many numbers will be entered, why don't you write it so that the user enters as many numbers as they need the program could keep looping until the user enters a null (presses enter without typing in a number).

It working fine with all 4 digit no. , all 3,2,1 digit no. (not tried 5, bcs of int limit), I still couldn't figure out in which cases, its giving wrong output. :/
thnks for ur suggestion, will move order check condition, right before first loop. any way thanks for reply
 
Back
Top Bottom