• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

C Programming help!

MxPhenom 216

ASIC Engineer
Joined
Aug 31, 2010
Messages
13,188 (2.43/day)
Location
Loveland, CO
System Name Main Stack
Processor AMD Ryzen 7 9800X3D
Motherboard Asus X870 ROG Strix-A - White
Cooling Air (temporary until 9070xt blocks are available)
Memory G. Skill Royal 2x24GB 6000Mhz C26
Video Card(s) Powercolor Red Devil Radeon 9070XT 16G
Storage Samsung 9100 Gen5 1TB | Samsung 980 Pro 1TB (Games_1) | Lexar NM790 2TB (Games_2)
Display(s) Asus XG27ACDNG 360Hz QD-OLED | Gigabyte M27Q-P 165Hz 1440P IPS | LG 24" 1440 IPS 1440p
Case HAVN HS420 - White
Audio Device(s) FiiO K7 | Sennheiser HD650 + Beyerdynamic FOX Mic
Power Supply Corsair RM1000x ATX 3.1
Mouse Razer Viper v3 Pro
Keyboard Corsair K65 Plus 75% Wireless - USB Mode
Software Windows 11 Pro 64-Bit
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);

printf ("Enter an integer value for R2: ");

scanf ("%d", &R2);

printf ("Enter an integer value for R3: ");

scanf ("%d", &R3);

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

printf ("Parallel Resistance for 3 given resistors: 1 / (1 / %d + 1 / %d + 1 / %d) = %lf", R1, R2, R3, parallel_resistance );

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.
 
Try this:

Code:
...
parallel_resistance = 1.0 / (1.0 / R1 + 1.0 / R2 + 1.0 / R3);
...
 
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
 
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.
 
Try this:

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

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

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.
 
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
 
You can do it in another way, but using c++
Code:
#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;



}
 
You must declare the variables R1, R2 and R3 as float because you are performing a division operation.
 
Code:
#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
 
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.
 
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...
 
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.
 
Back
Top