Like Tree1Likes
  • 1 Post By christop

Need help debugging code - Program uses function to sort array

This is a discussion on Need help debugging code - Program uses function to sort array within the C Programming forums, part of the General Programming Boards category; I have been working on this all day and cant seem to work anything out.The program accepts the array values ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Need help debugging code - Program uses function to sort array

    I have been working on this all day and cant seem to work anything out.The program accepts the array values and stops. No compiler errors.

    Any help whatsoever is greatly appreciated!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int *ptr;
    int* sort(int numbers[], int size);
    int main(int argc, char *argv[])
    {
      //declaring variables//
      int x;
      int i;
      int counter;
      int place1;
      int place2;
      int place3;
      int place4;
      int p;
      int size;
      
      //Prompts for and accepts integers//
      printf("How many integers in array?\n");
      scanf(" %d", &size);
      int numbers[size];
      
      for(i=0;i<(size);i++)
      {
      printf("Enter Number #%d  ", i+1);
      scanf(" %d", &numbers[i]);
      }
     
      //sorts array//
      ptr = sort(numbers,size);
      
      //prints sorted array//
      for(p=0;p<size;p++)
      {
      printf(" %d", *(ptr+p));
      }     
      
      system("PAUSE");    
      return 0;
    }
    
    
    
    
    int* sort(int numbers[], int size)
    {
     //declaring variables//
     int x;
     int smallest;
     int holdsvar;
     int y;
     //sorts array by finding min value//
     for(x=0; x=(size-1) ; x++)
     {
              smallest=x;
              for(y=x; y=(size-1); y++)  
              {
                       if (numbers[y] < numbers[smallest])
                       {
                                      smallest=y;
                       }
              }
     //switches analysed spot(0->array size) with smallest value//
     holdsvar=numbers[x];
     numbers[x]=numbers[smallest];
     numbers[smallest]=holdsvar;
     }         
             
    return &numbers[0];
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your compiler allows array declarations from local variables?

    I'm impressed!

    Make sort a void function, you don't need any return from it, or do have some special purpose in mind here?
    Last edited by Adak; 11-14-2012 at 07:20 PM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,521
    Code:
    for(x=0; x=(size-1) ; x++)
    Do you know the difference between == and = ?
    If not, you need to learn it.
    If yes, What do you think this does "x=(size-1)"?

    FYI: Making warning go way without understanding them is a bad thing to do.

    Tim S.
    "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

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by Adak View Post
    Your compiler allows array declarations from local variables?

    I'm impressed!
    Yeah, it's a feature added in C99. But I think you use a pre-1999 compiler which doesn't have that feature.
    Matticus likes this.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Thank you stahta01.


    I was assigning (size-1) to x...ooops

    To resolve this i changed
    Code:
    for(x=0; x=(size-1) ; x++)
    to
    Code:
    for(x=0; x<size ; x++)
    for both "for" loops in the sort function.
    Program works now, thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-30-2011, 12:49 PM
  2. what's wrong with this selection sort program code
    By suryak in forum C Programming
    Replies: 14
    Last Post: 05-20-2011, 10:23 AM
  3. Replies: 4
    Last Post: 04-14-2010, 11:42 AM
  4. Need Help Debugging Array Program
    By dcwang3 in forum C Programming
    Replies: 9
    Last Post: 10-01-2008, 08:56 PM
  5. Replies: 3
    Last Post: 08-09-2007, 06:14 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