By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

C Array Sort - Pass By Reference.........

P: n/a
Hi all,

I am new to this group and I have question that you may be able to
help me with.

I am trying to learn C but am currently stuck on this.

First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.

My problem is that the fist time I pass the array it is sorted fine,
but when I pass the array a second time (to the next sort function) it
is (obviously) already sorted.
I want to be able to keep the original values of the array (un-sorted)
for each function call and then sort the array each time, with each
sort routine.
Does anyone know how I can do this??
I have tried but keep getting it wrong and it is really irrating me!

I know I should use pointers to pass the array, but this is where I am
stuck.

I'd really aprechiate any help.

Thanks in advance,
Ritchie.
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"ritchie" <ga******@gofree.indigo.ie> wrote in message
news:98*************************@posting.google.co m...
Hi all,

I am new to this group and I have question that you may be able to
help me with.

I am trying to learn C but am currently stuck on this.

First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.

My problem is that the fist time I pass the array it is sorted fine,
but when I pass the array a second time (to the next sort function) it
is (obviously) already sorted.
I want to be able to keep the original values of the array (un-sorted)
for each function call and then sort the array each time, with each
sort routine.
Does anyone know how I can do this??
I have tried but keep getting it wrong and it is really irrating me!

I know I should use pointers to pass the array, but this is where I am
stuck.
If you pass the name of an array to a function, it will
automatically 'decay' to a pointer to its first element,
so simply define the parameter as a pointer to the array's
element type. You'll also need to pass the array's size,
since this information is not preserved when passing an
array to a function:

void func(int *arr, size_t elems)
{
size_t i = 0;
for(i = 0; i < elems; ++i)
; /* whatever */
}

int main()
{
int arr[5] = {0};
func(arr, sizeof arr / sizeof *arr);
return 0;
}


I'd really aprechiate any help.


It would really help if you showed us the actual code you're using,
combined with your specific questions.

About sending unsorted data to each subsequent function:
Simply make a copy of the original unsorted data, and
after each sort, restore the array from the copy.

void copy(int *dest, const int *source, size_t elems)
{
size_t i = 0;
for(i = 0; i < elems; ++i)
dest[i] = source[i];
}

void sort1(int *arr, size_t elems)
{
/* whatever */
}

void sort2(int *arr, size_t elems)
{
/* whatever */
}

int main(void)
{
int unsorted[] = {5, 2, 3, 6, 1, 4};
int array[sizeof unsorted / sizeof *unsorted];
size_t how_many = sizeof unsorted / sizeof *unsorted;

copy(array, unsorted, how_many);
sort1(array, how_many);

copy(array, unsorted, how_many);
sort2(array, how_many);

/* etc */
return 0;
}

-Mike
Nov 13 '05 #2

P: n/a
ritchie wrote:
[...]
First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.

My problem is that the fist time I pass the array it is sorted fine,
but when I pass the array a second time (to the next sort function) it
is (obviously) already sorted.
I want to be able to keep the original values of the array (un-sorted)
for each function call and then sort the array each time, with each
sort routine. [...]


You could keep two arrays, an "original" and a "working
copy." Never change the original array (after filling it
in the first place), but copy it to the working array before
each sort and let the sort rearrange the working array.
Pseudocode:

int orig[SIZE], copy[SIZE];
fill orig[] with data
memcpy (copy, orig, sizeof copy);
insertionsort(copy);
memcpy (copy, orig, sizeof copy);
heapsort(copy);
memcpy (copy, orig, sizeof copy);
bletcherousbubblesort(copy);
...

Or, if the computation that generates the original content
is easily re-executed, just re-execute it before each sort:

int array[SIZE];
fill array[] with data;
insertionsort(array);
fill array[] with data; // same values
heapsort(array);
fill array[] with data; // same values
bletcherousbubblesort(array);
...

--
Er*********@sun.com
Nov 13 '05 #3

P: n/a


Eric Sosman wrote:
ritchie wrote:
[...]
First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.

My problem is that the fist time I pass the array it is sorted fine,
but when I pass the array a second time (to the next sort function) it
is (obviously) already sorted.
I want to be able to keep the original values of the array (un-sorted)
for each function call and then sort the array each time, with each
sort routine. [...]

You could keep two arrays, an "original" and a "working
copy." Never change the original array (after filling it
in the first place), but copy it to the working array before
each sort and let the sort rearrange the working array.
Pseudocode:


Another alternative would be instead of making a copy of
the array, make an array of pointers that point to each
element in the original array. Then you can sort the array of
pointers and leave the original array unchanged. Not much
would be gained with object being int, but for a large
data type (ie. struct) this might be the better way to do it.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void PointertoOrigin( int *a, int **pa, size_t);
void bubblesort(void *base, size_t n, size_t size,
int (*cmp)(const void *el1, const void *el2));
void swap(void *e1, void *e2, size_t size);
int cmp(const void *e1, const void *e2);
void PrintArrays(int *a, int **pa,size_t n);

