Author Message

Hi!
I tried to write a program which will sort the array into
ascending and descending order and print the sorted lists
after I input 10 integers. The program must not change the
original array or create any other integer arrays.
I have to use two pointer arrays. The first pointer array is
rearranged so that it points to the data in ascending sequence.
The second pointer array is rearranged so that it points to
the data in descending sequence.
The out put  will look like:

Ascending    Original    Descending
14                26                57
26                14                41
33                57                33
41                33                26
57                41                14

Here is my code, but I am still confused what I did. I use bubble sort
in this case. My friend it is easiler to use selection sort. Can any one
explain the algorithm of sorting? Thank you.

#include<stdio.h>

void Asending(int*[], int[], int);
void Decending(int list[], int current, int last);

int main(void)
{
int array[10];
int i;
int numbers;
int *pAscend[10];
int *pDescend[10];
int list=0;
int current=10;
int last=0;

printf("Please enter 10 integers: ");
for (i = 0; i < 10; i++)
{
scanf("%d", &numbers);     /* sorry about using scanf again */
array[i] = numbers;
pAscend[i] = &array[i];
pDescend[i] = &array[i];
}

printf("\tAscending\t\tOriginal\t\tDecending\n");

Asending (pAscend, array, current);

for (i = 0; i < 10; i++)
{
printf("\t%d\t\t%d\t\t%d\n", /*Ascend, array[i], Decend*/);
}
printf("\n");
return 0;

Quote:
}

void Asending(int* pAscend[], int **p1, int **p2)
{
int *pAscend[] = list;
int temp;

printf("%d\n", list);

for (**p1 = **p2; **p1 < **p2; **p1++)
if (**p1 < **p2)
{
*p1 = &(**p2);
*p2 = &(**p1);
}
for(**p1 = 0; **p1 < 10; **p1++)
{
printf("\t\t%d\n", pAscend[walker]);
}
return;

Quote:
}

void Decending(int list[], int current, int last)
{
int *Decend = list;
int walker;
int temp;

for (walker = last; walker < current; walker++)
if (list[walker] < list[walker + 1])
{
temp             = list[walker];
list[walker]     = list[walker + 1];
list[walker - 1] = temp;
}
return;

Quote:
}

Mon, 21 Apr 2003 02:05:14 GMT

Quote:

> Hi!
> I tried to write a program which will sort the array into
> ascending and descending order and print the sorted lists
> after I input 10 integers. The program must not change the
> original array or create any other integer arrays.

These are conflicting requirements. You cannot both sort the array and
not change it. Sorting an array involves changing it (except in the
trivial case where it is already sorted).

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Mon, 21 Apr 2003 14:49:04 GMT
Richard Heathfield schrieb:

Quote:

> > Hi!
> > I tried to write a program which will sort the array into
> > ascending and descending order and print the sorted lists
> > after I input 10 integers. The program must not change the
> > original array or create any other integer arrays.

> These are conflicting requirements. You cannot both sort the array and
> not change it. Sorting an array involves changing it (except in the
> trivial case where it is already sorted).

> --
> Richard Heathfield
> "Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
> C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
> K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html

Agreed Richard,
anyway, the OP's text sounds like one of these not very precise
questions in some "mathematic" schoolbooks for children, so it could
be meant to force the student to use an array of pointers (which one
would probably do if he had to sort an array of strings).

To the OP:
(pseudocode)

#include <stdio.h>
int main(void)
{
int numbers[10];
int *sorted[10];
it i;

/*prompt for 10 numbers*/
/*store the 10 numbers in numbers[]*/
/*find the smallest number and store it's address in sorted[0]*/
/*find the next bigger number and store it's address in
sorted[1] and so on for all 10 numbers*/

for(i = 0; i < 10; i++)
{
printf("%d\n", *(sorted + i));
printf("%d\n", *(sorted + 9 - i));
}
return 0;

Quote:
}

--
I don't make my mistakes more than once - I store them carefully and
after some time I take them out again, add some new features and
_reuse_ them.

Mon, 21 Apr 2003 16:20:18 GMT
My impression was that the requirement was to create a sorted copy of
the original array.

--
Steve Horne

Mon, 21 Apr 2003 23:46:34 GMT

Quote:

> My impression was that the requirement was to create a sorted copy of
> the original array.

Actually, on more careful reading, it looks like she's allowed to use
pointer arrays. Silly of me to miss that.

Given an array of int, it's not hard to work up an array of int *.

Here's a solution, not to her problem, but to what I half-remember of
her problem. This is deliberate, so that it doesn't give her an easy
out, but does give her a punt in the right direction. Feel free to point
out errors! (Note: I've used qsort, naturally enough, to do the actual
sorting.)

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

int compintptr(const void *vpi1, const void *vpi2)
{
const int *const *pi1 = vpi1, *const*pi2 = vpi2;
return **pi1 < **pi2 ? -1 : **pi1 > **pi2;

Quote:
}

int main(void)
{
int a[] = {31, 41, 59, 26, 53, 58, 97, 93, 23, 84};
int *p[10] = {0};
int i;

/* set up the pointers */
for(i = 0; i < 10; i++)
{
p[i] = a + i;
}

printf("Show the array, unsorted.\n");
for(i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}

printf("\n");

printf("show that we've got pointers to the right stuff\n");
for(i = 0; i < 10; i++)
{
printf("%d ", *p[i]);
}
printf("\n");

printf("sorting pointers now\n");
qsort(p, 10, sizeof p[0], compintptr);

printf("show that we've got sorted pointers\n");
for(i = 0; i < 10; i++)
{
printf("%d ", *p[i]);
}
printf("\n");

printf("show that the data array remains unsorted\n");
for(i = 0; i < 10; i++)
{
printf("%d ", a[i]);
}

printf("\n");

printf("show off (sigh) - print the data in reverse order\n");
for(i = 9; i >= 0; i--)
{
printf("%d ", *p[i]);
}
printf("\n");

return 0;

Quote:
}

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

