Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #74985 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-11-05 09:09 -0800 |
| Last post | 2015-11-07 12:24 -0800 |
| Articles | 4 on this page of 44 — 7 participants |
Back to article view | Back to comp.lang.c
Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-05 09:09 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-05 17:44 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 00:49 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 03:01 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:09 -0800
Re: Pointers: misunderstanding Barry Schwarz <schwarzb@dqel.com> - 2015-11-05 11:40 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:08 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 11:33 +0000
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 08:53 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 03:16 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:30 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 03:32 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 04:58 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 09:00 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:25 -0800
Re: Pointers: misunderstanding supercat@casperkitty.com - 2015-11-06 10:07 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 12:31 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 20:08 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 04:59 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 13:06 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 08:50 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 17:00 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:17 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 17:22 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:29 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 17:53 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:22 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 17:24 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:33 -0800
Re: Pointers: misunderstanding Richard Heathfield <rjh@cpax.org.uk> - 2015-11-06 18:29 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:42 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 20:24 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-06 17:51 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:37 -0800
Re: Pointers: misunderstanding Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-11-07 13:36 +0000
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-07 08:17 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-07 12:04 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-06 08:55 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 09:23 -0800
Re: Pointers: misunderstanding Philip Lantz <prl@canterey.us> - 2015-11-06 03:26 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:48 -0800
Re: Pointers: misunderstanding Barry Schwarz <schwarzb@dqel.com> - 2015-11-07 00:35 -0800
Re: Pointers: misunderstanding Alla _ <modelling.data@gmail.com> - 2015-11-06 23:48 -0800
Re: Pointers: misunderstanding Keith Thompson <kst-u@mib.org> - 2015-11-07 12:24 -0800
Page 3 of 3 — ← Prev page 1 2 [3]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-06 23:48 -0800 |
| Message-ID | <eb0e631c-2edb-4c8f-8279-fe2ea7310b39@googlegroups.com> |
| In reply to | #74985 |
Here is the final version that has temp in it (I now have also the one
without temp variable). I am sorry if I missed something, but I still don't
see why the program doesn't swap 6 and 0.
The array before the sort:
34 -5 6 0 12 100
The array after the sort:
-5 6 0 12 34 100
// Program to sort an array of integers into ascending order
#include <stdio.h>
void sort (int* array_int, int n)
{
int temp;
int* pointer_1 = array_int;
int* array_end = &array_int[n - 1];
int* pointer_2 = pointer_1 + 1;
while (pointer_1 < array_end - 1)
{
while(pointer_2 < array_end)
{
if (*pointer_1 > *pointer_2)
{
temp = *pointer_1;
*pointer_1 = *pointer_2;
*pointer_2 = temp;
}
pointer_1++;
pointer_2++;
}
}
}
int main (void)
{
int i;
int array[6] = {34, -5, 6, 0, 12, 100};
printf("The array before the sort:\n");
for (i = 0; i < 6; i++)
printf("%i ", array[i]);
sort(array, 6);
printf("\nThe array after the sort: \n");
for (i = 0; i < 6; i++)
printf("%i ", array[i]);
printf("\n");
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Barry Schwarz <schwarzb@dqel.com> |
|---|---|
| Date | 2015-11-07 00:35 -0800 |
| Message-ID | <1ldr3bhvh5b4p3bii0tq22i5ua5i96f2mq@4ax.com> |
| In reply to | #75219 |
On Fri, 6 Nov 2015 23:48:36 -0800 (PST), Alla _ <modelling.data@gmail.com> wrote: >Here is the final version that has temp in it (I now have also the one >without temp variable). I am sorry if I missed something, but I still don't >see why the program doesn't swap 6 and 0. What steps have you taken to debug the program? Does you compiler provide the ability to step through your code one statement at a time and show you what variables have changed? If not, you can still take a piece of paper and play computer yourself, executing each step manually. It should not take you that long to find the point at which a comparison you need to perform is not happening. From there you can decide whether the error is in your algorithm or you made a mistake in implementing it. -- Remove del for email
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-11-06 23:48 -0800 |
| Message-ID | <38d3fcac-3453-4b66-a476-a5b7ed697ebd@googlegroups.com> |
| In reply to | #74985 |
Here is the final version that has temp in it (I now have also the one
without temp variable). I am sorry if I missed something, but I still don't
see why the program doesn't swap 6 and 0.
The array before the sort:
34 -5 6 0 12 100
The array after the sort:
-5 6 0 12 34 100
// Program to sort an array of integers into ascending order
#include <stdio.h>
void sort (int* array_int, int n)
{
int temp;
int* pointer_1 = array_int;
int* array_end = &array_int[n - 1];
int* pointer_2 = pointer_1 + 1;
while (pointer_1 < array_end - 1)
{
while(pointer_2 < array_end)
{
if (*pointer_1 > *pointer_2)
{
temp = *pointer_1;
*pointer_1 = *pointer_2;
*pointer_2 = temp;
}
pointer_1++;
pointer_2++;
}
}
}
int main (void)
{
int i;
int array[6] = {34, -5, 6, 0, 12, 100};
printf("The array before the sort:\n");
for (i = 0; i < 6; i++)
printf("%i ", array[i]);
sort(array, 6);
printf("\nThe array after the sort: \n");
for (i = 0; i < 6; i++)
printf("%i ", array[i]);
printf("\n");
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-11-07 12:24 -0800 |
| Message-ID | <lnio5d1rsp.fsf@kst-u.example.com> |
| In reply to | #75220 |
Alla _ <modelling.data@gmail.com> writes:
> Here is the final version that has temp in it (I now have also the one
> without temp variable). I am sorry if I missed something, but I still don't
> see why the program doesn't swap 6 and 0.
>
> The array before the sort:
> 34 -5 6 0 12 100
> The array after the sort:
> -5 6 0 12 34 100
>
> // Program to sort an array of integers into ascending order
>
> #include <stdio.h>
>
> void sort (int* array_int, int n)
> {
> int temp;
> int* pointer_1 = array_int;
> int* array_end = &array_int[n - 1];
> int* pointer_2 = pointer_1 + 1;
>
> while (pointer_1 < array_end - 1)
> {
> while(pointer_2 < array_end)
> {
> if (*pointer_1 > *pointer_2)
> {
> temp = *pointer_1;
> *pointer_1 = *pointer_2;
> *pointer_2 = temp;
> }
> pointer_1++;
> pointer_2++;
> }
> }
>
> }
>
> int main (void)
[...]
Initialliy, pointer_1 points to the initial element of the array and
pointer_2 points to the element immediately following it.
The only time they're changed, they're both incremented together, which
means that pointer_1 and pointer_2 always point to adjacent elements,
and they only advance from the beginning of the array to its end.
The only changes you make to elements of the array is to swap the
elements pointed to by element_1 and element_2.
Suppose you start with the smallest value at the end of the array.
Given the above, it might be swapped into the second-to-last position,
but it can never be moved earlier than that.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [standalone]
Page 3 of 3 — ← Prev page 1 2 [3]
Back to top | Article view | comp.lang.c
csiph-web