Solved

# C program- :)

Posted on 2004-04-13
714 Views
Last Modified: 2008-03-03
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))

could someone please guide me.

0
Question by:tarababu
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 12
• 7
• 4
• +6
35 Comments

LVL 45

Expert Comment

ID: 10817111

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

LVL 22

Expert Comment

ID: 10817575
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

LVL 17

Expert Comment

ID: 10818541
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

LVL 10

Expert Comment

ID: 10819871
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

LVL 9

Expert Comment

ID: 10819873
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

LVL 8

Expert Comment

ID: 10820025
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 Comment

ID: 10820906
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 Comment

ID: 10820928
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

LVL 10

Expert Comment

ID: 10820932
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

LVL 10

Expert Comment

ID: 10820939
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

LVL 8

Expert Comment

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

-ssnkumar
0

LVL 17

Expert Comment

ID: 10820988
Your code is erroneous.
0

LVL 2

Expert Comment

ID: 10821072
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

LVL 17

Expert Comment

ID: 10821173
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

LVL 17

Expert Comment

ID: 10821389
Hi experts

Is my computer out of order?
What about Yours

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

LVL 10

Expert Comment

ID: 10821457
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...
0

LVL 17

Expert Comment

ID: 10821496
And I say don't change Your types.
Change Your formula.
0

LVL 2

Expert Comment

ID: 10822039
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

LVL 17

Expert Comment

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

Regards
Marek
0

LVL 9

Accepted Solution

ankuratvb earned 500 total points
ID: 10822350
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

LVL 9

Expert Comment

ID: 10822378
Please ignore my last comment.
and my apologies to all experts for sounding repetitive of previous comments.

I realized that after i had posted my comment.
0

LVL 17

Expert Comment

ID: 10822444
Moreover You repeat the same error
0

LVL 2

Expert Comment

ID: 10823098
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

LVL 10

Expert Comment

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

LVL 17

Expert Comment

ID: 10823808
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 Comment

ID: 10829283
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

LVL 10

Expert Comment

ID: 10829292
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

LVL 17

Expert Comment

ID: 10830888
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

LVL 17

Expert Comment

ID: 10830924
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

LVL 10

Expert Comment

ID: 10831745
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

LVL 17

Expert Comment

ID: 10831912
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 Comment

ID: 10851969
:) thanks to all you guys for helping me with the program.
0

LVL 8

Expert Comment

ID: 10856392
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

LVL 4

Expert Comment

ID: 10887183
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

LVL 17

Expert Comment

ID: 10887264

At last somebody heard my cry. :)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infâ€¦
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
###### Suggested Courses
Course of the Month5 days, 9 hours left to enroll

#### 734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.