Computting Equivalent Resistance

Hey everyone, I am struggling with one of my homework problems for my programming class. Here is what I am asked to do : Design a program in C using a one-dimensional array and a prototype function which will compute the equivalent resistance to a user-supplied number and values of resistors connected in parallel. When the compiled program is executed, it will perform a one-shot computation of the equivalent resistance by going through the following steps sequentially:

1) Ask the user to enter on the keyboard the # of resistors connected in parallel. The entered number of resistors will be displayed on the console.

2) Ask the user to enter on the keyboard the values of the resistors. The entered values of the resistors will be represented by the elements of a one- dimensional array, with the values of these elements displayed on the console.

3) Compute the value of the equivalent resistance, and display it on the screen.


This is what I have so far: #include <stdio.h>

int main(void){
int noOfresistors,rnext=0,i;
float resistor, equiresis, rprev;
char abc;

printf("Enter number of resistors connected in parallel: ");
scanf_s("%d%c", &noOfresistors, &abc);

for(i=1;i<=noOfresistors;i++){
printf("Enter value of resistor[%d] :",i);
scanf_s("%d%c", &resistor, &abc);
rnext=resistor;
if(i==1&&i==2){
rprev=(resistor*rnext/resistor);

equiresis = (rprev*rnext/rprev+rnext);
}
}

printf("The equivalent resistance is: %d ohms", &equiresis);

getchar();
}

The program works but doesn't give me a correct output. I believe I messed up with the math/variables. I've been tweaking it and changing it but can't seem to get it right. Any help will be appreciated.
closed account (D80DSL3A)
The math does seem odd.
The formula is: 1/Req = 1/R1 + 1/R2 + ... + 1/Ri
I would do it this way:
1
2
3
4
5
6
7
equiresis = 0;
for(i=1;i<=noOfresistors;i++){
    printf("Enter value of resistor[%d] :",i);
    scanf_s("%f%c", &resistor, &abc);// what is the extra char input for? Is a symbol for ohms being read?
    equiresis += 1/resistor;// accumulate sum of inverses of resistances
}
equiresis = 1/equiresis;// invert Req 

EDIT:
Also, %d in scanf() and printf() is for an integer. Since resistor and equiresis are float variables use %f instead.
Last edited on
For one, I don't think i can be equal to 1 && two. Maybe you meant OR, which is ||. Second, the variable, equiresis, will only be equal to something IF i == 1 && (OR ||) i == 2.
Topic archived. No new replies allowed.