Like Tree1Likes

Need help sorting arrays

This is a discussion on Need help sorting arrays within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to try to write a program which allows the user to enter some numbers and get the ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Need help sorting arrays

    Hello, I'm trying to try to write a program which allows the user to enter some numbers and get the numbers back, sorted from small to big. The only problem is that I'm stuck at sorting, normally this should work, it's the sorting algorithm I also used in java, anyone see the fault?

    Code:
    /* AUTH: Kevin Strijbos   DATE: 17/01/2012
       DESCR: includes 2 functions that combine arrays
       */
    
    
    #include <stdio.h>
    
    
    #define MAX_LENGTH 3
    
    
    void initializeArray (int numbers[], int *pointers[]);
    void initializeMinima (int numbers[], int pointers[]);
    
    
    int main (void)
    {
        int numbers[MAX_LENGTH];
        int *pointers[MAX_LENGTH];
        int counter;
    
    
        for (counter = 0; counter < MAX_LENGTH ;counter++)
        {
            printf("Geef een getal:\t\n");
            scanf("%d", &numbers[counter]);
        }
    
    
        
        initializeArray(numbers,pointers);
    
    
        for (counter = 0; counter < MAX_LENGTH ;counter++)
            printf("Element %d:\t%d\n", counter, *pointers[counter]);
    
    
        initializeMinima(numbers, pointers);
    
    
        for (counter = 0; counter < MAX_LENGTH ;counter++)
            printf("Element %d:\t%d\n", counter, *pointers[counter]);
    
    
        return 0;
    }
    
    
    void initializeArray (int numbers[], int pointers[])
    {
        int counter;
    
    
        for (counter = 0; counter < MAX_LENGTH ;counter++)
            pointers[counter] = &numbers[counter];
    }
    
    
    void initializeMinima (int numbers[], int pointers[])
    {
        int temp, index, minima, counter, smallCounter;
    
    
        for (counter = 0; counter < MAX_LENGTH - 1 ;counter++)
        {
          minima = numbers[counter];
          index = counter;
    
    
          for (smallCounter = counter + 1; smallCounter < MAX_LENGTH ;smallCounter++)
          {
             if (numbers[smallCounter] < minima)
             {
                minima = numbers[smallCounter];
                index = smallCounter;
             }
          }
    
    
          /* swapping minima */
          temp = pointers[counter];
          pointers[counter] = pointers[index];
          pointers[index] = temp;      
       }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should be providing some context here. Does it not compile? Does it not give you the output you expect, what exactly?
    Code:
    void initializeArray (int numbers[], int *pointers[]);
    Code:
    void initializeArray (int numbers[], int pointers[])
    Compile with warnings on.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    No warnings, no errors, it prints then numbers but not in the right order. I'm trying to do it with pointers, therefore the 2 tables.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kevinstrijbos View Post
    No warnings, no errors
    Like I said, compile with warnings on. Your function prototype does not match your function definition.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Strange, I got my warning settings on wall... Corrected it now, still doesn't work.



    Give a number:
    10
    Give a number:
    99
    Give a number:
    2
    Give a number:
    0
    Give a number:
    1
    Give a number:
    4
    Give a number:
    8
    Give a number:
    2
    Give a number:
    56
    Give a number:
    14
    Element 0: 10
    Element 1: 99
    Element 2: 2
    Element 3: 0
    Element 4: 1
    Element 5: 4
    Element 6: 8
    Element 7: 2
    Element 8: 56
    Element 9: 14
    -----------------------------------
    Element 0: 0
    Element 1: 10
    Element 2: 99
    Element 3: 2
    Element 4: 1
    Element 5: 2
    Element 6: 4
    Element 7: 8
    Element 8: 14
    Element 9: 56
    Press any key to continue . . .

    This is my output, with a smaller amount of numbers it works...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void initializeMinima (int numbers[], int pointers[])
    'pointers' aren't actually pointers.
    Code:
        int temp,
    temp isn't a pointer either.
    Code:
          /* swapping minima */
          temp = pointers[counter];
          pointers[counter] = pointers[index];
          pointers[index] = temp;
    Somewhere in there you should be getting complaints about converting to/from an integer/pointer without a cast if you really do have your warnings enabled.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    initializeMinima is poorly named, it ought to be called sort or something like that, since it is supposed to sort the array.

    Your problem is you have two arrays that refer to essentially the same data. This is a common enough technique and is often called "parallel arrays". The problem is, your sort algorithm only swaps elements in one array, so they are no longer parallel. You can't properly find the minima in numbers after the first swap, since the data/indexes for pointers don't match up any more. If you want to leave the original numbers array alone, you have to create a copy of that, and swap it every time you swap elements in pointers.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    I know, but I'm an university student, we have to call the functions like that...
    And yes Anduril, I already tried it that way, also didn't work. And I don't think I have to create a copy, I can do it without a copy via pointers.

    And I thought it was possible to put an already stored adres in an integer?

    Edit: edited my code, made temp a pointer and made the parameters pointers, except numbers, still doesn't work.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kevinstrijbos View Post
    And I thought it was possible to put an already stored adres in an integer?
    Why would you try? Just use a pointer.

    1. Fill up an array with numbers.
    2. Set an array of pointers to store the address of each element in the first array.
    3. Sort the second array based on the value of where they point.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kevinstrijbos View Post
    Strange, I got my warning settings on wall... Corrected it now, still doesn't work.
    Right, so this was the output from your original code:

    test.c: In function ‘main’:
    test.c:33:5: warning: passing argument 2 of ‘initializeMinima’ from incompatible pointer type
    test.c:8:6: note: expected ‘int *’ but argument is of type ‘int **’
    test.c: At top level:
    test.c:44:6: error: conflicting types for ‘initializeArray’
    test.c:7:6: note: previous declaration of ‘initializeArray’ was here
    test.c: In function ‘initializeArray’:
    test.c:50:27: warning: assignment makes integer from pointer without a cast

    One of those is an error.

    Maybe you need to post your "corrected" code...

    Also, why are you using two arrays for this?

    And I don't think I have to create a copy, I can do it without a copy via pointers.
    You don't need a copy or pointers.
    Last edited by MK27; 01-17-2012 at 03:22 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Alright, I did it the way you said. The indices of my pointer array are the same as my numbers array, so: *pointers[0] = numbers[0],... when I call my function.

    Code:
    void initializeMinima (int *pointers[])
    {
    	int *temp, index, minima, counter, smallCounter;
    
    
    	for (counter = 0; counter < MAX_LENGTH - 1 ;counter++)
    	{
          minima = *pointers[counter];
          index = counter;
    
    
          for (smallCounter = counter + 1; smallCounter < MAX_LENGTH ;smallCounter++)
          {
             if (*pointers[counter] < minima)
             {
                minima = *pointers[counter];
                index = smallCounter;
             }
          }
    
    
          /* swapping minima */
          temp = pointers[counter];
          pointers[counter] = pointers[index];
          pointers[index] = temp;      
       }
    }
    This is my code, now it doesn't even change the numbers anymore? Damn, I hate pointers.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What type of sort are you even trying to do?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    From small to big.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    MK27, it's an excercise for school, we have to do it that way.
    And I know, I sent my code to another student (integers are the same amount of bytes on his computer) and he also got warnings and errors. I really get none, I use Microsoft Visual Studio?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by kevinstrijbos View Post
    From small to big.
    I think quzah meant, what algorithm? It looks more or less like bubble sort to me, but I still don't understand the point of the parallel array. If the goal is to just sort the array, you don't need that at all.

    it's an excercise for school, we have to do it that way
    Fair enough.

    You need to post all your corrected, or at least current, code. The program is not very long, so no big deal.

    If I just replace the one function, I now get:

    test.c: In function ‘main’:
    test.c:33:5: warning: passing argument 2 of ‘initializeMinima’ from incompatible pointer type
    test.c:8:6: note: expected ‘int *’ but argument is of type ‘int **’
    test.c: At top level:
    test.c:44:6: error: conflicting types for ‘initializeArray’
    test.c:7:6: note: previous declaration of ‘initializeArray’ was here
    test.c: In function ‘initializeArray’:
    test.c:50:27: warning: assignment makes integer from pointer without a cast
    test.c: At top level:
    test.c:53:6: error: conflicting types for ‘initializeMinima’
    test.c:8:6: note: previous declaration of ‘initializeMinima’ was here

    I think MSVS is more C++ oriented, BTW, and not so good for C.
    Last edited by MK27; 01-17-2012 at 03:38 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can i get some help with sorting arrays
    By airitout717 in forum C++ Programming
    Replies: 1
    Last Post: 12-03-2011, 04:15 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. Help with sorting arrays
    By Silence in forum C Programming
    Replies: 5
    Last Post: 05-17-2002, 11:05 AM
  5. Sorting Arrays
    By Jax in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 12:35 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