|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On September 26 2012 08:20 RoyGBiv_13 wrote:Show nested quote +On September 26 2012 01:25 jergason wrote:On September 25 2012 17:05 Morfildur wrote:
C is a great language... but you won't use it in the real world unless you program for microprocessors or on the linux kernel. C++ is the closest which is more often used and these days mostly used when developing 3D games/engines. You won't do either until far further into your programming career - if ever.
. . .
Note that i don't mean "Don't learn C". It's good to know C and how everything works behind the scenes, i really recommend learning it at some point, but it won't really land you any jobs.
This is untrue. Good C programmers earn top dollar. If you are doing software development, pretty much any field can make you lots of money. Pick what is interesting to you and you'll succeed. Don't pick and choose based on how much money you can make off of it. Can vouch for that  C/assembly/occasional machine code is way fun. Learning the complexity of the C language will unveil the mysteries of hardware. Also, <3 money. Also, +1 for supporting python as a first language. Awesome scripting language that will generate a ton of ideas for programs right out the gate. Starting with languages like java or C, you have to know quite a bit about packages and design in order to do some of the simplest things like read file metadata to make a decision. Starting with a web language is great, but only if that is what you want to do, and its very much a one way street (harder to go down the stack than up).
i did java as my first language and i am yet to come across a need to read metadata of files. but then again i have been strictly on the games programming side and any file editing is something i set up completely myself. also, knowledge is not needed about packages to make an application.
|
1019 Posts
Hey guys I'm working on this problem where the user inputs an amount of money between $0.00 and $19.99, and the program outputs the best way to calculate the change of that amount. So for example, if I put in "$9.94", then the program should output
One $5 bill 4 $1 bills 3 quarters 1 dime 1 nickel 4 pennies
I got this far
float total; cout << "Input a dollar amount between 0.00 and 19.99 (inclusive): "; cin >> total;
numTenDollar = static_cast<int>(total)*100 / (10*100); numFiveDollar = static_cast<int>((total - numTenDollar))*100 / (5*100); numOneDollar = static_cast<int>((total - numTenDollar - numFiveDollar))*100 / (1*100);
but the program is really weird when I run it and I think theres something wrong with the calculations. Can you guys help me out? Thanks.
|
Your total is in dollars, but when calculating the remainder after you take out the fives and tens, you minus only the number of them, not the dollar amounts.
It should look like this, I think:
numTenDollar = static_cast<int>(total)*100 / (10*100); numFiveDollar = static_cast<int>((total - (numTenDollar*10)))*100 / (5*100); numOneDollar = static_cast<int>((total - (numTenDollar*10) - (numFiveDollar*5)))*100 / (1*100);
I believe that would solve the problem.
|
So I think something you may find helpful is to subtract things from the total as you go, that way you don't have to keep doing these long chains of subtraction and multiplication. So for instance something like:int leftover = total*100; numTenDollar = leftover / (10*100)); leftover -= numTenDollar * (10*100); ... Doing it that way would probably limit the amount of issues you'll have.
Another suggestion would be to abstract this more, and create a structure with the different currency denominations and their values (in cents probably). Then you could simply loop through that list and figure the number of each until you had no leftover cents, and then loop through your results to display them. It would probably be a lot less code, and less code means less opportunity for error. You could also then solve the problem much more easily for any arbitrary currency denominations!
|
1019 Posts
Ok I did that, but a little differently, thanks. But the change doesn't appear for the coins (dimes, nickels, pennies) when I run it. Here is the rest of the code:
numTenDollar = static_cast<int>(total)*100 / (10*100); total = total - (numTenDollar*10); numFiveDollar = static_cast<int>(total)*100 / (5*100); total = total - (numFiveDollar*5); numOneDollar = static_cast<int>(total)*100 / (1*100); total = total - (numOneDollar*1); numQuarter = static_cast<float>(total)*100 / (0.25*100); total = total - (numQuarter*0.25); numDime = static_cast<float>(total)*100 / (0.10*100); total = total - (numDime*0.10); numNickel = static_cast<float>(total)*100 / (0.05*100); total = total - (numNickel*0.05); numPenny = static_cast<float>(total)*100 / (0.01*100);
what am I doing wrong??? I cant figure it out
|
So you're casting the total to a float for the change, which means that when you do the division its going to do float division, rather than int. What you really want is something like: numQuarter = static_cast<int>(total)*100 / 25; total = total - (numQuarter*0.25);
I'd really suggest that you develop a primal urge to never repeat stuff like you're doing in that snippet though; it will make you a much better coder.
|
To solve that problem properly you should really use an entirely different sort of solution. Google the phrase "dynamic programming". You should try to come up with a recursive algorithm that reduces the problem to a base case and then constantly checks partial solutions to determine what the best answer is.
|
On September 30 2012 10:16 lolmlg wrote: To solve that problem properly you should really use an entirely different sort of solution. Google the phrase "dynamic programming". You should try to come up with a recursive algorithm that reduces the problem to a base case and then constantly checks partial solutions to determine what the best answer is. Well, that too, but I think that's a bit past his depth atm. The assignment is probably meant more to develop the greedy algorithm that fails in some cases, rather than the exact correct solution.
|
On September 30 2012 10:04 white_horse wrote: Ok I did that, but a little differently, thanks. But the change doesn't appear for the coins (dimes, nickels, pennies) when I run it. Here is the rest of the code:
numTenDollar = static_cast<int>(total)*100 / (10*100); total = total - (numTenDollar*10); numFiveDollar = static_cast<int>(total)*100 / (5*100); total = total - (numFiveDollar*5); numOneDollar = static_cast<int>(total)*100 / (1*100); total = total - (numOneDollar*1); numQuarter = static_cast<float>(total)*100 / (0.25*100); total = total - (numQuarter*0.25); numDime = static_cast<float>(total)*100 / (0.10*100); total = total - (numDime*0.10); numNickel = static_cast<float>(total)*100 / (0.05*100); total = total - (numNickel*0.05); numPenny = static_cast<float>(total)*100 / (0.01*100);
what am I doing wrong??? I cant figure it out
Two major mistakes : - You are badly using variables types, you use a huge mess of cast between floats/ints (this can be tricky sometimes so stay with a clean plan on what type to use) - You repeat yourself which is bad :p
Take a look at this: (this is fundamentaly different from your code, but it still does the same thing, in the same language)
// 21.23 $ int penny = 2123; // use what ever input you want
char *titles[] = {"10 dollar", "5 dollar", "1 dollar", "quarter", "dime", "penny"}; int units[] = {1000, 500, 100, 25, 10, 1}; // in penny int i;
for (i = 0; i < sizeof(units) / sizeof(int); i++){ printf("%d * %s\n", penny / units[i], titles[i]); penny -= penny / units[i] * units[i]; }
Output : 2 * 10 dollar 0 * 5 dollar 1 * 1 dollar 0 * quarter 2 * dime 3 * penny
|
On September 30 2012 10:27 tec27 wrote:Show nested quote +On September 30 2012 10:16 lolmlg wrote: To solve that problem properly you should really use an entirely different sort of solution. Google the phrase "dynamic programming". You should try to come up with a recursive algorithm that reduces the problem to a base case and then constantly checks partial solutions to determine what the best answer is. Well, that too, but I think that's a bit past his depth atm. The assignment is probably meant more to develop the greedy algorithm that fails in some cases, rather than the exact correct solution. I could have responded with a simpler solution, but he said that he was looking for "the best" one. If he's taking an introductory class or something of that nature, they probably aren't looking for the most accurate algorithm.
Edit: As it happens, the greedy algorithm can be used as part of the recursive solution.
|
1019 Posts
yeah I'm in an introductory c++ class. I've studied on my own a little bit about arrays and stuff but we're not there yet in the class. I changed all the static_cast<float> to static_cast<int> and the program works for the bills but doesn't work for the coins. I know there must be something wrong with the calculation - can you guy help me find it...........
|
On September 30 2012 10:40 white_horse wrote: yeah I'm in an introductory c++ class. I've studied on my own a little bit about arrays and stuff but we're not there yet in the class. I changed all the static_cast<float> to static_cast<int> and the program works for the bills but doesn't work for the coins. I know there must be something wrong with the calculation - can you guy help me find it...........
numPenny = static_cast<int>(total)*100 / (0.01*100); You are casting "total" into int, which instantly kill floating point informations. As i said before, BE CAREFULL WITH THE TYPES ! 
try:
numPenny = static_cast<int>(total / 0.01);
|
1019 Posts
I tried out your suggestion and the program is working better, thanks. Could you explain why your suggestion works? In yours, total / 0.01 is still static cast to an int, isn't it.
Also could someone tell me how to write in code format using the bbcode, thanks
|
Here, maybe this will help explain it:
int float int float int numDime = static_cast<int>(1.07) * 100 / (0.10 * 100); => int float numDime = 107 / 10.0 => float numDime = 10.7
int float float numDime = static_cast<int>(total / 0.10); => int numDime = 10
As you can see, having a float in part of your calculation will cause the ints to be casted to floats as well. So because part of your equation was an uncasted float, the int casted total was changed back to a float, and the result was thus fractional as well.
|
That explanation isn't really correct. The problem with the last version of his code was that the call to static_cast took place first. He wanted it to take place last. The line
static_cast<int>(1.07) * 100 / (0.10 * 100);
if it compiles (which it might not, if warnings are treated as errors) will yield the answer 10.0. In that example, all he would need to do in order to get the answer he wanted was surround the entire set of numeric operations with brackets, or use two static_cast calls to avoid the warning.
static_cast<int>(total * 100 / (0.10 * 100)); or static_cast<int>(total) * 100 / static_cast<int>(0.10 * 100);
But then the question becomes, why bother with the multiplication? The operation can be reduced to the following:
static_cast<int>(total / 0.10);
Edit: Of course, this example didn't really apply to his program, because by the time he hit dimes, the amount of money should have been less than one, in which case the static_cast of total would have returned 0. So both the original line and the line with two static_cast calls would not have worked in his situation. The second line and the final one would be fine.
|
Well, I have been recently playing with an API called Parse (www.parse.com), and I was amazed when I realized the guys that are working on this are huge SC2 fans!! Check out the example code!! haha so fun :D. If you guys can find any more references let me know :D https://parse.com/docs/ios_guide
Day[9] + Show Spoiler + PFObject *gameScore = [PFObject objectWithClassName:@"GameScore"]; [gameScore setObject:[NSNumber numberWithInt:1337] forKey:@"score"]; [gameScore setObject:@"Sean Plott" forKey:@"playerName"]; [gameScore setObject:[NSNumber numberWithBool:NO] forKey:@"cheatMode"]; [gameScore save];
Artosis + Show Spoiler + PFQuery *query = [PFQuery queryWithClassName:@"GameScore"]; [query whereKey:@"playerName" equalTo:@"Dan Stemkoski"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { // The find succeeded. NSLog(@"Successfully retrieved %d scores.", objects.count); } else { // Log details of the failure NSLog(@"Error: %@ %@", error, [error userInfo]); } }];
Spanishiwa + Show Spoiler + [query whereKey:@"playerName" notEqualTo:@"Michael Yabuti"];
Sheth, TLO, Jinro + Show Spoiler + // Finds scores from anyone who is neither Jonathan, Dario, nor Shawn NSArray *names = [NSArray arrayWithObjects:@"Jonathan Walsh", @"Dario Wunsch", @"Shawn Simon", nil]; [query whereKey:@"playerName" notContainedIn:names];
|
|
1019 Posts
On September 30 2012 17:29 lolmlg wrote: That explanation isn't really correct. The problem with the last version of his code was that the call to static_cast took place first. He wanted it to take place last. The line
static_cast<int>(1.07) * 100 / (0.10 * 100);
if it compiles (which it might not, if warnings are treated as errors) will yield the answer 10.0. In that example, all he would need to do in order to get the answer he wanted was surround the entire set of numeric operations with brackets, or use two static_cast calls to avoid the warning.
static_cast<int>(total * 100 / (0.10 * 100)); or static_cast<int>(total) * 100 / static_cast<int>(0.10 * 100);
But then the question becomes, why bother with the multiplication? The operation can be reduced to the following:
static_cast<int>(total / 0.10);
Edit: Of course, this example didn't really apply to his program, because by the time he hit dimes, the amount of money should have been less than one, in which case the static_cast of total would have returned 0. So both the original line and the line with two static_cast calls would not have worked in his situation. The second line and the final one would be fine.
OK thank you so much that helped a lot. I have the program basically done now, and there is one problem left: it works for every input except 19.96, 19.97, 19.98, and 19.99.
For example, 19.96 outputs everything right except at the very end, it doesn't output "One penny" on the screen when it needs to. And then 19.97 outputs only one penny at the end when it needs to output two. Do you know what is wrong with this? The assignment tells us to be careful about inputting 19.99 so it seems like this is a problem that we are supposed to run into.
|
I don't know if any of you guys are familiar with wordpress but I'm trying to use it as a content management system for a website. So my goal is this... To have a "template" with a header and footer in it, and to use wordpress to insert the content of the page inbetween those two. In HTML. It seems simple but I can't figure out how to do this.
Anyone have any suggestions? I just wanna insert plain HTML into wordpress to make the content of a page...
+ Show Spoiler +<?php /*Template Name:default */ ?> <?php get_header(); ?> <!-- This gets the header -->
<!-- The container is styled in CSS, so in WordPress I can simply type in the rest of the divs. "div id="section1", "div id="quote" etc. --> <div id ="container">
<!-- Start the Loop. This is the WP Loop that returns the content the I type in the wordpress box and puts it into the div class "entry" --> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div class="entry"> <?php the_content(); ?> </div>
<?php endwhile; else: ?> <?php endif; ?> </div>
<?php get_footer(); ?> <!-- This gets the footer -->
Neater version: http://pastie.org/4895074
Currently this script messes up the layout of the page, and even some of the images go missing. However, it's all one CSS file so I don't understand why the layout would mess up (missing images probably a simple path problem).
|
On September 30 2012 09:38 white_horse wrote: Hey guys I'm working on this problem where the user inputs an amount of money between $0.00 and $19.99, and the program outputs the best way to calculate the change of that amount. So for example, if I put in "$9.94", then the program should output
One $5 bill 4 $1 bills 3 quarters 1 dime 1 nickel 4 pennies
I got this far
float total; cout << "Input a dollar amount between 0.00 and 19.99 (inclusive): "; cin >> total;
numTenDollar = static_cast<int>(total)*100 / (10*100); numFiveDollar = static_cast<int>((total - numTenDollar))*100 / (5*100); numOneDollar = static_cast<int>((total - numTenDollar - numFiveDollar))*100 / (1*100);
but the program is really weird when I run it and I think theres something wrong with the calculations. Can you guys help me out? Thanks.
Wouldn't this problem be very easy to solve by just using a series of while loops, one for each currency denomination?
eg: while(currentAmount-10>=0) { numTenBills++; currentAmount-=10; }
You could even print the amounts as you go if you don't want to use a different variable for each denomination.
|
|
|
|