Thread: Sorting 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
    28,413
    Take a closer look at your use of printf(). Can you see what's missing?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,079
    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
    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, 09: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