• 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
12,945 (2.60/day)
Location
Loveland, CO
System Name Ryzen Reflection
Processor AMD Ryzen 9 5900x
Motherboard Gigabyte X570S Aorus Master
Cooling 2x EK PE360 | TechN AM4 AMD Block Black | EK Quantum Vector Trinity GPU Nickel + Plexi
Memory Teamgroup T-Force Xtreem 2x16GB B-Die 3600 @ 14-14-14-28-42-288-2T 1.45v
Video Card(s) Zotac AMP HoloBlack RTX 3080Ti 12G | 950mV 1950Mhz
Storage WD SN850 500GB (OS) | Samsung 980 Pro 1TB (Games_1) | Samsung 970 Evo 1TB (Games_2)
Display(s) Asus XG27AQM 240Hz G-Sync Fast-IPS | Gigabyte M27Q-P 165Hz 1440P IPS | Asus 24" IPS (portrait mode)
Case Lian Li PC-011D XL | Custom cables by Cablemodz
Audio Device(s) FiiO K7 | Sennheiser HD650 + Beyerdynamic FOX Mic
Power Supply Seasonic Prime Ultra Platinum 850
Mouse Razer Viper v2 Pro
Keyboard Razer Huntsman Tournament Edition
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.
 

darkhmz

New Member
Joined
Aug 9, 2013
Messages
81 (0.02/day)
Location
Hungary
Processor Core i5-2500
Motherboard Gigabyte GA-H61M-D2-B3
Memory 2x4GB Kingston DDR3@1333
Video Card(s) Powercolor HD 7870 Myst Edition
Display(s) DELL U2412M
Power Supply FSP Raider 550W
Software W7 x64
Try this:

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

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,044 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
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
 
Joined
Feb 18, 2010
Messages
1,850 (0.36/day)
System Name Eldritch
Processor AMD Ryzen 5 5800X3D
Motherboard ASUS TUF X570 Pro Wifi
Cooling Satan's butthole after going to Taco Bell
Memory 64 GB G.Skill TridentZ
Video Card(s) Vega 56
Storage 6*8TB Western Digital Blues in RAID 6, 2*512 GB Samsung 960 Pros
Display(s) Acer CB281HK
Case Phanteks Enthoo Pro PH-ES614P_BK
Audio Device(s) ASUS Xonar DX
Power Supply EVGA Supernova 750 G2
Mouse Razer Viper 8K
Software Debian Bullseye
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

ASIC Engineer
Joined
Aug 31, 2010
Messages
12,945 (2.60/day)
Location
Loveland, CO
System Name Ryzen Reflection
Processor AMD Ryzen 9 5900x
Motherboard Gigabyte X570S Aorus Master
Cooling 2x EK PE360 | TechN AM4 AMD Block Black | EK Quantum Vector Trinity GPU Nickel + Plexi
Memory Teamgroup T-Force Xtreem 2x16GB B-Die 3600 @ 14-14-14-28-42-288-2T 1.45v
Video Card(s) Zotac AMP HoloBlack RTX 3080Ti 12G | 950mV 1950Mhz
Storage WD SN850 500GB (OS) | Samsung 980 Pro 1TB (Games_1) | Samsung 970 Evo 1TB (Games_2)
Display(s) Asus XG27AQM 240Hz G-Sync Fast-IPS | Gigabyte M27Q-P 165Hz 1440P IPS | Asus 24" IPS (portrait mode)
Case Lian Li PC-011D XL | Custom cables by Cablemodz
Audio Device(s) FiiO K7 | Sennheiser HD650 + Beyerdynamic FOX Mic
Power Supply Seasonic Prime Ultra Platinum 850
Mouse Razer Viper v2 Pro
Keyboard Razer Huntsman Tournament Edition
Software Windows 11 Pro 64-Bit
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.
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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

New Member
Joined
Nov 4, 2013
Messages
18 (0.00/day)
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;



}
 

ValerieCasady

New Member
Joined
Nov 18, 2013
Messages
3 (0.00/day)
You must declare the variables R1, R2 and R3 as float because you are performing a division operation.
 