int main(void)
{
int array[10] = {4,3,6,1,8,10,2,5,7,9};
int *parray[10];

puts("The unsorted arrays");
PointertoOrigin(array,parray,10); /* point to each element */
PrintArrays(array,parray,10);
puts("Using bubblesort:");
bubblesort(parray,10, sizeof(*parray),cmp);
PrintArrays(array,parray,10);
puts("Pointer back to unsorted");
PointertoOrigin(array,parray,10);
PrintArrays(array,parray,10);
puts("Using qsort:");
qsort(parray,10,sizeof *parray, cmp);
PrintArrays(array,parray,10);
return 0;
}
void PointertoOrigin( int *a, int **pa, size_t n)
{
size_t i;

for(i = 0; i < n;i++)
pa[i] = &a[i];
return;
}

void bubblesort(void *base, size_t n, size_t size,
int (*cmp)(const void *el1, const void *el2))
{
size_t i, sorted = 0;
char *p = (char *) base;

while(!sorted)
{
sorted = 1;
for(i = 0;i < n-1; i++)
{
if(cmp(p+(i*size),p+((i+1)*size))>0)
{
swap(p+(i*size),p+((i+1)*size),size);
sorted = 0;
}
}
}
}

void swap(void *e1, void *e2, size_t size)
{
char buf[256], *p1 = (char *)e1, *p2 = (char *)e2;
size_t ms;

for(ms = size; 0< ms; )
{
size_t m = ms < sizeof(buf)?ms:sizeof(buf);
memcpy(buf,p1,m);
memcpy(p1,p2,m);
memcpy(p2,buf,m);
ms -= m, p1+=m, p2+=m;
}
return;
}

int cmp(const void *e1, const void *e2)
{
int *i1 = *(int **)e1, *i2 = *(int **)e2;

return (*i1<*i2)?-1:(*i1!=*i2);
}

