10.10 5*5 Matrix, Middle largest, 4 corners 4 min elements

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.