#include <stdio.h>
void operate(int *); void print_data(int *); int main(int argc,char *argv[]) { int arr[5][5] = {{35,34,33,32,31},{30,29,28,27,26},{25,24,23,22,21},{20,19,18,17,16},{15,14,13,12,11}},*p,i; p = arr; printf("the soure data:\n"); print_data(p); operate(p); printf("the result is :\n"); print_data(p); system("pause"); return 0; }
void operate(int *arr) { int i,j,temp; int *p,*max,*min; p = arr; max = p; min = p; for (i = 0; i < 5; i++) { for (j = 0; j < 5;j++) { if (*(p + 5 * i + j) > *max) { max = p + 5 * i + j; } if (*(p + 5 * i + j) < *min) { min = p + 5 * i + j; } } } p = arr; temp = *(p + 5 * 2 + 2); *(p + 5 * 2 + 2) = *max; *max = temp; p = arr; temp = *p; *p = *min; *min = temp; p = arr; min = p + 1; for (i = 0; i < 5; i++) { for (j = 0; j < 5;j++) { if ((p + 5 * i + j != p) && *(p + 5 * i + j) < *min) { min = p + 5 * i + j; } } } p =arr; temp = *(p + 0 * 5 + 4); *(p + 0 * 5 + 4) = *min; *min = temp; p = arr; min = p + 1; for (i = 0; i < 5; i++) { for (j = 0; j < 5;j++) { if ((p + 5 * i + j != p) && (p + 5 * i + j != (p + 0 * 5 + 4)) && *(p + 5 * i + j) < *min) { min = p + 5 * i + j; } } } p =arr; temp = *(p + 4 * 5 + 0); *(p + 4 * 5 + 0) = *min; *min = temp; p = arr; min = p + 1; for (i = 0; i < 5; i++) { for (j = 0; j < 5;j++) { if ((p + 5 * i + j != p) && (p + 5 * i + j != (p + 0 * 5 + 4)) && (p + 4 * 5 + 0 != p + 5 * i + j ) && *(p + 5 * i + j) < *min) { min = p + 5 * i + j; } } } p =arr; temp = *(p + 4 * 5 + 4); *(p + 4 * 5 + 4) = *min; *min = temp; }
void print_data(int *arr) { int i,j,*p; p = arr; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { printf("%4d",*(p + 5 * i + j)); } printf("\n"); } }
|