Sorting an array of integers

This is a discussion on Sorting an array of integers within the C Programming forums, part of the General Programming Boards category; hI, Does anyone know why this wont work, All im trying to do is simply sort an array of integers, ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Sorting an array of integers

    hI, Does anyone know why this wont work, All im trying to do is simply sort an array of integers, its based on the bubblesort algorithm from the code snippets section of this site

    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int A[11]= {12, 5, 7, 2, 56, 20, 43, 8, 31, 33, 14};
    int x=0, y=0, n=11;
    
    int main(void)
    {
    for(x; x<n; x++)
    	{
    		for(y; y<n-1; y++)
    		{
    			if(A[y]>A[y+1])
    			{
    				int temp = A[y+1];
    				A[y+1] = A[y];
    				A[y] = temp;
    				}
              }
              
    printf ("A[%d] = %d\n");
           }	
    
    		}
    but the output appears

    Code:
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608
    A[4007008] = 4008608

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,915
    Take a closer look at your use of printf(). Can you see what's missing?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Well it wouldn't hurt if you actually passed the variables to printf().
    Code:
    printf ("A[%d] = %d\n", y, A[y]);
    Sent from my iPadŽ

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your output statement displays rubbish. and it should be in some kind of loop e.g
    Code:
        for ( x=0; x < n; ++x )
            printf ("A[%d] = %d\n", x, A[x]);
    the y in this statement has no effect
    Code:
    for(y; y<n-1; y++)
    make it
    Code:
    for(y=0; y<n-1; y++)
    Same wit x in the outer loop. You could write it like this as well ( because x is initialized to 0 )
    Code:
    for(; x<n; x++)
    Kurt

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Excellent, works perfectly now i think, thanks for all the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Reading in integers into char Array
    By fortune10 in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:32 AM

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