Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 02-07-2004, 12:35 PM   #1 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
another simple C question

Ok this program is the same as the last one, i am to enter 3 variables height width and length and it spits out the volume, the square footage and the ratio between the length and the width, it does everything except the ratio it rounds to the nearest hole number, even though it is stored as a double.

Thanks in advance.


Quote:


#include <stdio.h>

main(void)
{
int nRoomLength=0, nRoomWidth=0, nRoomHeight=0;

double dWidthToLength=0 ; /* width/length */

short int nRoomVolume=0 ; /* nRoomWidth * nRoomLength * nRoomHeight*/

unsigned int nRoomArea=0 ; /* nRoomWidth * nRoomLength */

printf("Please enter a value for the room width:");
scanf("%d", &nRoomLength);
printf("Please enter a value for the room length::");
scanf("%d", &nRoomWidth);
printf("Please enter a value for the room height::");
scanf("%d", &nRoomHeight);


nRoomArea = nRoomLength * nRoomWidth;
nRoomVolume = nRoomLength * nRoomWidth * nRoomHeight;
dWidthToLength = (nRoomWidth / nRoomLength);
printf("The area of the floor of the room is: %d\n", nRoomArea);

printf("The volume of the room is: %d\n", nRoomVolume);

printf("The ratio of the width to the length is: %lf\n\n", dWidthToLength);


printf("Variable Dump: \n\n");

printf("Variable\tNumber of\tAddress of\tValue in\n");
printf("Name\t\tBytes\t\tVariable\tVariable\n");

printf("nRoomWidth\t%d\t\t%p\t%d\n",sizeof(nRoomWidth),&nRoomWidth,nRoomWidth);
printf("nRoomWidth\t%d\t\t%p\t%d\n",sizeof(nRoomLength),&nRoomLength,nRoomLength);
printf("nRoomWidth\t%d\t\t%p\t%d\n",sizeof(nRoomHeight),&nRoomHeight,nRoomHeight);
printf("nRoomWidth\t%d\t\t%p\t%lf\n",sizeof(dWidthToLength),&dWidthToLength,dWidthToLength);
printf("nRoomWidth\t%d\t\t%p\t%d\n",sizeof(nRoomVolume),&nRoomVolume,nRoomVolume);
printf("nRoomWidth\t%d\t\t%p\t%d\n",sizeof(nRoomArea),&nRoomArea,nRoomArea);



}
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
Old 02-07-2004, 03:11 PM   #2 (permalink)
paranoid
 
Silvy's Avatar
 
Location: The Netherlands
I'm no C - programmer but maybe the operands for the division need to be doubles for a result to return a double?

dValueResult = dValueWidth / dValueLength
(or perhaps only one of the two)
__________________
"Do not kill. Do not rape. Do not steal. These are principles which every man of every faith can embrace. "
- Murphy MacManus (Boondock Saints)
Silvy is offline  
Old 02-07-2004, 03:33 PM   #3 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
Your not really describing what your problem is.

I am going to let you in on a little secret, real programmers don't bother to save every teeny tiny bit here and there.

If you designing an object that might have millions of instantiation... well then I might understand if you used shorts when you don't need the capacity.

Mixing data types is generally a pain in C/Java like languages because there are strict rules that govern how the compiler will do things. You end up sending explicit casting instructions (which waste time) and don't end up with the hugest performance or space gains. And casting makes your code ugly... really ugly.

In an object with 10 members if you make one of them a short, you only save a few bytes. Even with millions of them your only saving some constant multiple of space. It might sound important but in the big scheme of things these aren't the make or break performance determinants. The exception being realtime/interactive applications.

In this case I would say just use floats throughout. If this were Java I would say doubles.
__________________
"It better be funny"
kel is offline  
Old 02-07-2004, 03:51 PM   #4 (permalink)
Addict
 
Location: Ottawa, ON, Canada
Quote:
Originally posted by Silvy
I'm no C - programmer but maybe the operands for the division need to be doubles for a result to return a double?

dValueResult = dValueWidth / dValueLength
(or perhaps only one of the two)
Yes, that's exactly it. It's a common mistake made when doing division in C (one that I've made myself many times )

Let's say you have something like:

Code:
int    nDenominator  = 2;
int    nNumerator   = 3;
double dResult;
dResult = nNumerator / nDenominator;
You would expect the value of dResult to be 1.5, except that it's not. Instead, it ends up holding 1.0. Why? Because in order for floating point division to work, either one or both the numerator, and denominator in the equation need to be a double (at least in Win32 it does, I didn't have a chance to verify this with any other compiler).

