作业帮 > 综合 > 作业

编写一个函数,其功能是求给定数组中的最小值与最大值的元素

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/04/28 05:00:21
编写一个函数,其功能是求给定数组中的最小值与最大值的元素
输入:第一行是测试数据的组数,第二行是数组的大小(n
编写一个函数,其功能是求给定数组中的最小值与最大值的元素
#include
#include
#include
char * getline() //读取一行,动态分配内存,此函数在网上搜到的,遇到问题先搜搜:)
{
char * line = malloc(100), *linep = line;
size_t lenmax = 100, len = lenmax;
int c;
if(line == NULL)
return NULL;
for(;;) {
c = fgetc(stdin);
if(c == EOF)
break;
if(--len == 0) {
char * linen = realloc(linep, lenmax *= 2);
len = lenmax;
if(linen == NULL) {
free(linep);
return NULL;
}
line = linen + (line - linep);
linep = linen;
}
if((*line++ = c) == '\n')
break;
}
*line = '\0';
return linep;
}
int main()
{
int N,temp,min,max;
char *s = NULL,*tok;
scanf("%d",&N);
s = getline();
while(N--)
{
s = getline();
tok = strtok(s," ");//用空格分割行字串
min = max = atoi(tok);
while(tok!=NULL)
{
tok = strtok(NULL," ");
if(tok==NULL)
break;
temp = atoi(tok);
if(temp>max)
max = temp;
if(temp