sorting array

This is a discussion on sorting array within the C Programming forums, part of the General Programming Boards category; hello, i need a function which gets integers array, and prints it in ascending order, the point is that it's ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    sorting array

    hello, i need a function which gets integers array, and prints it in ascending order, the point is that it's not allowed to change the array. loops allowed-maximum 3!
    thanks,

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Quote Originally Posted by bJudy View Post
    hello, i need a function which gets integers array, and prints it in ascending order, the point is that it's not allowed to change the array. loops allowed-maximum 3!
    thanks,
    it's also not allowed to use another array

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,764
    Welcome aboard.

    Please read the three sticky notes (Announcements).
    The first two:

    C Board - Announcements in Forum : C Programming
    C Board - Announcements in Forum : General Programming Boards

    Tim S.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh, I am sorry would you like champagne while you wait as well sir?
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What did your instructor suggest in class, that you use to complete this assignment?

    Show what you've done, and tell us what the instructor recommended for this problem. That will save a lot of time, and get you off to a good start.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Adak View Post
    What did your instructor suggest in class, that you use to complete this assignment?

    Show what you've done, and tell us what the instructor recommended for this problem. That will save a lot of time, and get you off to a good start.
    Oh, Adak, you are too nice sometimes. His instructor probably suggested they do their own work.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I've got a bet going on how the student winds up doing this.

    Money, money, money, money -- $money$!

    It's a sing-along with Pink Floyd! < ROFL >

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    array sorting

    well, i thank all nice people for being such nice,
    any way, i am a new user on this forum,
    i wrote
    Code:
    {
         int i,x;
         int min,tmp;
         int A[SIZE];
        for(i=0;i<SIZE;i++)
           {
              printf("Please insert to array grade to idx %d\n",i);
              fflush(stdin);
              scanf("%d",&A[i]);
            }
          min=A[0];
    for(x = 0; x < SIZE; x++)
    {
    if(A[x]<min )						    min=A[x];	
    }
    printf("%d\n",min); 
    }
    the problem is that i can't find a way to complete the function, i know that i should have another loop that compares other numbers and find next minimum, can i get some ideas??
    thanx

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    {
         int i,x;
         int min,tmp;
         int A[SIZE];
        //add int max variable
         //add int variable sum equals 0, here
        for(i=0;i<SIZE;i++)
        {
              printf("Please insert to array grade to idx %d\n",i);
              fflush(stdin);
              scanf("%d",&A[i]);
              getchar();  //removes left over newline char from the enter key
                                               //}
            if(i equals zero) {
              //assign min to A[i]
              //assign max to A[i]
            }
            if(A[i]  is less than min)
             //what is min now equal to?
            if(A[i] is greater than max)
               //what is max now equal to?
    
            sum equals sum plus A[i]; //get your sum going
    
        }
    
    //for(x = 0; x < SIZE; x++)
    //{
    //if(A[x]<min )						    min=A[x];	
    //}
    printf("%d\n",min); 
    }
    You may need to use more parameters, but that's not prohibited by the instructor. Any variable that you want to change, in a local function, and have it stayed changed after the function returns, needs to have an & "address of" operator used in the calling function, and a * used in the receiving function:

    Code:
    getData(A, &min, &max, &sum); //<== the call to the function
    
    void getData(int A[50], int *min, int *max, int *sum) { //<==the function receiving it
      //etc.
    }
    If I just wrote:
    Code:
    getData(A, min, max, sum);
    then any changes made to min, max, or sum, would be lost when control returned from getData, since here, all these variables are not addresses, they're just copies being sent to getData().
    Last edited by Adak; 11-09-2010 at 10:08 AM.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Also, never fflush(stdin).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 01:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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