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. 7 25086
"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
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
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/
Hi,
Just want to thank everyone who replied to my post.
It was all really helpful.
Thanks again,
Ritchie
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
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>
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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: its me |
last post by:
Let's say I have a class of people...
Public Class People
Public Sex as String
Public Age as int
Public Name as string
end class
And I declare an array of this class...
|
by: GIMME |
last post by:
My question is ... How do I sort an Array on numeric, not character values ?
In the example below, after sorting the contents are 1,10,2,3 .
How do I get the contents to be 1,2,3,10 ?
...
|
by: Steve Wasser |
last post by:
I need to sort a two-dimensional array. Each day I process a file with 9
comma-delimited fields, and varying amount of records (lines). I want to
pull in each line, split it according to the comma...
|
by: Rene \(Programmer Wannabe\) |
last post by:
Sorry for reposting this question, I asked this a couple of days ago but got
no definite answer as to why the Array class behaves this way. Hope this
time I will have better luck. Please read...
|
by: aprivate |
last post by:
Hi
I tried all the examples in the visual studio IDE but
the array.sort (array,icomparer) examples fails to
work in VB.NET for Smart Devices..
its get an error before it is compiled...
Is...
|
by: Gerrit |
last post by:
Hello,
Is it possible to sort an array with a struct in it?
example:
I have a struct:
public struct mp3file
{
public int tracknr;
|
by: Jan Smith |
last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a
clear answer to my question. Maybe it's not in Array.Sort.
Here's the question:
I initialize an array X with the...
|
by: cindy |
last post by:
i build a string
string eProduct = "#blah#ablah#bbbb";
StringBuilder sb = new StringBuilder(eProduct);
eProduct = sb.Replace("#","").ToString();
string aProducts= eProduct.Split(';');
then I...
|
by: Bob Palank |
last post by:
' Given
Dim v(19) As Integer ' a 20 element array
Dim vSorted(19) As Integer
Dim i As Integer
' The array v() is filled with random numbers
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |