Like Tree6Likes
  • 1 Post By anduril462
  • 1 Post By memcpy
  • 2 Post By phantomotap
  • 1 Post By Salem
  • 1 Post By iMalc

can any1 help me with this bubble sort program and tell why it takes garbage values

This is a discussion on can any1 help me with this bubble sort program and tell why it takes garbage values within the C Programming forums, part of the General Programming Boards category; in this program i am dynamically allocating space for an array and then sorting the numbers entered by the user ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Bangalore
    Posts
    5

    can any1 help me with this bubble sort program and tell why it takes garbage values

    in this program i am dynamically allocating space for an array and then sorting the numbers entered by the user this program run successfully when array size is small as i try to take larger array size it some how take some garbage value plz help me with this problem...
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define NULL 0
    void main()
    {
    clrscr();
    int *ptr,*i,j,n,t;
    printf("enter the number\n :" );
    scanf("%d",&n);
    ptr=(int*)malloc(n*sizeof(int));//dynamic memory allocation for a block of memory of size n to be entered by user
    printf("input %d values\n",n);
    
    for(i=ptr;i<ptr+n;i++)
    {
    scanf("%d",i);//inputting the values entered by the user into the block of dynamic memory of size n 
    }
    
    
    if(ptr==NULL)//checking for availability of memory for dynamic allocation
    {
    printf("memory not sufficient");
     exit(1);
    }
    
    else
    {
    for(j=1;j<=n-1;j++)//applying bubble sort algorithm 
    {
    for(i=ptr;i<=ptr+(n-j);i++)
    {
    if(*i<=*(i+1))
    {
    t=*i;
    *i=*(i+1);
    *(i+1)=t;
    }
    else
    {
    continue;
    }
    }
    }}
    
    for(i=ptr;i<ptr+n;i++)// printing the sorted list
    {
    printf("%d\t",*i);
    }
    
    
    getch();
    }
    Last edited by ig_bpit; 04-17-2012 at 03:47 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your code has no spacing or indentation. I'm not reading it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Apr 2012
    Location
    Bangalore
    Posts
    5
    i m really sry.i am a nwb so plz frgiv me for nt putting d ques in right form and nw will keep in my mnd before postin...

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    531
    Agreed, indentation please!

    You should do your NULL pointer check straight after the malloc. As it is you try to read lots of data into a pointer that could be NULL. But then I'd expect a segfault/access violation, rather than garbage.

    How large is "larger"? Potentially large enough for the malloc to fail? What compiler are you using?

  5. #5
    Registered User
    Join Date
    Apr 2012
    Location
    Bangalore
    Posts
    5
    larger mean 20,compiler which i am using is turboc

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    I suggest learning to use functions!

    I am guessing you have a poor bubble sort code; not worth my time to verify it.

    Tim S.

    Formatted your code below.
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define NULL 0
    void main()
    {
    	int *ptr,*i,j,n,t;
    	clrscr();
    	printf("enter the number\n :" );
    	scanf("%d",&n);
    	ptr=(int*)malloc(n*sizeof(int));//dynamic memory allocation for a block of memory of size n to be entered by user
    	printf("input %d values\n",n);
    
    	for(i=ptr; i<ptr+n; i++)
    	{
    		scanf("%d",i);//inputting the values entered by the user into the block of dynamic memory of size n
    	}
    
    
    	if(ptr==NULL)//checking for availability of memory for dynamic allocation
    	{
    		printf("memory not sufficient");
    		exit(1);
    	}
    
    	else
    	{
    		for(j=1; j<=n-1; j++) //applying bubble sort algorithm
    		{
    			for(i=ptr; i<=ptr+(n-j); i++)
    			{
    				if(*i<=*(i+1))
    				{
    					t=*i;
    					*i=*(i+1);
    					*(i+1)=t;
    				}
    				else
    				{
    					continue;
    				}
    			}
    		}
    	}
    
    	for(i=ptr; i<ptr+n; i++) // printing the sorted list
    	{
    		printf("%d\t",*i);
    	}
    
    
    	getch();
    }
    Last edited by stahta01; 04-17-2012 at 04:18 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,304
    Quote Originally Posted by ig_bpit View Post
    i m really sry.i am a nwb so plz frgiv me for nt putting d ques in right form and nw will keep in my mnd before postin...
    Just like code with no indentation, spelling, etc is obnoxious to read, so is your "SMS speak", with poor spelling, spacing and punctuation. Use normal English to the best of your ability, so everybody is clear on what you're saying. I'm pretty sure that's not how they taught you English at school.

    I know schools in India require you to use Turbo C, but it would be a good idea to learn a modern compiler and the modern standard version of C as well. There are a number of free compilers/IDEs available, like Pelles C, Code::Blocks with MinGW, MS Visual C++ Express, and of course, if you have Linux or the like, there is GCC.

    Start with some basics like using int main(void) and returning an int at the end, usually 0 for success. Also, clrscr() and getch() are not standard functions. Just don't use clrscr, and use getchar in place of getch.

    Your code does seg fault. When I ran it for 20 integers, it seg faulted here:
    Code:
    if(*i<=*(i+1))
    i is at ptr+19, so i+1 is at ptr+20, i.e. ptr[20], which is an out of bounds access. You allocate 20 spaces, which are indexes 0-19. The problem is probably due to your inner for loop:
    Code:
    for(i=ptr; i<=ptr+(n-j); i++)
    That <= should probably be just a <.

    This should make it obvious just how ugly and confusing pointer syntax usually is. Prefer array syntax, since ptr is effectively an array (i.e. it's consecutive data). That means do stuff like ptr[index] instead of *i, *(i+1), etc.
    Salem likes this.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by ig_bpit View Post
    i m really sry.i am a nwb so plz frgiv me for nt putting d ques in right form and nw will keep in my mnd before postin...
    I'm no grammar nazi, but for ........s sake, out of those 28 "words", 12 were misspelled. And the problem isn't that it's the wrong forum, it's that you didn't use indentation and your code is hard to read.

    Also, you might want to get another compiler that warns about void main, and doesn't support the <30 year-old <conio.h> header.
    Salem likes this.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    Quote Originally Posted by ig_bpit View Post
    i m really sry.i am a nwb so plz frgiv me for nt putting d ques in right form and nw will keep in my mnd before postin...
    If you want anyone to actually help you, then stop wasting our time by forcing us to spend extra time trying to decode your obfuscated text.
    You job is to do as much as possible to make it easy for those who might choose to help you, not to annoy them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,800
    O_o

    D0 j00Z L1|<3 r34D1|\|9 7|-|1$ |\|0|\|$3|\|$3? 1 d0|\|'7. 1'D r47|-|3r r34D $0/\/\37|-|1|\|9 /\/\34|\|1|\|9PhUL. 1Ph j00Z (4|\|'7 74|<3 7|-|3 71/\/\3 70 7'/P3 pr0P3rL'/ j00Z pr0B4BL'/ (4|\|'7 4PhPh0rD 7|-|3 71/\/\3 70 (0D3 pr0P3rL'/ L34D1|\|9 /\/\3 70 4$$U/\/\3 7|-|@ j00r (0D3 1$ br0|<3|\| b3(4U$3 j00Z R 4|\| 1D107.
    claudiu and manasij7479 like this.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by phantomotap View Post
    O_o

    D0 j00Z L1|<3 r34D1|\|9 7|-|1$ |\|0|\|$3|\|$3? 1 d0|\|'7. 1'D r47|-|3r r34D $0/\/\37|-|1|\|9 /\/\34|\|1|\|9PhUL. 1Ph j00Z (4|\|'7 74|<3 7|-|3 71/\/\3 70 7'/P3 pr0P3rL'/ j00Z pr0B4BL'/ (4|\|'7 4PhPh0rD 7|-|3 71/\/\3 70 (0D3 pr0P3rL'/ L34D1|\|9 /\/\3 70 4$$U/\/\3 7|-|@ j00r (0D3 1$ br0|<3|\| b3(4U$3 j00Z R 4|\| 1D107.
    I know what that last word means haha, pure gold though.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    I pasted it into google translate, and it thought it was French
    memcpy likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Location
    Bangalore
    Posts
    5
    Thanks for all your sarcastic replies but for your kind informatiom none of them really helped....

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    Maybe you misunderstand sarcasm. I was being frank, as I'm thought many others were.

    Back to the code at hand... Why do you have a continue in there? Do you expect the loop to stop otherwise. Okay, that was perhaps sarcasm that time. Anyway, there' nothing between it and the next loop iteration, so it has no effect.

    I think the best thing for you to do now would be to post your updated code after taking anduril462's advice. Don't forget indentation this time.

    Lastly, whenever possible always post the input you are providing, the output you are getting, and the output you were expecting.
    Salem likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. garbage values
    By j.vick in forum C Programming
    Replies: 2
    Last Post: 04-06-2010, 03:31 PM
  2. Replies: 7
    Last Post: 07-28-2009, 03:15 PM
  3. Bubble sort program stumped
    By matthughes in forum C Programming
    Replies: 12
    Last Post: 05-17-2007, 01:28 PM
  4. problem(program error) with bubble sort(code included)
    By choykawairicky in forum C++ Programming
    Replies: 6
    Last Post: 05-16-2004, 08:54 AM
  5. Garbage values
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-03-2002, 05:17 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21