• C

# C program- :)

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:

RT= 1 / ((1/R1)+(1/R2))

###### Who is Participating?

Commented:
Ok.Try these:

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

Data Warehouse Architect / DBACommented:

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
0

Commented:
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.

0

Commented:
Hi grg99

> Then you need to calculate the answer.  Is there some C statement that does this?

I think this is a wrong direction, because of calculating errors.
0

Commented:
1. identify the type of data you need to use for variables and calculation (one line of C):
integer ? floating values ?

2. how to name your variables ? your formula is a good tip :)

3. how can you get the input from the user (2 lines of C, or 4 lines, 2 per input variable ) ?

4. how can you simply calculate  your formula in one line ?

5. how can you display the result (one line)?
0

Commented:
int er=(R1*R2)/(R1+R2)
print value of er.

Put these 2 statements inside a nested for loop,which will cycle thru each combination of R1 and R2.

0

Commented:
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.

Hope this helps.....

-ssnkumar
0

Author Commented:
this is what i have so far

#include<stdio.h>
main()
{
int R1, R2;

for(R1=1;R1<101;R1++)
{

for(R2=1;R2<101;R2++)
{
float Rt;
Rt= (R1*R2)/(R1+R2);
printf("Total resistance is: %.2f\n",Rt);
}

}

}
0

Author Commented:
how do i check if  Rt is whole number or float.
i am not sure how to output the values R1, R2 , Rt just when Rt is a whole number.
0

Commented:
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....
0

Commented:
Have a look at http://www.gnu.org/software/libc/manual/html_node/Rounding-Functions.html
Example of algorithm : if (a - int(a)) == 0 ...
0

Commented:
If the mantissa part is 0, then RT is a whole number, otherwise it is float.

-ssnkumar
0

Commented:
0

Commented:
Hi tarababu!
i can suggest this code, but there are few point that are not clear. i'll ask those after posting the code

// program starts here
main()
{
int r1, r2;
float rt;

printf("\n Enter R1:");
scanf("%d", &r1);
printf("\n Enter R2:");
scanf("%d", &r2);

rt=(float)(r1*r2)/(r1+r2);

printf("r1= %d    r2=%d     ", r1,r2);
printf("rt= %f  %d", rt);

return 0;
}
// End of program

Q) why do you need the RT value in Integer/whole number? in most of the cases the RT will be a float value (say,R1=2, R2=3 then RT=1.2 )

-vasu
0

Commented:
I answer for him because he may be don't know it yet.
This is homework.
It should teach him doing calculations on float and integers.
0

Commented:
Hi experts

Is my computer out of order?

int R1;
for( R1 = 1 ; R1 <= 100 ; R1++)
printf("%g\n",R1 - 1 / (1/(double)R1));
0

Commented:
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 ...
or use the cast.
(float)R1 will make R1 value a float...
0

Commented:
And I say don't change Your types.
0

Commented:
HI tarababu!
have u tried any further??

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 .

-vasu
0

Commented:
Hi vasureddym
Nice to meet Yot too.
Look at Hardware | Microchips, but it is rather strongly related to PC only.

Regards
Marek
0

Commented:
and my apologies to all experts for sounding repetitive of previous comments.

I realized that after i had posted my comment.
0

Commented:
Moreover You repeat the same error
0

Commented:
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
0

Commented:
Maybe you can post it in Hardware/Modems
See http://www.experts-exchange.com/allTopics.jsp
0

Commented:
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

Author 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

#include<stdio.h>

main()

float  R1, R2, Rt;

for(R1=1;R1<=100;R1++)
{
for(R2=1;R2<=100;R2++)
{
Rt= (R1*R2)/(R1+R2);

if(Rt-(int)Rt==0)
{
printf("R1=%0.2f      2=%0.2f   Total resistance is: %.2f\n",R1,R2,Rt);
}
}
}
}
0

Commented:
Ok you followed the (x - int(x)) advice, good.
I think it is ok, but ... didn't you have to use R1,R2 as integers?
0

Commented:
Hi tarababu and Mercantilum

Both of You are wrong

THIS CODE IS ERRONEOUS !!!. EVEN IF IT SEEMS WORKING. IT'S ONLY BY ACCIDENT.

Execute the code below and look at the results carefully .

main()
{
int R1
for( R1 = 1 ; R1 <= 100 ; R1++)
printf("R1=%d  %g\n",R1, R1 - 1. / (1./(double)R1));
}

or such code

main()
{
int R1
for( R1 = 1 ; R1 <= 100 ; R1++)
if( R1==(int)(1. / (1./(double)R1)))
printf("R1\n",R1);
}
0

Commented:
Last piece of code should be of course like this

main()
{
int R1
for( R1 = 1 ; R1 <= 100 ; R1++)
if( R1==(int)(1. / (1./(double)R1)))
printf("R1=%d\n",R1);
}
0

Commented:
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 :)
0

Commented:
Hi
It's hard to be cool but I'll try.

No, it is not correct either.

Did You test my example code? I think no. Please do it. Discussion will be easier.
When You do it let me know.

Regards
cool man Marek
0

Author Commented:
:) thanks to all you guys for helping me with the program.
0

Commented:
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.

-ssnkumar
0

Commented:
tarababu,

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));
}

b.
0

Commented:

At last somebody heard my cry. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.