简体   繁体   English

函数未将值返回给其他函数(C)

[英]Function not returning value to other function (C)

I'm writing this program for class in which I have to compute the equivalent resistance in series. 我正在为该课程编写该程序,其中我必须串联计算等效电阻。 The description of the Homework is: "The present homework is concerned with the use of pointers in computing the equivalent resistance to resistors connected in series. The number of resistors N connected in series and their resistance values R[0], R[1], …, R[N-1] are user-supplied...the inputting of the number of resistors N and the one-dimensional array elements R[0], R[1], …, R[N-1] is done within the “input” prototype function...these values in turn are passed to the “series” prototype function, where the computation of the equivalent resistance Req is performed... the input and output values are outputted to the console from within the “input” function. 对作业的描述是:“当前作业涉及在计算串联电阻的等效电阻时使用指针。串联电阻N的数量及其电阻值R [0],R [1] ,...,R [N-1]由用户提供...输入电阻N的数量和一维阵列元素R [0],R [1],...,R [N-1]为在“输入”原型函数中完成...这些值又传递到“系列”原型函数,在其中执行等效电阻Req的计算...输入和输出值从内部输出到控制台“输入”功能。

-The prototype functions “input” and “series” are to be both placed before the “main” function, with the prototype function “series” placed between the “input” and “main” functions." -原型功能“输入”和“系列”都应放置在“主要”功能之前,原型功能“系列”应放置在“输入”和“主要”功能之间。”

Code: 码:

#include <stdio.h>
#define x 100
#define y 10000
float series(int N, float R[]);

void input() {
    printf("\n---------------Compute equivalent resistance in series!---------------\n");
    int N;
    float R[y];

    printf("\nPlease enter amount of resistors: \n");
    scanf_s("%d", &N);

    for (int i = 1; i <= N; i++) {
        printf("\nEnter resistance for resistor %d: \n", i);
        scanf_s("%f", &R[i]);
    }

    series(N, R);

    printf("\n");

    for (int i = 1; i <= N; i++) {
        printf("The resistance of R[%d] is: %.2f.\n", i, R[i]);
    }
    printf("\nThe equivalent resistance is: %.2f Ohms.", Req);
}

float series(int N, float R[]) {
    float Req = 0;

    for (int i = 1; i <= N; i++) {
        Req += R[i];
    }
    return Req;
}

int main() {

    input();

    getchar();
    return 0;
}

My problem is that Req isn't being returned to the 'input' function in order to output the Equivalent resistance. 我的问题是Req不会返回到“输入”功能以输出等效电阻。 Please help. 请帮忙。 Thank You 谢谢

You're never assigning the result of series to a variable. 您永远不会将系列的结果分配给变量。

float req_ret;
req_ret = series (N, R);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM