473,322 Members | 1,241 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Sorting an array of pointers

2
Hi everyone, I'd just like to preface by saying any input or direction in this would be very much appreciated; i've been working on this endlessly and can't figure out what's going wrong.

I'm learning about pointers, and I thought I had that down, so now I'm working on double pointers. First i just ask the user for a number of values, (ie: 4), and then the actual values (4 3 2 1). My program puts the values into the array. I then create a double pointer and then calls my sort function which wants to manipulate just those pointers; not the values in the original array.

I believe my sorting function works, as when I give it 4 3 2 1 is reports
Swap 3 with 4
Swap 2 with 4
Swap 1 with 4
Swap 2 with 3
Swap 1 with 3
Swap 1 with 2
Which does correctly sort the variables. However when I display what is in my ascending pointer array, it displays a mix of garbage and actual values:

Here is your ascending array{ 1 0 25 134520852 }
{ 0x804a014 0x804a018 0x804a01c 0x804a020 }

1 is the only value that is in the correct place, but is a glimmer of hope for me because it does point to the same address location as my original array.

As before, any help, insight, direction, etc would be extremely appreciated.

Expand|Select|Wrap|Line Numbers
  1. /**********************************************************
  2. Program to create an array of pointers which are
  3. sorted without changing the original set of values
  4.  **********************************************************/ 
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. void print(int a[], int size);
  10. void fill(int a[], int size);
  11. void sort(int orig[], int *asc[], int n);
  12. void printAdd(int a[], int size);
  13.  
  14.  
  15. int main(){
  16.   int n;
  17.   int *original = NULL;
  18.   int **ascend = NULL;
  19.  
  20.   printf("Enter the number of elements in the array: ");
  21.   scanf("%d", &n);
  22.  
  23.   original = (int *) calloc(n, sizeof(int *));
  24.   ascend = (int **) calloc(n, sizeof(int *));
  25.  
  26.   fill(original, n);
  27.  
  28.   printf("Here is your original array");
  29.   print(original, n);
  30.   printAdd(original, n);
  31.   sort(original, ascend, n);
  32.   printf("Here is your ascending array");
  33.   print(*ascend, n);
  34.   printAdd(*ascend, n);
  35.   printf("Here is your original array");
  36.   print(original, n);
  37.   printAdd(original, n);
  38.  
  39.  
  40.   return 0;
  41. }
  42.  
  43.  
  44. void sort(int orig[], int **asc, int n){ //sort function
  45.   int x;
  46.   for(x = 0; x < n; x++){
  47.         asc[x] = &orig[x];
  48.   }
  49.  
  50.   int i, j;
  51.   for (i=0; i<n-1; i++) {
  52.     for (j=0; j<n-1; j++)
  53.       if (*asc[j+1] < *asc[j]) {  /* compare the two neighbors */
  54.           printf("Swap %d with %d\n", *asc[j+1], *asc[j]);
  55.               int *tempPtr = asc[j];
  56.               asc[j] = asc[j+1];
  57.               asc[j+1] = tempPtr;
  58.     }
  59.   }  
  60. }
  61.  
  62. void print(int a[], int size){ //prints an array
  63.   int x;
  64.   printf("{ ");
  65.   for(x = 0; x < size; x++){
  66.      printf("%d ", *(a + x));
  67.   }
  68.   printf("} \n");
  69. }
  70.  
  71. void printAdd(int a[], int size){ //prints addresses
  72.   int x;
  73.   printf("{ ");
  74.   for(x = 0; x < size; x++){
  75.      printf("%p ", (a + x));
  76.   }
  77.   printf("} \n");
  78. }
  79.  
  80. void fill(int a[], int size){ //gets values from user
  81.   int x;
  82.   int temp;
  83.   printf("Enter the values delimited by spaces: \n");
  84.   for(x = 0; x < size; x++){
  85.      scanf("%d", &temp);
  86.      a[x] = temp;
  87.   }
  88. }
Sep 23 '07 #1
1 9337
Puddle
2
I was able to get this to work.

If anyone is using this for reference, I couldn't just dereference *ascending and expect it to be print correctly; I made a to print function to print a **int and it worked.
Sep 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
13
by: Nenad Jalsovec | last post by:
using std::list; struct Something{ Something( val ): value( val ){} bool operator<( Something & s ){ return value < s.value; } int value; }; main(){ list< Something * > things;
0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
3
by: SilverWolf | last post by:
I need some help with sorting and shuffling array of strings. I can't seem to get qsort working, and I don't even know how to start to shuffle the array. Here is what I have for now: #include...
3
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C or C++ code...
23
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
77
by: arnuld | last post by:
1st I think of creating an array of pointers of size 100 as this is the maximum input I intend to take. I can create a fixed size array but in the end I want my array to expand at run-time to fit...
1
by: arnuld | last post by:
On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote: An abstract overview of my own program by Ben had helped me focus on the design issue first. I came to know that whole problem went...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.