void PrintArrays( int *a, int **pa, size_t n)
{
size_t i;

printf("The unsorted array: ");
for(i = 0; i < n;i++) printf("%d ",a[i]);
printf("\nThe pointer array : ");
for(i = 0; i < n;i++) printf("%d ", *pa[i]);
puts("\n");
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #4

P: n/a
Hi,

Just want to thank everyone who replied to my post.
It was all really helpful.

Thanks again,
Ritchie
Nov 13 '05 #5

P: n/a
Hi,
Just wanted to ask if you could help me with this program?

I don't fully understand what's going on in this program, (i'm still a
newbie!).
void func(int *arr, size_t elems)
Could you please explain 'size_t elms', I know that this is probally
size of the elements of the array, but still don't fully understand
how this should be implemented in function declarations, passes to
functions...

And also, func(arr, sizeof arr / sizeof *arr);
Thanks again,
Ritchie

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<xl****************@newsread2.news.pas.earthl ink.net>... "ritchie" <ga******@gofree.indigo.ie> wrote in message
news:98*************************@posting.google.co m...
Hi all,

I am new to this group and I have question that you may be able to
help me with.

I am trying to learn C but am currently stuck on this.

First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.

My problem is that the fist time I pass the array it is sorted fine,
but when I pass the array a second time (to the next sort function) it
is (obviously) already sorted.
I want to be able to keep the original values of the array (un-sorted)
for each function call and then sort the array each time, with each
sort routine.
Does anyone know how I can do this??
I have tried but keep getting it wrong and it is really irrating me!

I know I should use pointers to pass the array, but this is where I am
stuck.


If you pass the name of an array to a function, it will
automatically 'decay' to a pointer to its first element,
so simply define the parameter as a pointer to the array's
element type. You'll also need to pass the array's size,
since this information is not preserved when passing an
array to a function:

void func(int *arr, size_t elems)
{
size_t i = 0;
for(i = 0; i < elems; ++i)
; /* whatever */
}

int main()
{
int arr[5] = {0};
func(arr, sizeof arr / sizeof *arr);
return 0;
}


I'd really aprechiate any help.


It would really help if you showed us the actual code you're using,
combined with your specific questions.

About sending unsorted data to each subsequent function:
Simply make a copy of the original unsorted data, and
after each sort, restore the array from the copy.

void copy(int *dest, const int *source, size_t elems)
{
size_t i = 0;
for(i = 0; i < elems; ++i)
dest[i] = source[i];
}

void sort1(int *arr, size_t elems)
{
/* whatever */
}

void sort2(int *arr, size_t elems)
{
/* whatever */
}

int main(void)
{
int unsorted[] = {5, 2, 3, 6, 1, 4};
int array[sizeof unsorted / sizeof *unsorted];
size_t how_many = sizeof unsorted / sizeof *unsorted;

copy(array, unsorted, how_many);
sort1(array, how_many);

copy(array, unsorted, how_many);
sort2(array, how_many);

/* etc */
return 0;
}

-Mike

Nov 13 '05 #6

P: n/a
On 22 Nov 2003 13:06:22 -0800, in comp.lang.c , ri*********@yahoo.com
(ritchie) wrote:
void func(int *arr, size_t elems)
Could you please explain 'size_t elms',


"func" is a function taking two arguments, a pointer to an int and a
size_t. The pointer is presumably in fact the start of an array, and
elems is its length.
I know that this is probally
size of the elements of the array, but still don't fully understand
how this should be implemented in function declarations, passes to
functions...
Call it with appropriate arguments, for example like this:
func(arr, sizeof arr / sizeof *arr);


arr is presumably declared as
int arr[SOMESIZE];
sizeof arr is the size of the whole array
sizeof (*arr) is the size of its first element
so the ratio of the two is the numbe of elements in the array

Be warned that this only works for real arrays, not for malloc'ed
memory blocks.

Thanks again,
Ritchie

"Mike Wahler" <mk******@mkwahler.net> wrote in message news:<xl****************@newsread2.news.pas.earthl ink.net>...
"ritchie" <ga******@gofree.indigo.ie> wrote in message
news:98*************************@posting.google.co m...
> Hi all,
>
> I am new to this group and I have question that you may be able to
> help me with.
>
> I am trying to learn C but am currently stuck on this.
>
> First of all, I have a function for each sort (Bubble, insertion,
> selection..).
> I have an array of int's and am passing them to each sort function.
>
> My problem is that the fist time I pass the array it is sorted fine,
> but when I pass the array a second time (to the next sort function) it
> is (obviously) already sorted.
> I want to be able to keep the original values of the array (un-sorted)
> for each function call and then sort the array each time, with each
> sort routine.
> Does anyone know how I can do this??
> I have tried but keep getting it wrong and it is really irrating me!
>
> I know I should use pointers to pass the array, but this is where I am
> stuck.


If you pass the name of an array to a function, it will
automatically 'decay' to a pointer to its first element,
so simply define the parameter as a pointer to the array's
element type. You'll also need to pass the array's size,
since this information is not preserved when passing an
array to a function:

void func(int *arr, size_t elems)
{
size_t i = 0;
for(i = 0; i < elems; ++i)
; /* whatever */
}

int main()
{
int arr[5] = {0};
func(arr, sizeof arr / sizeof *arr);
return 0;
}

>
> I'd really aprechiate any help.


It would really help if you showed us the actual code you're using,
combined with your specific questions.

About sending unsorted data to each subsequent function:
Simply make a copy of the original unsorted data, and
after each sort, restore the array from the copy.

void copy(int *dest, const int *source, size_t elems)
{
size_t i = 0;
for(i = 0; i < elems; ++i)
dest[i] = source[i];
}

void sort1(int *arr, size_t elems)
{
/* whatever */
}

void sort2(int *arr, size_t elems)
{
/* whatever */
}

int main(void)
{
int unsorted[] = {5, 2, 3, 6, 1, 4};
int array[sizeof unsorted / sizeof *unsorted];
size_t how_many = sizeof unsorted / sizeof *unsorted;

copy(array, unsorted, how_many);
sort1(array, how_many);

copy(array, unsorted, how_many);
sort2(array, how_many);

/* etc */
return 0;
}

-Mike


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
Nov 13 '05 #7

P: n/a
ritchie wrote:

Just wanted to ask if you could help me with this program?

I don't fully understand what's going on in this program, (i'm
still a newbie!).
> void func(int *arr, size_t elems)


Could you please explain 'size_t elms', I know that this is
probally size of the elements of the array, but still don't
fully understand how this should be implemented in function
declarations, passes to functions...

And also,
func(arr, sizeof arr / sizeof *arr);


Don't toppost. Do snip. Failure to do those things has lost most
of the context of your message.

The first quote above appears to be from the function prototype.
size_t is a type, capable of holding a suitable range of numbers
to describe things. arr appears to be a pointer to an array of
ints, and elems appears to be a count of the number of elements
present in that array.

The second quote appears to be a call to that function, made a a
point where the array declaration of arr is visible. Thus sizeof
arr is the overall size of that array, and sizeof *arr is the size
of one item in the array, making the quotient the count of
elements in that particular array.

Next time, snip areas not germane to you enquiry, and either
intermix your questions with the quoted data, or place them at the
end, as appropriate. Also do not remove attributions for any
material you quote.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.