Sorting random numbers using pointing arrays.

This is a discussion on Sorting random numbers using pointing arrays. within the C Programming forums, part of the General Programming Boards category; Code that generates 25 random numbers. An [iarray] is set to each number. Another array, [parray] is set to the ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    6

    Sorting random numbers using pointing arrays.

    Code that generates 25 random numbers.
    An [iarray] is set to each number.
    Another array, [parray] is set to the address of the [iarray]

    My only issue is when it prints out the new sorted digits, instead of printing out the actual number, it prints out the address.

    I know I'm missing a few "*", just cant find where.

    Any help would be appreciated.

    EDIT: New code, I'm pretty sure its correct now, just feels weird.




    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    #define ARRAY_SIZE 25
    
    void print_array(int *array) 
    {
    	int x;
    	for(x = 0; x < ARRAY_SIZE; x++) 
    	{
    		if(x != ARRAY_SIZE-1)
    			fprintf(stdout, "%d, ", array[x]);
    		else
    			fprintf(stdout, "%d\n", array[x]);
    	}
    }
    
    int main() {
    	int iarray[ARRAY_SIZE];
    
    	int *parray[ARRAY_SIZE];
    
    	int x, y, p, holder;
    
    	
    	// Creating an array to point to the addresses.
    	for(p=0; p < ARRAY_SIZE; p++)
    		parray[p] = &iarray[p];
    	
    	// Seed rand()
    	srand((unsigned int)time(NULL));
    	
    	for(x = 0; x < ARRAY_SIZE; x++)
    		iarray[x] = (int)(rand() % 100);
    	
    	fprintf(stdout, "Before Sort\n---------------\n");
    	print_array(iarray);  
    	
    	
    	// Bubble sort method.
    	for(x = 0; x < ARRAY_SIZE; x++)
    		for(y = 0; y < ARRAY_SIZE-1; y++)
    			if(*parray[y] > *parray[y+1]) 
    			{
    				holder = *parray[y+1];
    				*parray[y+1] = *parray[y];
    				*parray[y] = holder;
    			}
    	
    	fprintf(stdout, "\nAfter Sort\n---------------\n");
    	print_array(*parray);  
    
    
    	getchar();getchar();
    }
    Last edited by jFran; 05-10-2011 at 12:54 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you're using holder to hold the address of a variable that you're swapping, then to be honest with yourself/your readers/the compiler you should make holder a variable that holds addresses (i.e. a pointer).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Works OK for me. Although I can't explain why you need '*' when you call print_array(*parray); After 25 years of C it still confuses me. Let's just all agree that C is just WRONG.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by nonoob View Post
    Works OK for me. Although I can't explain why you need '*' when you call print_array(*parray); After 25 years of C it still confuses me. Let's just all agree that C is just WRONG.
    Ooh, that's a good point I missed that. print_array should take "int *array[]" and you shouldn't use a * when you pass parray into the function.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    That's just it, having the * there is wrong. The function signature for print_array needs to be:
    Code:
    void print_array(int **array)
    You can't take an array of pointers and turn it into a pointer to an array of consecutive integers.
    You'll get lucky if the data was already sorted, by the fact that the first address will be the start of the array, but in all other cases it is totally wrong.

    Edit: or int *array[] which was going to be my first suggestion.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    6
    Yeah, I figured passing a *parray was incorrect.
    But when I have it there, it works, and sorts all the random numbers in order.
    When i remove it, it will print out random addresses

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've got to change both, not one.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    6
    Yeah, I have

    Code:
    print_array(parray)
    and

    Code:
    void print_array(int **array)

    Output for that:
    4454456, 4454460, 4454464... Etc

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Aaaaaaaaaand the other part is to print the variable pointed to.

    Code:
    fprintf(stdout, "%d, ", *array[x]);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  2. Replies: 4
    Last Post: 11-16-2004, 06:29 AM
  3. Replies: 2
    Last Post: 02-23-2004, 05:34 AM
  4. Sorting Random Numbers
    By kid kash in forum C++ Programming
    Replies: 4
    Last Post: 12-07-2002, 03:47 AM
  5. arrays and random numbers
    By bruceness in forum C Programming
    Replies: 3
    Last Post: 10-23-2002, 09:36 PM

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