# C Programming help!



## MxPhenom 216 (Sep 5, 2013)

I am working on an assignment for my programming class in Visual studio and one part is to do this equation.

Equivalent parallel resistance: parallel_resistance = 1 / (1 / R1 + 1 / R2 + 1 / R3), for 3 resistors. R1, R2, and R3 are integers.

I have it setup as follows

.
.
.
.
.
.


> printf ("Enter an integer value for R1: ");
> 
> scanf ("%d", &R1);
> 
> ...



And it crashes right after I give the value for R3. If I do debug, it says something about division by zero. 

Read that this has to do with integer division if the denominator ends up being 0 it will fail, but if you are doing floating point division it will work even if its 0.0. 

IM confused, Cant get it working, and R1 R2 and R3 have to be ints.


----------



## darkhmz (Sep 5, 2013)

Try this:


```
...
parallel_resistance = 1.0 / (1.0 / R1 + 1.0 / R2 + 1.0 / R3);
...
```


----------



## W1zzard (Sep 5, 2013)

use %f with scanf to parse a floating point value, obviously you need to declare the R3 variable as float, too.

and add some checking if R3 is zero to not do the calculation


----------



## hellrazor (Sep 6, 2013)

R1, R2, and R3 can't be ints when you do the math, you're going to have to do a lot of casting and replace the 1s with 1.0fs.


----------



## MxPhenom 216 (Sep 6, 2013)

darkhmz said:


> Try this:
> 
> 
> ```
> ...





W1zzard said:


> use %f with scanf to parse a floating point value, obviously you need to declare the R3 variable as float, too.
> 
> and add some checking if R3 is zero to not do the calculation





hellrazor said:


> R1, R2, and R3 can't be ints when you do the math, you're going to have to do a lot of casting and replace the 1s with 1.0fs.



Thanks for the help guys, I turned in the assignment already with my own solution, but will probably be marked off a little bit since the calculation wont be exactly right, but it was the only way to get it to work. But now I realized I could have type casted the R1, 2, and 3 as floats in the equation and it would have worked, while still having the variables initialized as ints which is a program requirement.


----------



## GSquadron (Oct 4, 2013)

Just curious, did you try assigning R1 = 1, R2 = 1, R3 = 1 before doing anything?
Or R1 > 1, R2 > 1, R3 > 1 so that it wont be taking 0 values


----------



## alexstone (Nov 22, 2013)

You can do it in another way, but using c++

```
#include<iostream>
#include<math.h>
namespace std;
int main(){

double r1, r2,r3,parallel_resistance;

cout<<"Enter R1";
cin>>r1;
cout<<"Enter R2";
cin>>r2;
cout<<"Enter R3";
cin>>r3;

parallel_resistance = 1 / (1 / R1 + 1 / R2 + 1 / R3);

cout<<"Your value is: "<<parallel_resistance;



}
```


----------



## ValerieCasady (Nov 25, 2013)

You must declare the variables R1, R2 and R3 as float because you are performing a division operation.


----------



## punisher186 (Nov 26, 2013)

```
#include <stdio.h>

main()
{
      float R1, R2, R3;
      float resistance;
    
      printf("Enter an integer value for R1: ");
      scanf("%f", &R1);
    
      printf("Enter an integer value for R2: ");
      scanf("%f", &R2);
    
      printf("Enter an integer value for R3: ");
      scanf("%f", &R3);
    
      resistance = 1 / (1 / R1 + 1 / R2 + 1 / R3);
      printf("\nParallel resistance: %.2f", resistance);
      getch();
}
```

Screenshot: http://gyazo.com/f2eab3675e4fb4684e724fc37435155a.png


----------



## de.das.dude (Nov 26, 2013)

you need to write the formula liek this

1/((1/R1)+(1/R2)+1/R3))

computer doesnt understand bodmass. 
so i assume its making a complete mess of that formula.
use brackets when you can/or is necessary.


----------



## de.das.dude (Nov 26, 2013)

ValerieCasady said:


> You must declare the variables R1, R2 and R3 as float because you are performing a division operation.


not necessary. even if the answer storing varible is int, it will still output, but the decimals would be truncated, i think...


----------



## MxPhenom 216 (Nov 26, 2013)

Didn't think people are still replying to this. I got 97% on the assignment with what I turned in. Now that its been a while and i've learned a lot more, I could do it in a few different ways now.


----------

