Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Bravo!

I didn't compile and run it myself, but this looks quite good. You don't need to cast the return from the second scale call to double, that's already what you'll get back.

-Lee
 

SkippyThorson

macrumors 68000
Original poster
Jul 22, 2007
1,670
942
Utica, NY
Bravo!

I didn't compile and run it myself, but this looks quite good. You don't need to cast the return from the second scale call to double, that's already what you'll get back.

-Lee

:) In the words of Elvis, thank you very much.

Just after my excitement, I ran it myself, and entering 34.5678989 returns 0.3457. Effectively stumping me again. I do know that I want to kick the decimal back 2 places, so I need a negative 2 to accomplish that. Getting everything to the right place is the hard part.

I really took that "think it through" thing seriously, going not just step by step, but drawing little mental arrows to where everything goes through the code. It really does help.
 

naples98

macrumors member
Sep 9, 2008
95
3
Houston
My thoughts

I've been following this thread to watch your progress. With all of the great hints you have been given, you are getting really close but you need to make sure you understand what each line of your code is doing. Here are a few hints from me.

Code:
	y = scale(x, 2);
//y is what is returned from scale using x and the power of 2

1. What are you doing with 'y' once you set it to the return value of your scale function?

Code:
	rounded_x = (int) (x + .5);

2. Why are you adding .5 to 'x' then setting it equal to rounded_x? Also, is this doing exactly what you want it to do?

Code:
//and now I'm unsure
	x = (double) scale(x, -2);

3. Make sure you understand what this line of code is doing and is it what you want it to do?

UPDATE: Modify my post to make it more readable.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Ah, yeah, missed that. Y is scaled to x * 100. But then you add .5 to something else and store it. You're very close.

-Lee
 

chown33

Moderator
Staff member
Aug 9, 2009
10,758
8,451
A sea of green
Code:
//----------------------------------------
	y = scale(x, 2);
//y is what is returned from scale using x and the power of 2
	rounded_x = (int) (x + .5);
//and now I'm unsure
	x = (double) scale(x, -2);
//----------------------------------------

It's good that you're stepping it through, but I think you need to be more concrete: write it down.

It looks like you're doing the right thing in your head, but when it comes to writing it down, you're making simple avoidable coding mistakes. If you have the steps actually written down (algorithm), transcribing them into code makes mistakes a little more obvious, especially for the kind of mistakes you're making.

For example, in this:
Code:
	y = scale(x, 2);
//y is what is returned from scale using x and the power of 2
The comment isn't telling you anything you don't know by reading the code. Comments shouldn't paraphrase code, they should explain code. Code tells you what happens. Comments tell you why it happens.

An example of a useless comment:
Code:
x++;  // increment x
This is useless because anyone who knows the language well enough to read the source is going to know that "x++" means "increment x". Either the comment shouldn't be there at all, because the code is obvious, or the comment should explain why the relatively simple action of incrementing x needs to be commented upon.

Now, going back to your code... Yes, y is the returned value. So how about giving an example in the comment:
Code:
// Example: if x is 12.347, y is 1234.7
// Example: if x is 0.841, y is 84.1
Now the reader has some specific examples that illustrate exactly what happens.


Next, consider what your next line of code is doing:
Code:
rounded_x = (int) (x + .5);

Do you not see any problem with this? Look carefully at exactly what variables are being used. Read the examples in the comments immediately above this line of code. Notice anything?

And why no comments about what rounded_x holds? Again, comments should either explain or illustrate what the code does. Saying "rounded_x receives the integer value of x plus .5" would merely paraphrase the code. Add a meaningful comment here, and it might show the mistake.

And your third line:
Code:
	x = (double) scale(x, -2);

Do you see any problem with this, which might be similar to a problem in one of the lines above it?
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
Nice progress, Skippy!

We're all dancing a fine line here because we want to tell you what's going on but we also don't want to spoil your own "aha!" moment when you discover it for yourself -- plus it's good practice for learning to "think like a programmer". That's why you're getting all these hints and almost-straight-answers. I hope that we're not driving you too crazy! :)

I agree with the previous posters about comments -- they should help the reader understand WHY the code is doing what it's doing. For example, consider:

Code:
// assign the value to x
float x = 12.37;
// do some math
x = x * 1.08;
// y is x multiplied by 100
int y = x * 100;

And compare that with:

Code:
// current price of a widget (in dollars)
float x = 12.37;
// adjust for 8% state sales tax
x = x * 1.08;
// the price, represented in cents instead of dollars  
int y = x * 100;

And of course using descriptive variable names helps even more:

