help with an easy resistor prob!

Design a program in C which will compute the equivalent resistance to a user-specified number of resistors connected in series if the user types a value of 0 on the keyboard for a runmode operator, and the equivalent resistance to a user-specified number of resistors connected in parallel if the user types a value 1 for the runmode operator. In your design, the selection as to whether the resistors are connected in series or in parallel is to be implemented using the IF/ELSE construct.
NOTE: The runmode operator is simply a variable of type int, with the value of runmode being either runmode=0 or runmode=1.

this is what i have so far.. thank you guyss !!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 #include<stdio.h>
int main(){
	int num,i,runMode,resistors,sum,tNum,fNum;;
	
	while(1){
		printf("enter a value for num to find tNum or fNum:  ");
		scanf("%d",&num);
		
		printf("enter a value for runMode, 0 to compute tNum, 1 to compute fNum:  ");
		scanf("%d",&runMode);

		tNum=0;
		fNum=1;
		resistors=0;
		sum=0;

		if(runMode==0){
			for(i=1;i<=num;i=i+1){
				  tNum=tNum+i;
				  printf("enter values for resistor: ");
				  scanf("%d",&resistors);
				  sum+=resistors;
			}
			printf("tNum for num=%d is %d\n\n",num,tNum);
		}
		else if(runMode==1){
			for(i=1;i<=num;i=i+1){
				   fNum=fNum*i;
				   sum+=1/resistors;
				   sum=1/sum;
			}
				   
			printf("fNum for num=%d is %d\n\n",num,fNum);
		}
	}

		return 0;
	}



					
The while loop
 
while(1)


Should change that to:
1
2
3
4
while(true)
{ 
    // ...Code here... 
}


Creating an endless loop, although that's not really good practice,
What I'd use is a bool variable for the application loop.
1
2
3
4
5
6
7
bool run = true;
while(run)
{
    // ...Code Here...
    if(userCommand == 'q')
        run = false;
}


Something of that sort..
Last edited on
i actually changed fNum to sum on line24 and line33 !
now resistors series run==0 works!!
but parallel doesnt even work it just crashes!!
when i try while(true), it crashes too...
thank u for ur time tho...
ok..thanks!!

but i think the main problems are lines 29 and 30
im having issues figuring out a way to do parallel sum of resistors..
1/R=1/R1+1/R2+....+1/RN

How about this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
 
int main(){
	int num, i, runMode;
	float resistors, sum;
	printf("How many resistors: ");
	scanf("%d", &num);
	
	printf("Enter runMode: 0 to compute series, 1 to compute parallel: ");
	scanf("%d", &runMode);
	
	sum = 0.;
	
	if(runMode == 0)
		for(i = 1; i <= num; ++i){
			printf("Enter value for R%d... ", i);
			scanf("%f", &resistors);
			sum += resistors;
		}
	else if(runMode == 1){
		for(i = 1; i <= num; ++i){
			printf("Enter value for R%d... ", i);
			scanf("%f", &resistors);
			sum += 1. / resistors;
		}
		sum = 1. / sum;
	}
	printf("sum = %5.3f", sum);
	
	return 0;
}
Example 1
*********
How many resistors: 3
Enter runMode: 0 to compute series, 1 to compute parallel: 0
Enter value for R1... 1.2
Enter value for R2... 2.3
Enter value for R3... 3.4
sum = 6.900

Example 2
*********
How many resistors: 3
Enter runMode: 0 to compute series, 1 to compute parallel: 1
Enter value for R1... 1.2
Enter value for R2... 2.3
Enter value for R3... 3.4
sum = 0.640
Ohhhh shittt it works!!
"float" huhhh? it actually makes sense...
thank you!!!
:D
if you dont mind me asking what is "5" in printf("sum = %5.3f", sum) means?
thank you again for your time!!
OK, you're welcome; take a look here:

http://www.cplusplus.com/reference/cstdio/printf/

There are all the format specifiers used in C.
Cool !it's very helpful! thanks :D
Topic archived. No new replies allowed.