Tue, 22 Apr 2003 00:12:50 GMT

Quote:

> My impression was that the requirement was to create a sorted copy of
> the original array.

>>The program must not change the original array or create any other
>>integer arrays.  I have to use two pointer arrays. The first pointer
>>array is rearranged so that it points to the data in ascending sequence.
>>The second pointer array is rearranged so that it points to the data in
>>descending sequence.

How can one create a sorted copy without creating an integer array?
While one could, or example, create a pointer to int, allocate space
for all entries, copy them there, then sort, this strikes me as the
kind of solution one's professor is unlikely to appreciate.  It works
around the letter of the requirement but violates its spirit.

Besides, the OP already stated that (s)he is required to use an array
of pointers, so I think this is not a debatable point.

--

Tue, 22 Apr 2003 02:13:27 GMT
This is my code.

#include <stdio.h>

void Ascending(int Ar[]);
void Descending(int Ar_descending[]);
void Assign_order(int* element1, int* element2);

int main()
{
int i, j, t, k;
int Array[10];
int Ar_Asc_copy[10];
int Ar_Dec_copy [10];

printf("Please enter 10 integers: ");

for(i = 0; i < 10; i++)
{
scanf("%d", &Array[i]);  /* &Array[i] == (Array + i) */
}
for(j = 0; j < 10; j++)
{
Ar_Asc_copy[j] = Array[j];  /* Array[j] == *(Array + j) */
}
printf("\n");

Ascending(Ar_Asc_copy);

for (t = 0; t < 10; t++)
{
Ar_Dec_copy[t] = Ar_Asc_copy[t];
}

Descending(Ar_Dec_copy);

printf("Ascending\tOriginal\tDescending\n");
for(k = 0; k < 10; k++)
{
printf("%5d%16d%16d\n", *(Ar_Asc_copy + k), *(Array + k), *(Ar_Dec_copy +
k));
}

return 0;

Quote:
}

void Ascending(int Ar[])
{
int* ptr;
int tmp, count;
ptr = &Ar[1];

for(tmp = 0; tmp<10 ; tmp++ )
{
for(count = 0; count < 9; count++)
{
if(*(Ar + count) > *(ptr + count) )
{
Assign_order((Ar + count), (ptr + count));
}
}
}

Quote:
}

void Assign_order(int* element1, int* element2)
{
int temp;

temp = *element1;
*element1 = *element2;
*element2 = temp;

Quote:
}

void Descending(int Ar_descending[])
{
int d, e;
int De_Ar[10];
int big_number = 9;
for(d = 0; d < 10; d++)
{
*(De_Ar + d) = *(Ar_descending + big_number);
big_number--;
}
for (e = 0; e < 10; e++)
{
*(Ar_descending + e) = *(De_Ar + e);
}

Quote:
}

Quote:
> Hi!
> I tried to write a program which will sort the array into
> ascending and descending order and print the sorted lists
> after I input 10 integers. The program must not change the
> original array or create any other integer arrays.
> I have to use two pointer arrays. The first pointer array is
> rearranged so that it points to the data in ascending sequence.
> The second pointer array is rearranged so that it points to
> the data in descending sequence.
> The out put  will look like:

> Ascending    Original    Descending
>     14                26                57
>     26                14                41
>     33                57                33
>     41                33                26
>     57                41                14

> Here is my code, but I am still confused what I did. I use bubble sort
> in this case. My friend it is easiler to use selection sort. Can any one
> explain the algorithm of sorting? Thank you.

> #include<stdio.h>

> void Asending(int*[], int[], int);
> void Decending(int list[], int current, int last);

> int main(void)
> {
>  int array[10];
>  int i;
>  int numbers;
>  int *pAscend[10];
>  int *pDescend[10];
>  int list=0;
>  int current=10;
>  int last=0;

>  printf("Please enter 10 integers: ");
>  for (i = 0; i < 10; i++)
>  {
>   scanf("%d", &numbers);     /* sorry about using scanf again */
>   array[i] = numbers;
>   pAscend[i] = &array[i];
>   pDescend[i] = &array[i];
>  }

>  printf("\tAscending\t\tOriginal\t\tDecending\n");

>  Asending (pAscend, array, current);

>  for (i = 0; i < 10; i++)
>  {
>  printf("\t%d\t\t%d\t\t%d\n", /*Ascend, array[i], Decend*/);
>  }
>  printf("\n");
>  return 0;
> }

> void Asending(int* pAscend[], int **p1, int **p2)
> {
>  int *pAscend[] = list;
>  int temp;

>  printf("%d\n", list);

>  for (**p1 = **p2; **p1 < **p2; **p1++)
>   if (**p1 < **p2)
>   {
>    *p1 = &(**p2);
>    *p2 = &(**p1);
>   }
>   for(**p1 = 0; **p1 < 10; **p1++)
>   {
>    printf("\t\t%d\n", pAscend[walker]);
>   }
>   return;
> }

> void Decending(int list[], int current, int last)
> {
>  int *Decend = list;
>  int walker;
>  int temp;

>  for (walker = last; walker < current; walker++)
>   if (list[walker] < list[walker + 1])
>   {
>    temp             = list[walker];
>    list[walker]     = list[walker + 1];
>    list[walker - 1] = temp;
>   }
>   return;
> }

Tue, 22 Apr 2003 15:18:53 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages