The program should calculate total resistance of any two resistors
in parallel (R1 and R2) whose values should be any possible combination
of integer numbers between 1 and 100. The program should analyze
the output (RT), and display R1, R2, and RT on the screen if the
equivalent resistance RT is just a whole number (without decimals).
-----------------------------------------X------------------------------------------
Formula to calculate total resistance (RT) of two parallel resistors is:
Well, you need to cycle thru the values of r1 from 1 to 100, then inside that you have to cycle r2 likewise.
Is there some C statement that dos something like this? Could you nest two of them to do both R1 and R2?
Then you need to calculate the answer. Is there some C statement that does this?
Then you have to check if the answer is a whole number. Is there some C function that returns the whole part of a number?
then you need to see if the value is equal to the function result. Is there some C operation that checks if two numbers are equal?
Then you need to print out the numember if the conditio is true. IS there some C startement that does something if a condition is truw?
Just take these steps one at a time and just might make it.
I will write the algorithm for you.
You will have to convert that to C code.
1. R1 = 1
2. Repeat steps 3 to 12.
3. R2 = 1
4. Repeat steps 4 to 9.
5. RT = 1 / ((1/R1)+(1/R2))
6. Display R1, R2, RT.
7. Increment R2.
8. If (R2 = 101) goto step 10.
9. End of Loop1.
10. Increment R1.
11. If (R1 = 101) goto step 13.
12. End of Loop2.
13. End of program.
Well you did the factoring ;-)
Loops are good,
- you should declare Rt as well.
- display values of R1,R2 as well
- display only if there are no decimals as specified in your first message.
to do that, you have a function to get the integer part....
tarababu any progress ?
tip : you declated int R1,R2;
If R1==5 and R2==2 do you think you would get R1/R2 == 2.5 ? Answer no ...
Change your types :)
or use the cast.
(float)R1 will make R1 value a float...
Hi MOKULE! I READ YOUR PROFILE. I LEARNT FROM YOUR PROFILE THAT YOU ARE ELECTRONICS ENGINEER. NICE MEATING YOU! is there any sub-group in Experts Exchange for Q&A discussions on Electronics related questions? pls let me know(i too is in electronics related field)
thanks .
Rt=(float) (R1*R2)/(R1+R2);
//since R1 and R2 are int,the resultant is also int,so you need to cast the result of
//the division to a float to get the correct decimal values.
if(Rt-(int)Rt==0)
{
printf("R1:%d,R2:%d,Total resistance is: %.2f\n",R1,R2,Rt);
}
//Rt is a float,so casting it as an int will truncate the decimal part.
//If the difference of the float value and the int value is 0,then Rt is a whole number
Also,if you need to calculate the resistances for fractional values also,you can consider changing the datatypes of R1 and R2 to float as mercantilum has already pointed out.
Hi Marek!
it looks that the asker is not in hurry :~)
by-the-way i'm having questions regarding RS232/485 communications. where can i post those questions??
-vasu
hi Mercantilum!
the link u posted is very useful. thanks
-vasu
to Kdo
It turned out not so incredibly easy program.
to tarabubu
My advice is: avoid any divisions. Do necessary checking only on integer numbers.
0
tarababuAuthor Commented:
this what i have so far, could someone tell me if it is right. the program seems to be working but just need a second opinion. any suggestions are welcome
Hi mokule, stay cool man, no fire around :)
Right, I didn't test the code - after a re-lecture I think
if(Rt-(float)((int)Rt)==0)
is more correct ... not tested either :)
Good to know that your problem is solved:-)
So, why don't you close this question?
Award points to the expert whom you think gave good solution.
If you think more than one person helped you in finding the solution, then you can split the points among different experts and then close the question.
Just in case you're interested... the code w/o errors introduced by floating point division..
#include<stdio.h>
main() {
int R1, R2, Rt;
for(R1=1;R1<=100;R1++)
for(R2=1;R2<=100;R2++)
if (!((Rt = (R1*R2))%(R1+R2)))
printf("R1=%d R2=%d Total resistance is: %d\n",R1,R2,Rt/=(R1+R2));
}
Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.
Sure.
This is an incredibly easy program. Since it is obviously homework we're very limited in what we can do for you. Do you have any specific questions?
Kent