Code:
// current price of a widget (in dollars)
float widgetPrice = 12.37;
// adjust for 8% state sales tax
widgetPrice = widgetPrice * 1.08;
// the price, represented in cents instead of dollars  
int priceInCents = widgetPrice * 100;

Hope that is helpful!
 

SkippyThorson

macrumors 68000
Original poster
Jul 22, 2007
1,670
942
Utica, NY
Woohoo!!

:) Everyone in this thread has my undying appreciation for helping me through this one. It seems simple, and probably is, but I don't care and I'm damn proud! I would list all the names, but you all definitely know what I mean. :)

Code:
#include <stdio.h>
#include <math.h>
int main(void)

{
//variables
	int rounded_x;
	double x, y, z;
	double scale(double, int);
	
//input
	printf ("Welcome.\n");
	printf("Enter a positive number: \n");
//the number is set to x
	scanf("%lf", &x);

//y is the result of the call to the scale function
	y = scale(x, 2);
//rounded_x is the result of checking the rounded number
	rounded_x = (int) (y + .5);
//z is the result of the call to scale with the rounded number
	z = scale(rounded_x, -2);
	
//output
	printf("Rounded number is: %.4lf \n", z);
//end
	return 0;
}

//double x represent the x or rounded_x
//int n represents the power of 2 or -2
double scale(double x, int n)
{
	double scale_factor;
	scale_factor = pow(10,n);
	return (x * scale_factor);
}

I lumped together what everyone said, and I took everything everyone suggested, and I walked through it with the friggin' alphabet, and wouldn't you know, it was the most sensible solution I've ever found.

I'm going to fix up the comments, but I just wanted to get this up here because I feel like a giddy school child. Thankyouthankyouthankyou!
 

naples98

macrumors member
Sep 9, 2008
95
3
Houston
Great job

Great job on the final program. It looks like you may have finally figured it out. Although, I still have no idea why you add .5 to y.

Code:
//rounded_x is the result of checking the rounded number
	rounded_x = (int) (y + .5);

For example, if you your function returns 735.88 and you add .5 to it, you get 736.38. Now you cast that to an int which gives you 736 when you really wanted 735. Am I missing something? :confused:
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
For example, if you your function returns 735.88 and you add .5 to it, you get 736.38. Now you cast that to an int which gives you 736 when you really wanted 735. Am I missing something? :confused:

Because the default behaviour when casting a floating point to an int is to truncate the value. So 735.88 would become 735, which is fine if all you want is to cut off the decimal part, but wrong if you actually want to round up/down to the nearest number mathemetically. 735.88 should round up to 736.

Adding 0.5 to the number simulates the effect of "rounding up" because, if the existing fractional value was greater than 0.5, then it would bump up the number to the next whole number, which then gets truncated. If the number was below the 0.5 threshold, then adding 0.5 to it doesn't bring it past the current whole number, so it still gets truncated properly.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,758
8,451
A sea of green
I'm going to fix up the comments, but I just wanted to get this up here because I feel like a giddy school child.

You have done well. <mechanical inhale> Welcome to the dark side, my son. <mechanical inhale> Now that you have proven yourself, you can be tutored in the true powers that rule the galaxy. <mechanical inhale> Revel in it now, for giddiness is an unsuitable display for a future Sith Lord.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Huzzah! Hopefully through trial and error you found some good methods of working things out that you can apply in your future projects.

-Lee
 

naples98

macrumors member
Sep 9, 2008
95
3
Houston
Because the default behaviour when casting a floating point to an int is to truncate the value. So 735.88 would become 735, which is fine if all you want is to cut off the decimal part, but wrong if you actually want to round up/down to the nearest number mathemetically. 735.88 should round up to 736.

Adding 0.5 to the number simulates the effect of "rounding up" because, if the existing fractional value was greater than 0.5, then it would bump up the number to the next whole number, which then gets truncated. If the number was below the 0.5 threshold, then adding 0.5 to it doesn't bring it past the current whole number, so it still gets truncated properly.

Ok, that makes much more sense. I obviously missed that. Thanks! :)
 

SkippyThorson

macrumors 68000
Original poster
Jul 22, 2007
1,670
942
Utica, NY
You have done well. <mechanical inhale> Welcome to the dark side, my son. <mechanical inhale> Now that you have proven yourself, you can be tutored in the true powers that rule the galaxy. <mechanical inhale> Revel in it now, for giddiness is an unsuitable display for a future Sith Lord.

:) Pleased with my submission, the lady is. Knowledge, gained, I have. Trial and error, the best method, is not. Understanding, I hope I now have.

Thanks again for all the help, everyone. I think I have a better understanding of how to plan and walk through issues like this now.

I'll be back... :cool: (Eh, wrong movie.)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.