编写自定义函数 int max( int *s , int n , int *m ) 找出数组s中最大值,并返回最大值的个数 函数的参数:s指向一个整型数组,n为数组元素的个数,指针m指向的变量,保存数组中的最大值 函数返回值:最大值的个数(>0) 用于测试函数的程序: #include<stdio.h> int main() { int s[ ] [ 5 ] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4 } ; int m , k ; k = max ( &s[0][0] , 20, &m ) ; printf ( "%d %d \n " , m, k ) ; } 程序运行后,如果输出9 3,则表明你写的max 函数是正确的
时间: 2023-07-02 12:11:19 浏览: 59
以下是实现 int max( int *s , int n , int *m ) 函数的代码:
```c
#include <stdio.h>
int max(int *s, int n, int *m) {
int max_val = *s;
int count = 1;
for (int i = 1; i < n; i++) {
if (*(s + i) > max_val) {
max_val = *(s + i);
count = 1;
} else if (*(s + i) == max_val) {
count++;
}
}
*m = max_val;
return count;
}
int main() {
int s[][5] = {{3,1,4,1,5},{9,2,6,5,3},{5,8,9,7,9},{3,2,3,8,4}};
int m, k;
k = max(&s[0][0], 20, &m);
printf("%d %d\n", m, k);
return 0;
}
```
程序输出为:
```
9 3
```
说明函数实现正确。
相关问题
编写函数int max(int *a,int n),返回值为n个数的最大值。
好的,下面是一个求解n个数最大值的函数示例代码:
```c
#include <stdio.h>
int max(int *a, int n) {
int max_val = a[0];
for (int i = 1; i < n; i++) {
if (a[i] > max_val) {
max_val = a[i];
}
}
return max_val;
}
int main() {
int arr[] = {10, 20, 30, 50, 80, 70, 90, 60, 40, 100};
int n = sizeof(arr) / sizeof(arr[0]);
int max_val = max(arr, n);
printf("最大值为:%d\n", max_val);
return 0;
}
```
在上述代码中,我们定义了一个max函数,通过遍历数组来找到最大值,最后将其返回。主函数中我们定义了一个大小为10的整型数组,通过调用max函数计算出数组中最大值,并输出结果。
编写一个函数int max(int *a,int n)返回值为n个数的最大值
好的,以下是一个实现这个功能的函数:
```c
int max(int *a, int n) {
int max_num = a[0]; // 先将第一个数设为最大值
for (int i = 1; i < n; i++) { // 循环遍历数组
if (a[i] > max_num) { // 如果当前数大于最大值
max_num = a[i]; // 将当前数设为最大值
}
}
return max_num; // 返回最大值
}
```
这个函数接受一个整型指针 `a` 和一个整型变量 `n`,表示需要求出最大值的数组和数组长度。函数会遍历整个数组,找出其中最大的数,然后将其返回。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)