函数返回多个件事 [英] Function that returns more than one thing

查看:118
本文介绍了函数返回多个件事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在学习C,我试图使返回更是一件事的功能。我已经写了code,但其中一个值是不是给我正确的输出和我不能找出它为什么这样做的。

 结构NUM {
    INT分钟;
    INT最大;
};结构NUM一些(int数组[]){
    INT I;
    结构NUM结果;
    result.max =阵列[0];
    result.min =阵列[0];
    对于(i = 0; I< = 3;我++){
        如果(result.min>数组[I]){
            result.min =阵列[我]
        }否则如果(result.max<数组[I]){
            result.max =阵列[我]
        }
    }
    返回结果;
}诠释的main()
{
    int数组[] = {7,8,3};
    结构NUM的东西;
    东西=一些(阵列);
    的printf(总为%d,最大为%d,something.min,something.max);
}

程序打印数组的正确的最小值,但它给4200784最大值。


解决方案

 为(i = 0; I< = 3;我++){
// ^^^^

您循环四次。

在有三个元素的数组。

I'm currently learning C, and I'm trying to make a function that returns more that one thing. I've written the code but one of the values is not giving me the right output and I cant figure out why it does that.

struct num {
    int min;
    int max;
};

struct num Some(int Array[]) {
    int i;
    struct num result;
    result.max = Array[0];
    result.min = Array[0];
    for(i = 0; i<=3; i++) {
        if(result.min > Array[i]) {
            result.min = Array[i];
        } else if(result.max < Array[i]) {
            result.max = Array[i];
        }
    }
    return result;
}

int main()
{
    int Array[] = {7,8,3};
    struct num something;
    something  = Some(Array);
    printf("Total is %d and max is %d",something.min,something.max);
}

The program prints the correct minimum value of the array but the max value it gives 4200784.

解决方案

for(i = 0;i<=3;i++){
//        ^^^^

You loop four times.

Over an array that has three elements.

这篇关于函数返回多个件事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