C programming Do – While loop problems

AI Thread Summary
The discussion revolves around troubleshooting a C program designed to calculate equivalent resistances in series and parallel for a user-defined number of resistors. The main issues identified include incorrect accumulation of resistance values due to a misplaced closing bracket in the printf statement, which separates the output into three distinct statements instead of formatting them correctly. This leads to RTS displaying the last entered resistance value and RTP not calculating as intended. Additionally, it's emphasized that using floating-point numbers for loop counters is inappropriate; integer counters should be used since the number of resistors must be a whole number. Suggestions include printing values within the loop for debugging and ensuring that compiler warnings are enabled to catch such errors. Overall, the conversation highlights common pitfalls in programming related to syntax and data types.
JJ452
Messages
3
Reaction score
0
Hi,
I have to build a program that will find the equivalent resistances in series and parallel of any number of resistances entered at the keyboard. So far i have this:

/*

TITLE- q4__4.c 2/10/06
DESCRIPTION- Calculates the effective resistance of a entered number of resistors in series and in parallel
*/

/* Include standard input ouput library */

#include <stdio.h>


main()

{
/* local variables go here */
double RTS,RTP,LOOPS,X,loop_count;

/* The Number of Resistors */

printf("How many resistors would you like to calculate a value for?");
scanf("%lf",&LOOPS);

/* The loop statement */

loop_count=0;
RTS=0;
RTP=0;

do
{
printf ("Enter a value for resistance >");
scanf("%lf",&X);
RTS += X;
RTP += (1/X);

loop_count = (loop_count + 1);
}while (loop_count < LOOPS);

/* The Result */
printf ("\nThe Total resistance is %lf if they are in series and %lf they are in parallel."),RTS,(1/RTP);

I really don't know where i went wrong. The value returned for RTS is the same value as the user entered and the value returned for RTP is the last value for the resistor entered. I reckon it's a problem with the loop but what it is i don't know. Help please?
 
Technology news on Phys.org
How many times does the loop execute?
To test it print the value of x,rts, rtp and loop count inside the loop.

ps. It's a bad idea to use floating point for loop counters, use integers.
 
mgb_phys said:
How many times does the loop execute?
To test it print the value of x,rts, rtp and loop count inside the loop.

ps. It's a bad idea to use floating point for loop counters, use integers.

It executes as many times as the user entered (The number of resistors they have) because the program compiles and asks for that number of resistances but, for some reason, doesn't seem to add the 'X' value (the resistance entered) correctly. I can't use integers as it will have decimals which could be small and a value of 1 or 0 wouldn't be accurate enough.
 
Had to actualy test this - it's a very subtle error.
The line printf ("blah"),RTS,(1/RTP);

Is wrong - note the misplaced closing bracket for printf
what the compiler see is printf("blah blah"); RTS; 1/RTP; As three separate statements.

Printf is missing the arguements for the %f calls, so it just uses whatever is on the stack next - which happens to be the last values entered in scanf ( because they use the same temporary storage). the extra RTS and 1/RTP statements are just ignored.

It should be printf ("blah",RTS,(1/RTP));

If you compiled with warnings on ( eg /Wall in gcc) it shoudl tell you - MS c compiler didn't tell me!

ps. I mean use integers for counters in the loop, the number of times around the loop MUST be an integer - you can't enter 0.1 resistor values.
 
Thanks for your advice and help, it was great.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...
Back
Top