C programming Do – While loop problems

Click For Summary

Discussion Overview

The discussion revolves around a C programming issue related to the implementation of a Do-While loop for calculating equivalent resistances in series and parallel configurations based on user input. Participants are addressing specific coding errors and offering suggestions for improvement.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents a code snippet aimed at calculating total resistance but encounters issues with incorrect output values for series and parallel resistances.
  • Another participant suggests printing the values of variables inside the loop to debug the execution and highlights that using floating point numbers for loop counters is not advisable.
  • A third participant reiterates the need to print variable values for debugging and emphasizes that the loop executes the correct number of times based on user input, but the addition of resistance values is not functioning as expected.
  • A later reply identifies a specific error in the printf statement, noting a misplaced bracket that leads to incorrect argument passing, which affects the output values. They suggest correcting the printf syntax to include the necessary arguments.
  • There is a consensus that using integers for loop counters is important, as the number of iterations must be a whole number.

Areas of Agreement / Disagreement

Participants generally agree on the need for debugging the code and the importance of using integers for loop counters. However, there is no consensus on the best approach to handle floating point values for resistances, as one participant defends the use of floating points for accuracy.

Contextual Notes

There are unresolved issues regarding the handling of floating point arithmetic in the context of loop counters and the implications of using printf incorrectly. The discussion does not resolve whether the proposed solutions will fully address the initial problem.

Who May Find This Useful

Individuals interested in C programming, particularly those learning about loops and debugging techniques, may find this discussion beneficial.

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 arguments 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.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
Replies
14
Views
4K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 20 ·
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 18 ·
Replies
18
Views
9K
Replies
47
Views
5K