You can fix this code in either one of two ways.

1) Change the variables nNumerator or nDenominator to be a double
2) Typecast either one or both of the variables to be a double (i.e. (double) nNumerator / (double) nDenominator ).

In either case, you end up getting the answer 1.5, which is exactly what you should be getting.
__________________
"A witty saying proves nothing"
- Voltaire
Quadraton is offline  
Old 02-07-2004, 04:02 PM   #5 (permalink)
Addict
 
Location: Ottawa, ON, Canada
Quote:
Originally posted by kel
Your not really describing what your problem is.

I am going to let you in on a little secret, real programmers don't bother to save every teeny tiny bit here and there.

If you designing an object that might have millions of instantiation... well then I might understand if you used shorts when you don't need the capacity.

Mixing data types is generally a pain in C/Java like languages because there are strict rules that govern how the compiler will do things. You end up sending explicit casting instructions (which waste time) and don't end up with the hugest performance or space gains. And casting makes your code ugly... really ugly.

In an object with 10 members if you make one of them a short, you only save a few bytes. Even with millions of them your only saving some constant multiple of space. It might sound important but in the big scheme of things these aren't the make or break performance determinants. The exception being realtime/interactive applications.

In this case I would say just use floats throughout. If this were Java I would say doubles.
I'm sorry, but huh??

I can see exactly where you're coming from, but you shouldn't cloud a simple issue with big talk like instanstiation and memory management. It's unnecessary, and just adds chaos to confusion.
__________________
"A witty saying proves nothing"
- Voltaire
Quadraton is offline  
Old 02-07-2004, 04:03 PM   #6 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
He is having trouble because he is trying to use smaller data types to save space. I am explaining to him why this isn't always a good idea.
__________________
"It better be funny"
kel is offline  
Old 02-07-2004, 04:22 PM   #7 (permalink)
Addict
 
Location: Ottawa, ON, Canada
Quote:
Originally posted by kel
He is having trouble because he is trying to use smaller data types to save space. I am explaining to him why this isn't always a good idea.
I don't think he's trying to do anything other that write a simple program. I doubt he's very concerned with optimizing his memory usage at this point.

Not to put any words into Dilbert1234567's mouth, or anything.
__________________
"A witty saying proves nothing"
- Voltaire
Quadraton is offline  
Old 02-07-2004, 04:25 PM   #8 (permalink)
kel
WARNING: FLAMMABLE
 
Location: Ask Acetylene
Neither should I :-)
Dilbert why did you mix ints,shorts, doubles, and unsigned ints?
__________________
"It better be funny"
kel is offline  
Old 02-07-2004, 10:18 PM   #9 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
Yeah, I think Dilbert1234567 is confused about the use of data types. However, I disagree with kel as to why he's confused. I simply think that Dilbert1234567 has no understanding of the roles of the different variable types.

I mean, who would use integers to represent area or volume?

Generally speaking, integers are used for counting or indexing, and floats (or doubles) are used for measurements...
KnifeMissile is offline  
Old 02-08-2004, 12:29 AM   #10 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
*sigh* men. ok, long and short of it: an int, divided by an int, will always return an int. you need to use a more precise type of variable to store your data. or cast your ints as doubles before your devison. for the "why" sa well as the how, see above.
cheerios is offline  
Old 02-08-2004, 12:31 AM   #11 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
i have to use the difrent memory sizes for the home work, to see waht and how they function, my problem is that the program is rounding dWidthToLength when it should not be rounding it off.
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
Old 02-08-2004, 02:49 AM   #12 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
dWidthToLength = (nRoomWidth / nRoomLength);

ok. dWidthToLenght is a double. but nRoomWidth and nRoomLenght are INTEGERS. you need to open your book up, and look up Integer division. it's not the same as normal division. if you divide integers, you will get an integer answer, that is equal to the floor of the decimal division of the 2 values. so 2/4 = 2. 2/5 will ALSO =2. ALWAYS. because you're deviding INTEGERS.

to prevent this, you can do 2 things.
1: cast nRoomWidth and nRoomLength as doubles or floats.
2: make nRoomWidth and nRoomLength doubles or floats.

that's it. those are your options.
cheerios is offline  
Old 02-09-2004, 10:51 PM   #13 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
ya i just found that so i taked on *1.0 and that fixes it, int*int * float = float
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
 

Tags
question, simple

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:14 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360