1 #include<stdio.h> 2 #define N 5 3 void input(int x[], int n); 4 void output(int x[], int n); 5 void find_min_max(int x[], int n, int* pmin, int* pmax); 6 int main() { 7 int a[N]; 8 int min, max; 9 printf("录入%d个数据;\n", N); 10 input(a, N); 11 printf("数据是;\n"); 12 output(a, N); 13 printf("数据处理...\n"); 14 find_min_max(a, N, &min, &max); 15 printf("输出结果...\n"); 16 printf("min=%d,max=%d\n", min, max); 17 return 0; 18 } 19 void input(int x[], int n) { 20 int i; 21 for (i = 0; i < n; ++i) 22 scanf("%d", &x[i]); 23 } 24 void output(int x[], int n) { 25 int i; 26 for (i = 0; i < n; ++i) 27 { 28 printf("%d", x[i]); 29 } 30 printf("\n"); 31 } 32 void find_min_max(int x[], int n, int* pmin, int* pmax) { 33 int i; 34 *pmin = *pmax = x[0]; 35 for (i = 0; i < n; ++i) 36 { 37 if (x[i] < *pmin) 38 *pmin = x[i]; 39 else if (x[i] > *pmax) 40 *pmax = x[i]; 41 } 42 }
问题1;找到一组数中最大最小数
问题2;指向x[0]的数值
1 #include<stdio.h> 2 #define N 5 3 void input(int x[],int n); 4 void output(int x[],int n); 5 int *find_max(int x[],int n); 6 int main(){ 7 int a[N]; 8 int *pmax; 9 printf("录入%d个数据:\n", N); 10 input(a, N); 11 printf("数据是: \n"); 12 output(a, N); 13 printf("数据处理...\n"); 14 pmax = find_max(a, N); 15 printf("输出结果:\n"); 16 printf("max = %d\n", *pmax); 17 return 0; 18 } 19 void input (int x[],int n){ 20 int i; 21 for(i=0;i<n;++i) 22 scanf("%d",&x[i]); 23 } 24 void output(int x[],int n){ 25 int i; 26 for (i=0;i<n;i++) 27 printf("%d",x[i]); 28 printf("\n"); 29 } 30 int *find_max(int x[],int n){ 31 int max_index=0; 32 int i; 33 for (i=0;i<n;i++) 34 if(x[i]>x[max_index]) 35 max_index=i; 36 return &x[max_index]; 37 }
问题1;找到一组数据中最大值
问题2;可以
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 5 int main() { 6 char s1[N] = "Learning makes me happy"; 7 char s2[N] = "Learning makes me sleepy"; 8 char tmp[N]; 9 10 printf("sizeof(s1) vs. strlen(s1): \n"); 11 printf("sizeof(s1) = %d\n", sizeof(s1)); 12 printf("strlen(s1) = %d\n", strlen(s1)); 13 14 printf("\nbefore swap: \n"); 15 printf("s1: %s\n", s1); 16 printf("s2: %s\n", s2); 17 18 printf("\nswapping...\n"); 19 strcpy(tmp, s1); 20 strcpy(s1, s2); 21 strcpy(s2, tmp); 22 23 printf("\nafter swap: \n"); 24 printf("s1: %s\n", s1); 25 printf("s2: %s\n", s2); 26 27 return 0; 28 }
问题1;s1是20;strlen统计s1长度
问题2;不能,数组不能直接赋值;
标签:int,max,s1,实验,printf,void,pmax From: https://www.cnblogs.com/zhj910/p/18589413