#include <stdio.h>
#define N 5
/* by row pointer p and two-dimensional array subscript i,j; Gets a pointer to a two-dimensional array element */
int *getelementpoint (int *p,int i,int j)
{
Consider a two-dimensional array as a one-dimensional array, assuming a[i][j] ~ ~ A[i*n + j]
Return (p + i*n + j);
}
/* The value that the Exchange pointer points to */
void swap (int *p,int *q)
{
int temp;
Temp=*p;
*p=*q;
*q=temp;
}
void Specialsort (int *p)
{
int *max,*min;
int i,j;
Max=p;
Min=p;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (*getelementpoint (p,i,j) >*max)
{
Max=getelementpoint (P,I,J);
}
if (*getelementpoint (p,i,j) <*min)
{
Min=getelementpoint (P,I,J);
}
}
}
Interchange maximum and intermediate position elements
Swap (Max, (p+ (N-1) * (n+1)/2));
Swap minimum and top left position elements
Swap (MIN,P);
min=p+1;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if ((*getelementpoint (p,i,j) <*min) && (Getelementpoint (p,i,j)!=p))
{
Min=getelementpoint (P,I,J);
}
}
}
Interchange minimum and upper right position elements
Swap (min, (p+n-1));
min=p+1;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (*getelementpoint (p,i,j) <*min) && (Getelementpoint (p,i,j)!=p) && (Getelementpoint (p,i,j)! = (p + N-1)))
{
Min=getelementpoint (P,I,J);
}
}
}
Swap minimum and bottom left position elements
Swap (Min, (p+ (N-1) *n));
min=p+1;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if (*getelementpoint (p,i,j) <*min) && (Getelementpoint (p,i,j)!=p) && (Getelementpoint (p,i,j)! = (p +n-1) && (Getelementpoint (p,i,j)! = (p+ (N-1) *n)))
{
Min=getelementpoint (P,I,J);
}
}
}
Interchange minimum and bottom right position elements
Swap (min, (p+n*n-1));
}
void Main ()
{
int arr[n][n];
int i,j,*p = arr[0]; Note that P is a row pointer, and initialization points to the first row
void (*f) (int *p);
f = specialsort; F is a function pointer pointing to the Specialsort function
printf ("Please input a%d*%d matrix:\n", n,n);
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
scanf ("%d", &arr[i][j]);
}
}
(*f) (p); Calling the Specialsort function with a function pointer
printf ("\nthe adjusted matrix:\n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf ("%d", arr[i][j]);
}
printf ("\ n");
}
}
10.10 5*5 Matrix, Middle largest, 4 corners 4 min elements