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. - /**********************************************************
-
Program to create an array of pointers which are
-
sorted without changing the original set of values
-
**********************************************************/
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
void print(int a[], int size);
-
void fill(int a[], int size);
-
void sort(int orig[], int *asc[], int n);
-
void printAdd(int a[], int size);
-
-
-
int main(){
-
int n;
-
int *original = NULL;
-
int **ascend = NULL;
-
-
printf("Enter the number of elements in the array: ");
-
scanf("%d", &n);
-
-
original = (int *) calloc(n, sizeof(int *));
-
ascend = (int **) calloc(n, sizeof(int *));
-
-
fill(original, n);
-
-
printf("Here is your original array");
-
print(original, n);
-
printAdd(original, n);
-
sort(original, ascend, n);
-
printf("Here is your ascending array");
-
print(*ascend, n);
-
printAdd(*ascend, n);
-
printf("Here is your original array");
-
print(original, n);
-
printAdd(original, n);
-
-
-
return 0;
-
}
-
-
-
void sort(int orig[], int **asc, int n){ //sort function
-
int x;
-
for(x = 0; x < n; x++){
-
asc[x] = &orig[x];
-
}
-
-
int i, j;
-
for (i=0; i<n-1; i++) {
-
for (j=0; j<n-1; j++)
-
if (*asc[j+1] < *asc[j]) { /* compare the two neighbors */
-
printf("Swap %d with %d\n", *asc[j+1], *asc[j]);
-
int *tempPtr = asc[j];
-
asc[j] = asc[j+1];
-
asc[j+1] = tempPtr;
-
}
-
}
-
}
-
-
void print(int a[], int size){ //prints an array
-
int x;
-
printf("{ ");
-
for(x = 0; x < size; x++){
-
printf("%d ", *(a + x));
-
}
-
printf("} \n");
-
}
-
-
void printAdd(int a[], int size){ //prints addresses
-
int x;
-
printf("{ ");
-
for(x = 0; x < size; x++){
-
printf("%p ", (a + x));
-
}
-
printf("} \n");
-
}
-
-
void fill(int a[], int size){ //gets values from user
-
int x;
-
int temp;
-
printf("Enter the values delimited by spaces: \n");
-
for(x = 0; x < size; x++){
-
scanf("%d", &temp);
-
a[x] = temp;
-
}
-
}
1 9337
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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;
|
by: Alex Vinokur |
last post by:
===================================
------------- Sorting -------------
Comparative performance measurement
===================================
Testsuite : Comparing Function Objects to...
|
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...
|
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...
|
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},
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |