An electrical engineer wishes to calculate the equivalent resistance value for a
ID: 3539505 • Letter: A
Question
An electrical engineer wishes to calculate the equivalent resistance value for an electronic board. The total resistance value depends if the resistors are placed in parallel or series. For resistors in series, the resistance is calculated by adding the values in Ohms (r1+r2+...) of the individual resistors. For resistors in parallel, the resistance is calculated by the following formula: 1 / (1/r1 + 1/r2 + ...).
Write a program that first asks the user if the resistors are placed in series or parallel, then asks the user for the values of resistors in Ohms (we do not know how many resistors - use 0 as sentinel to end entry) and then calculates and prints out the total resistance.
Your program must have two user defined functions: one for the series computations and another for the parallel computations.
. Make sure your program is commented and properly indented.
I am having diffuculty with the parallel ciruit part i tried two different method
method 1)
#include <stdio.h>
main(){
double num[100],sum=0, finalsum;
int i, j;
puts("please enter the values for the resistors 0 to end");
while(num[i+1]!=0)
{
scanf("%lf", &num[i]);
i++;
}
for(j=0; j<i+1; j++)
{
sum=sum+(1/(num[j]));
}
sum = 1/sum;
printf("The sum of array elements is %lf",sum);
}
method 2)
#include <stdio.h>
main(){
double num, sum=0, finalsum;
puts("please enter the values for the resistors 0 to end");
while(num!=0)
{
scanf("%lf", &num);
sum=sum+(1/num);
}
finalsum=1/sum;
printf("the equivalent resistance %lf", finalsum);
}
if you can help me out itd be great :)
Explanation / Answer
Your method 2) needs some modification:
1) Initialize the variable num to zero,failing which it will take up some garbage value.
2) Use a do...while() loop instead of a while() loop so that the scanf(...) function gets executed atleast once.
I would write the method in this way :
method 2)
#include <stdio.h>
main(){
double num = 0, sum=0, finalsum = 0;
puts("please enter the values for the resistors 0 to end");
do
{
scanf("%lf", &num);
if((num <= 0) )
{
break;
}
else
{
sum + = (1/num);
}
} while(1);
if(sum > 0)
{
finalsum=1/sum;
}
printf("the equivalent resistance %lf", finalsum);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.