Calculate Equivalent Resistance of Resistors in C++

  • Context: Engineering 
  • Thread starter Thread starter Smiler7
  • Start date Start date
  • Tags Tags
    C++ Circuit Program
Click For Summary

Discussion Overview

The discussion revolves around writing a C++ program to calculate the equivalent resistance of resistors entered by the user. Participants explore the requirements of the program, including how to handle user input, store values in an array, and compute the equivalent resistance, while also addressing ambiguities in the problem statement regarding the configuration of the resistors (series or parallel).

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about the program requirements, particularly the use of a while loop to continuously enter resistor values until a zero is entered.
  • Another participant clarifies that entering zero signals the end of input and that only positive resistances should be stored in the array.
  • A different participant points out the ambiguity in the problem regarding whether the resistors are in series or parallel, which affects the calculation of equivalent resistance.
  • One participant shares their current implementation but notes that the total resistance calculated is always zero and seeks advice on how to store values greater than zero in an array.
  • Another participant suggests improving code readability and questions the use of certain input/output functions, while also highlighting potential issues with the logic in the loop condition.
  • There is a suggestion to use debugging techniques to check intermediate results and a discussion about the challenges of storing resistances in an array without knowing the number of inputs in advance.

Areas of Agreement / Disagreement

Participants generally agree on the need to clarify the program requirements and the handling of user input. However, there is no consensus on how to proceed with the implementation, particularly regarding the ambiguity of resistor configuration and the method of storing values.

Contextual Notes

Participants express uncertainty about the expected programming constructs (e.g., STL, dynamic memory allocation) for storing resistances, which may affect their implementation strategies.

Smiler7
Messages
2
Reaction score
0
Write a program that prompts the user to enter the resistance of each resistor continuously by using a while loop until a zero resistance is entered and store it in an array, then calculates the equivalent resistance R. Also count the number of resistors n. Test your program with R1 = 1 kΩ,R2 = 2 kΩ.,R3 = 4 kΩ and R4 = 8 kΩ . What is the equivalent resistance? Use another separate loop to print the resistances and the equivalent resistance.

I'm trying to do this question above but i don't actually get what its asking for?!
----------------------------------------------------------------------------------------
"Write a program that prompts the user to enter the resistance of each resistor continuously by using a while loop until a zero resistance is entered and store it in an array, then calculates the equivalent resistance R..."

/* Starting with this bit i could write a program where i enter the value of each resistor. But "continuously by using a while loop until a zero resistance is entered and store it in an array"?! */
 
Physics news on Phys.org
It means you're supposed to enter 0 to signal that you've entered all the resistances. Something like this:

Enter your resistances one by one; enter a 0 when you're finished:
Resistance #1: 1
Resistance #2: 2
Resistance #3: 4
Resistance #4: 8
Resistance #5: 0

The result is...

Of course, you don't actually store "resistance #5" in the array or use it in the calculation.
 
Read the value.

If it is not zero - add it to the end of the array, continue reading.

If it is zero - don't add to the array, stop reading, proceed to the next stage.

IMHO the way it is worded now it is ambiguous, as you are not told if the resistors are connected in series, or in parallel.
 
Thanks guys.

but now I'm into it I am stuck...

So far i have created a program that asks straight away the value of resistance.

Say i type in 10 ohms it loops until i press 0 ohms. When i press 0 ohms it prints out the total. But the total always comes out to be zero.?!

My other problem... How do i save all my values > than zero into an array?!

I think we will try and tackle these problems first. Am i going the right way?

#include <stdio.h>

main()

{

double RTP,X;

/* The loop statement */

RTP=0;

do
{
printf ("Enter the value of Resistance in Ohms >");
scanf("%lf",&X);
RTP += (1/X);

if (X < 0)
{
printf ("Resistance can't be negative >\n");


}

else if (X == 0)

{
printf ("Zero Resistance entered. >\n");
printf ("\n The Total resistance is %lf Ohms.",(1/RTP));


}

}while (X > 0,X);

}
 
To make your programs more readable, use [noparse]
Code:
[/noparse] tags, for example

[noparse]
Code:
int main()
{
   return 0;
}
[/noparse]

displays as

Code:
int main()
{
   return 0;
}

Are you sure you should be using printf and scanf, and not cin/cout for input/output?

Code:
while (X > 0,X)

works as expected only by accident - was it really your plan to use a comma operator?

When it comes to the result - it is not that unexpected. Simplest way of checking what is happening is to either run it under debugger, or to add a line

Code:
printf("%lf, %lf\n",X,RTP);

just before if (X < 0) check. This way you will see intermediate results, they should be helpful.

Storing the information in array is a different animal, and it can be a little bit tricky if you are not told how many elements to expect. Are you expected to use STL and vector, or Microsoft's CArray, or should you use a dynamically allocated memory and implement everything by yourself? The latter seems unlikely.
 

Similar threads

Replies
5
Views
4K
Replies
12
Views
2K
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
13K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K