Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to sort an array into ascending and descending order using pointers.

ID: 1934693 • Letter: I

Question

I am trying to sort an array into ascending and descending order using pointers. Here is my program: What is wrong with it ): #include void sort (int *a, int n, char z) { int i, j, temp; if (z == 'u'){ for ( i = 0; i < n; ++i) for ( j = i + 1; j < n; ++j) if ( *(a+i) > *(a+j) ) { temp = *(a+i); *(a+i) = *(a+j); *(a+j) = temp; } } else if (z == 'd'){ for ( i = 0; i < n; ++i) for ( j = i + 1; j < n; ++j) if ( *(a+i) < *(a+j) ) { temp = *(a+i); *(a+i) = *(a+j); *(a+j) = temp; } } } int main(void) { int array[16] = { 34, -5, 6, 0, 12, 100, 56, 22, 44, -3, -9, 12, 17, 22, 6, 11}; int i, j, *p1 = array, *p2 = p1 + 1; char x; void sort (int *a, int n, char z); printf ("To sort, insert 'u' for ascending and 'd' for descending:"); scanf ("%c", &x); if ( x=='u' || x=='d'){ printf (" The array before the sort: "); for ( i = 0; i <16; ++i){ printf ("%i ", *p1); ++p1; } sort (p1, 16, x); p1 = array; printf (" The array after the sort: "); for ( i = 0; i <16; ++i){ printf ("%i ", *p1); ++p1; } printf (" "); } else printf ("Invalid character!!! "); return 0; }

Explanation / Answer

#include #include void main(){ int *arr,i,j,tmp,n; clrscr(); printf("Enter how many data you want to sort : "); scanf("%d",&n); for(i=0;i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote