C programming Do – While loop problems

In summary, the program calculates the equivalent resistance of a number of resistors entered at the keyboard. The user enters the number of resistors and the program assigns the equivalent resistance for each resistor. The program also prints the total resistance for the number of resistors entered.
  • #1
JJ452
3
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
Thanks for your advice and help, it was great.
 

Related to C programming Do – While loop problems

1. What is a do-while loop in C programming?

A do-while loop in C programming is a control flow statement that executes a block of code repeatedly until a certain condition is met. It differs from a regular while loop in that the code block is executed at least once before the condition is checked.

2. How do I use a do-while loop in my C program?

To use a do-while loop, you need to first initialize the loop counter and then specify the code block to be executed. After the code block, you need to provide the condition that will be checked at the end of each iteration. If the condition is true, the loop will continue; if it is false, the loop will terminate.

3. What is the difference between a do-while loop and a while loop?

The main difference is that a do-while loop will always execute the code block at least once, while a while loop will only execute the code block if the condition is initially true. Another difference is that the condition in a do-while loop is checked at the end of each iteration, while in a while loop it is checked at the beginning.

4. How do I exit a do-while loop in C programming?

To exit a do-while loop, you can use the break statement inside the code block. This will terminate the loop and resume execution after the loop. Alternatively, you can use the continue statement to skip the rest of the code block and start the next iteration of the loop.

5. Can I nest do-while loops in C programming?

Yes, you can nest do-while loops in C programming. This means that you can have a do-while loop inside another do-while loop. However, it is important to make sure that each loop has its own unique condition and that the nested loops do not cause an infinite loop.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
4
Views
857
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
6
Views
6K
Back
Top