Anviori
Anviori

Reputation: 33

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.

-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. Please help. Thank You

Upvotes: 0

Views: 171

Answers (1)

tonysdg
tonysdg

Reputation: 1385

You're never assigning the result of series to a variable.

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

Upvotes: 1

Related Questions