#include<stdio.h> /*********Begin*********/ int max_check(int *p,int n) { int *a; int i; a=p; for(;a<=p+n;a++) {if(*a<*p) a=p; } return *a; } /*********End**********/ int main(void) { int n,s[110]; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&s[i]); int ans; /*********Begin*********/ ans=max_check(s,n); /*********End**********/ printf("%d",ans ); return 0; }这个代码为什么输不出最大值
时间: 2023-07-06 09:40:46 浏览: 45
这个代码中的 `max_check` 函数并不是返回最大值,而是返回数组中第一个元素的后面那个最小的元素。所以在 `main` 函数中调用 `max_check` 函数返回的是数组中第二大的元素,而不是最大值。如果要返回最大值,可以修改 `max_check` 函数中的代码如下:
```c
int max_check(int *p, int n) {
int *a;
int i;
a = p;
for (; a < p + n; a++) {
if (*a > *p) {
p = a;
}
}
return *p;
}
```
这样就可以输出数组中的最大值了。
相关问题
//求最大字符串 #include<string.h> #include<stdio.h> char* fun(char word[][10],int n) { //********begin***********// //*******end************// } int main(void) { int i; char color[5][10] = {"red", "blue", "yellow", "green", "black" }; char *maxstr; maxstr=fun(color,5); for(i = 0; i < 5; i++) { printf("%s\n", color[i]); } printf("\nthe max is: %s\n", maxstr); return 0; }
以下是可以求出最大字符串的C语言代码:
```
#include <stdio.h>
#include <string.h>
char* fun(char word[][10], int n) {
char* maxString = word[0]; // 先将第一个字符串设为最大字符串
for (int i = 1; i < n; i++) {
if (strlen(word[i]) > strlen(maxString)) { // 如果当前字符串比最大字符串长
maxString = word[i]; // 更新最大字符串
}
}
return maxString;
}
int main() {
char color[5][10] = {"red", "blue", "yellow", "green", "black"};
char* maxStr = fun(color, 5);
printf("The max string is: %s\n", maxStr);
return 0;
}
```
这个程序使用了C语言的字符串函数库中的strlen函数来获取字符串长度,然后进行比较。注意,由于C语言中不能直接返回局部变量的地址,因此我们在函数中返回了最大字符串的指针,而在主函数中使用指针来输出最大字符串的值。
编一个函数,找出其中最大的字符串。(用指针实现),#include<string.h> #include<stdlib.h> /* * 该函数用来实现查找n个字符串中最大的那一个 * a是二维字符数组,每一行存放一个字符串,n为带查找的字符串的个数 */ char * find_max_string(char a[][300],int n) { char *pmax; // 请在下面添加代码 // ********** Begin ********** // ********** End ********** return pmax; }
好的,以下是函数的代码示例:
```c
#include <stdio.h>
#include <string.h>
char* find_max_string(char a[][300], int n) {
char* pmax = a[0];
for (int i = 1; i < n; i++) {
if (strcmp(a[i], pmax) > 0) {
pmax = a[i];
}
}
return pmax;
}
```
在这个函数中,我们首先将指针 `pmax` 初始化为第一个字符串。然后,我们遍历整个字符串列表,比较每个字符串与 `pmax` 的大小关系。如果某个字符串大于 `pmax`,我们更新 `pmax` 的指向。最后,函数返回 `pmax`。
在比较两个字符串大小时,我们使用了 `strcmp()` 函数,该函数返回一个整数值,用于表示两个字符串的大小关系。如果第一个字符串大于第二个字符串,返回值大于 0;如果两个字符串相等,返回值等于 0;如果第一个字符串小于第二个字符串,返回值小于 0。
请注意,在比较字符串大小时,我们使用了 `strcmp()` 函数,而不是简单地比较字符串的长度。这是因为字符串的大小关系不仅与长度有关,还与每个字符的 ASCII 码值有关。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![png](https://img-home.csdnimg.cn/images/20210720083516.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)