GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-May-2012, 19:37
choco choco is offline
New Member
 
Join Date: Apr 2012
Posts: 11
choco is on a distinguished road

Result of this calculation is zero


Hey guys I was writting a program when I came up with a problem I couldn't explain.

Here is my questionable part of code, i include only whats needed.
CPP / C++ / C Code:
 
int maxhr; /*definition line */ 
float minthr,maxthr; /* continues */ 
/* ........ */

minthr=maxhr*50/100;  
maxthr=maxhr*0.85;
printf("\nHeart rate range, from %.1f to %.1f" , minthr,maxthr);

Before writing the code as seen (working now)
What i wrote was minthr=maxhr*(50/100);
maxthr=maxhr*(85/100);

Printf results were zeroes.

Why was there such a problem using parenthesis?
  #2  
Old 07-May-2012, 23:20
Howard_L Howard_L is offline
Senior Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 1,004
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Another newbs question.


Good question.

First, the arithmetic is done from left to right.
So if maxhr was 90, in the statement would first do 90*50 and then 4500/100.
The answer to that would be 45.

If you were to use the parentheses as you show you would first do the (50/100).
In C that would return an int - a whole number with no fraction.
ie: the number of times 100 goes into 50, which is zero.
And of course maxhr*0 will always return 0.

To get that to return a fraction you can simply introduce an decimal point like this:
CPP / C++ / C Code:
printf("%d \n", (50/100) );
printf("%f \n", (50.0/100) );
/* output:
0 
0.500000 
*/
This is called 'casting' from one type to another. (from type int to type double)
In C this is done automatically according to what type is requested as the statement is processed as determined by order of precedence.

You can also specify a type in your code. (casting).
CPP / C++ / C Code:
printf("%d \n", (int)123.4567 ); /* cast from constant double to int */
So you want to review operators '/' and '%' and "operator precedence" and "data casting" in your book.

It's a bit confusing at first but you'll get used to it.
  #3  
Old 08-May-2012, 03:44
choco choco is offline
New Member
 
Join Date: Apr 2012
Posts: 11
choco is on a distinguished road

Re: Result of this calculation is zero


Oh, I see. Thank you!
 
 

Recent GIDBlogMatch IP in CIDR by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help!!! A Problem with Camera Resolution Melvin Lin C++ Forum 8 18-Jun-2004 21:36
A.I. Question calculus87 Open Discussion Forum 1 03-May-2004 12:48
question of practice magiccreative C++ Forum 1 06-Feb-2004 07:17
a C input question tmike C Programming Language 1 16-Sep-2003 02:31
Motherboard and CPU Question pcxgamer Computer Hardware Forum 2 29-Oct-2002 06:15

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:06.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.