Joined
Mar 17, 2011
Messages
103 (0.02/day)
Location
Indiana
Processor Intel i7-3770k
Motherboard ASRock Z77 Extreme 4
Cooling Hyper 212 Evo
Memory Corsair Vengeance 8GB DDR3
Video Card(s) Gigabyte GTX 770
Storage Western Digital Caviar Blue 320GB
Display(s) Acer G264HL
Case Cooler Master CM 690 III
Power Supply Corsair CX600M
Mouse Logitech G400
Software Linux for computing, Windows 7 for gaming
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
 

de.das.dude

Pro Indian Modder
Joined
Jun 13, 2010
Messages
8,783 (1.73/day)
Location
Stuck in a PC. halp.
System Name Monke | Work Thinkpad| Old Monke
Processor Ryzen 5600X | Ryzen 5500U | FX8320
Motherboard ASRock B550 Extreme4 | ? | Asrock 990FX Extreme 4
Cooling 240mm Rad | Not needed | hyper 212 EVO
Memory 2x16GB DDR4 3600 Corsair RGB | 16 GB DDR4 3600 | 16GB DDR3 1600
Video Card(s) Sapphire Pulse RX6700XT 12GB | Vega 8 | Sapphire Pulse RX580 8GB
Storage Samsung 980 nvme (Primary) | some samsung SSD
Display(s) Dell 2723DS | Some 14" 1080p 98%sRGB IPS | Dell 2240L
Case Ant Esports Tempered case | Thinkpad | Antec
Audio Device(s) Logitech Z333 | Jabra corpo stuff
Power Supply Corsair RM750e | not needed | Corsair GS 600
Mouse Logitech G400 | nipple
Keyboard Logitech G213 | stock kb is awesome | Logitech K230
VR HMD ;_;
Software Windows 10 Professional x3
Benchmark Scores There are no marks on my bench
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

Pro Indian Modder
Joined
Jun 13, 2010
Messages
8,783 (1.73/day)
Location
Stuck in a PC. halp.
System Name Monke | Work Thinkpad| Old Monke
Processor Ryzen 5600X | Ryzen 5500U | FX8320
Motherboard ASRock B550 Extreme4 | ? | Asrock 990FX Extreme 4
Cooling 240mm Rad | Not needed | hyper 212 EVO
Memory 2x16GB DDR4 3600 Corsair RGB | 16 GB DDR4 3600 | 16GB DDR3 1600
Video Card(s) Sapphire Pulse RX6700XT 12GB | Vega 8 | Sapphire Pulse RX580 8GB
Storage Samsung 980 nvme (Primary) | some samsung SSD
Display(s) Dell 2723DS | Some 14" 1080p 98%sRGB IPS | Dell 2240L
Case Ant Esports Tempered case | Thinkpad | Antec
Audio Device(s) Logitech Z333 | Jabra corpo stuff
Power Supply Corsair RM750e | not needed | Corsair GS 600
Mouse Logitech G400 | nipple
Keyboard Logitech G213 | stock kb is awesome | Logitech K230
VR HMD ;_;
Software Windows 10 Professional x3
Benchmark Scores There are no marks on my bench
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

ASIC Engineer
Joined
Aug 31, 2010
Messages
12,945 (2.60/day)
Location
Loveland, CO
System Name Ryzen Reflection
Processor AMD Ryzen 9 5900x
Motherboard Gigabyte X570S Aorus Master
Cooling 2x EK PE360 | TechN AM4 AMD Block Black | EK Quantum Vector Trinity GPU Nickel + Plexi
Memory Teamgroup T-Force Xtreem 2x16GB B-Die 3600 @ 14-14-14-28-42-288-2T 1.45v
Video Card(s) Zotac AMP HoloBlack RTX 3080Ti 12G | 950mV 1950Mhz
Storage WD SN850 500GB (OS) | Samsung 980 Pro 1TB (Games_1) | Samsung 970 Evo 1TB (Games_2)
Display(s) Asus XG27AQM 240Hz G-Sync Fast-IPS | Gigabyte M27Q-P 165Hz 1440P IPS | Asus 24" IPS (portrait mode)
Case Lian Li PC-011D XL | Custom cables by Cablemodz
Audio Device(s) FiiO K7 | Sennheiser HD650 + Beyerdynamic FOX Mic
Power Supply Seasonic Prime Ultra Platinum 850
Mouse Razer Viper v2 Pro
Keyboard Razer Huntsman Tournament Edition
Software Windows 11 Pro 64-Bit
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.
 
Top