Thursday, June 7, 2012

Assignment 4 - Walk Program

Can someone help me understand what the purpose of these steps are?  It seem like the code computes a number as an integer while also multiplying it by 10.5 and then converts it back to a float and divides it by 10.0.

My questions are:
1. why convert it to an int at all?
2. why multiply by 10.5? if you we just divide by 10.0?  won't that throw off the math?
3. why calculate 10.5 as "10.0 + 0.5"?  why not just "10.5"?

// Compute miles for each distance on the map
miles = float(int(distance1 * scale * 10.0 + 0.5)) / 10.0;
outData << "For a measurement of " << distance1
<< " the first distance is " << miles
<< " mile(s) long." << endl;
totMiles = totMiles + miles;


ANSWER TO MY OWN QUESTION:
That's a way to round the number to the nearest 10th.

Try some examples:
let x = distance*scale
if x = 12.345
then
10x+.5 = 123.95
int( 123.95 ) = 123
123/10 = 12.3

thus the number is correctly rounded to the nearest 10th.

Tuesday, June 5, 2012

Did you know...

If you purchased a used book (like I did) and it came without the CD you can download the contents 

By the way, you can also preview one chapter for Pearson titles while you wait for your book.  I found the first chapter under the instructor resources. If you're waiting for your financial aid but need access to online homework like MyLab they will allow you to use it for the first 17 days of class.  Just register and poke around the site for a bit for the links to your class materials. Customer service responds quickly. 
 Pearson Higher Ed link

... and nope I don't work for them I just had to get creative while I waited for the book :D


Monday, June 4, 2012

Trial and error

I completed example 4-11 but the file kept giving me incorrect output.  Figured out that if the user enters Y or N as capital letters the program runs fine however if caps are not used the program prints the wrong message.  Looks like a good example of how to predict user errors and a good place to use || (or).