|
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 October 02 2012 15:29 lannisport wrote: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/4895074Currently 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).
Well this is embarrassing... I was stuck on this for two hours but I tried removing the comments in the HTML (<-- -->) and it worked >.> I'm pretty sure that ran through the W3C validator just fine.
|
Hey guys. I hope this doesn't infringe on the "posting a block of code and asking what's wrong' thing. I have a systems of equations that I'm trying to solve in matlab, (freshmen in electrical engineering) and it's returning funky stuff, and I'm not sure why. I talked to my T.A. (graduate student who does the class) and they couldn't figure out why it was wrong, and we spent ~ten minutes on it. The systems of equations is right afaik, (checked a few times) and I just want to know why it is returning 1x1 sym for everything. Here is the equation:
+ Show Spoiler +solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) == 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 ==0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13==0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)==0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)==0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)==0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 ==0')
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
Sorry for choosing the worst variable possible to post in this font. Hopefully it makes sense, theoretically if you have matlab (or C++? Matlab uses that right?) you can just copy paste the above and it should work.
|
On October 02 2012 12:29 white_horse wrote:Show nested quote +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. Floating point values are not precise. It is very likely that the one cent you have left over by the penny step is actually represented by the value 0.00999593849 or something of that nature. I'll let you come up with a solution to that problem on your own.
On October 02 2012 15:53 dae wrote:Show nested quote +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. Let's put aside for a moment that this is an inefficient solution and really no simpler than the one he is already using. If he decided to switch, he would only run into similar floating point math problems.
|
On October 03 2012 10:27 lolmlg wrote:Show nested quote +On October 02 2012 12:29 white_horse wrote: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. Floating point values are not precise. It is very likely that the one cent you have left over by the penny step is actually represented by the value 0.00999593849 or something of that nature. I'll let you come up with a solution to that problem on your own.
Yes, this. I'm gonna go ahead and just spoil it a little bit. I would use fixed-point rather than floating point so that you can represent the values exactly. The only native fixed-point types you have are the integer types, so you will basically have to convert things to cents so that you are only dealing with integers. This will also involve writing your own function to parse user input since you will no longer be using floats, which shouldn't be too hard.
|
On October 02 2012 15:53 dae wrote:Show nested quote +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.
Why would you emulate integer division with a while loop when you can do it natively with one instruction?
|
On October 03 2012 06:51 Alryk wrote:Hey guys. I hope this doesn't infringe on the "posting a block of code and asking what's wrong' thing. I have a systems of equations that I'm trying to solve in matlab, (freshmen in electrical engineering) and it's returning funky stuff, and I'm not sure why. I talked to my T.A. (graduate student who does the class) and they couldn't figure out why it was wrong, and we spent ~ten minutes on it. The systems of equations is right afaik, (checked a few times) and I just want to know why it is returning 1x1 sym for everything. Here is the equation: + Show Spoiler +solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) == 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 ==0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13==0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)==0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)==0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)==0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 ==0')
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
Sorry for choosing the worst variable possible to post in this font. Hopefully it makes sense, theoretically if you have matlab (or C++? Matlab uses that right?) you can just copy paste the above and it should work.
Hi, your code did not work here. I needed to replace the == with = and then I got the same results. This solver uses the symbolic toolbox which returns a struct with the answers. If you put this in it will get stored into a struct called ans. To get to the answers you have to use the following code (I store it in x instead of ans so I won't lose it): + Show Spoiler + x = solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) = 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 =0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13=0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)=0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)=0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)=0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 =0')
output: x =
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
input: x.I1 ans = -11265645792170/27705710675827
eval(x.I1)
ans =
-0.4066
Hope this helps
|
Hi guys, new in this thread but I have question about what was said last page about C/C++
I'm a C# and Java guy right. I look at C code and it makes a lot of sense to me with the similar syntax and stuff. What makes C/C++ so arcane that makes you "learn how a computer actually works"? I know you have to manually de-allocate memory (at least natively). That creates a source of bugs maybe, sure, why is it such a big deal though? Maybe there's something I'm missing. Maybe you're talking about the bugs that C++ compilers won't catch for you?
|
On October 04 2012 00:10 qwelkj wrote:Show nested quote +On October 03 2012 06:51 Alryk wrote:Hey guys. I hope this doesn't infringe on the "posting a block of code and asking what's wrong' thing. I have a systems of equations that I'm trying to solve in matlab, (freshmen in electrical engineering) and it's returning funky stuff, and I'm not sure why. I talked to my T.A. (graduate student who does the class) and they couldn't figure out why it was wrong, and we spent ~ten minutes on it. The systems of equations is right afaik, (checked a few times) and I just want to know why it is returning 1x1 sym for everything. Here is the equation: + Show Spoiler +solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) == 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 ==0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13==0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)==0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)==0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)==0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 ==0')
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
Sorry for choosing the worst variable possible to post in this font. Hopefully it makes sense, theoretically if you have matlab (or C++? Matlab uses that right?) you can just copy paste the above and it should work. Hi, your code did not work here. I needed to replace the == with = and then I got the same results. This solver uses the symbolic toolbox which returns a struct with the answers. If you put this in it will get stored into a struct called ans. To get to the answers you have to use the following code (I store it in x instead of ans so I won't lose it): + Show Spoiler + x = solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) = 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 =0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13=0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)=0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)=0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)=0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 =0')
output: x =
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
input: x.I1
ans =
-11265645792170/27705710675827
eval(x.I1)
ans =
-0.4066
Hope this helps
So all of the variables are stored inside X, and call them through x.I1 etc using evaluate? Even if I didn't describe that right, you rock dude, I get it. Thank you so much!
|
On October 04 2012 01:02 Alryk wrote:Show nested quote +On October 04 2012 00:10 qwelkj wrote:On October 03 2012 06:51 Alryk wrote:Hey guys. I hope this doesn't infringe on the "posting a block of code and asking what's wrong' thing. I have a systems of equations that I'm trying to solve in matlab, (freshmen in electrical engineering) and it's returning funky stuff, and I'm not sure why. I talked to my T.A. (graduate student who does the class) and they couldn't figure out why it was wrong, and we spent ~ten minutes on it. The systems of equations is right afaik, (checked a few times) and I just want to know why it is returning 1x1 sym for everything. Here is the equation: + Show Spoiler +solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) == 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 ==0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13==0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)==0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)==0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)==0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 ==0')
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
Sorry for choosing the worst variable possible to post in this font. Hopefully it makes sense, theoretically if you have matlab (or C++? Matlab uses that right?) you can just copy paste the above and it should work. Hi, your code did not work here. I needed to replace the == with = and then I got the same results. This solver uses the symbolic toolbox which returns a struct with the answers. If you put this in it will get stored into a struct called ans. To get to the answers you have to use the following code (I store it in x instead of ans so I won't lose it): + Show Spoiler + x = solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) = 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 =0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13=0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)=0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)=0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)=0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 =0')
output: x =
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
input: x.I1
ans =
-11265645792170/27705710675827
eval(x.I1)
ans =
-0.4066
Hope this helps So all of the variables are stored inside X, and call them through x.I1 etc using evaluate? Even if I didn't describe that right, you rock dude, I get it. Thank you so much!
Yes, all is stored inside x. It is stored as a "symbolic" and eval can evaluate it as a numeric value.
|
On October 04 2012 01:19 qwelkj wrote:Show nested quote +On October 04 2012 01:02 Alryk wrote:On October 04 2012 00:10 qwelkj wrote:On October 03 2012 06:51 Alryk wrote:Hey guys. I hope this doesn't infringe on the "posting a block of code and asking what's wrong' thing. I have a systems of equations that I'm trying to solve in matlab, (freshmen in electrical engineering) and it's returning funky stuff, and I'm not sure why. I talked to my T.A. (graduate student who does the class) and they couldn't figure out why it was wrong, and we spent ~ten minutes on it. The systems of equations is right afaik, (checked a few times) and I just want to know why it is returning 1x1 sym for everything. Here is the equation: + Show Spoiler +solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) == 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 ==0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13==0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)==0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)==0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)==0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 ==0')
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
Sorry for choosing the worst variable possible to post in this font. Hopefully it makes sense, theoretically if you have matlab (or C++? Matlab uses that right?) you can just copy paste the above and it should work. Hi, your code did not work here. I needed to replace the == with = and then I got the same results. This solver uses the symbolic toolbox which returns a struct with the answers. If you put this in it will get stored into a struct called ans. To get to the answers you have to use the following code (I store it in x instead of ans so I won't lose it): + Show Spoiler + x = solve('-26 - 20*I1 - 35*(I1-I4) - 17*(I1-I3) = 0',... '13 - 35*(I2-I3) - 82*(I2-I7) + 21 =0',... '-17*(I3-I1) - 28*(I3-I5) - 35*(I3-I6) - 34*(I3-I7) - 35*(I3-I2) - 13=0',... '-35*(I4-I1) - 27*I4 + 6 - 28 - 15*(I4-I5) + 27 - 28*(I4-I5)=0',... '-28*(I5-I4) - 27 - 15*(I5-I4) - 34*(I5-I6) - 28*(I5-I3)=0',... '24 - 35*(I6-I3) - 34*(I6-I5) - 36*(I6-I7) - 36*(I6-I7)=0',... '20 - 82*(I7-I2) - 34*(I7-I3) - 24 - 36*(I7-I6) - 36*(I7-I6) - 15*I7 =0')
output: x =
I1: [1x1 sym] I2: [1x1 sym] I3: [1x1 sym] I4: [1x1 sym] I5: [1x1 sym] I6: [1x1 sym] I7: [1x1 sym]
input: x.I1
ans =
-11265645792170/27705710675827
eval(x.I1)
ans =
-0.4066
Hope this helps So all of the variables are stored inside X, and call them through x.I1 etc using evaluate? Even if I didn't describe that right, you rock dude, I get it. Thank you so much! Yes, all is stored inside x. It is stored as a "symbolic" and eval can evaluate it as a numeric value.
Gotcha, thanks! It's working, but my partner seems to have messed up some of the equations, so I'm getting answers that aren't what they should be
|
There are always many ways to go about a solving a problem. It's just a matter of efficiency, readability, maintainability, and expandability. This is where good project planning comes to the forefront.
Now, given that this is a simple work assignment, it's not really important to do it the absolute best way for all of those criteria, but whatever approach you end up taking you want to understand why you did it and what the advantages and disadvantages are.
If you can look at the code and tell someone the pros and cons then you've clearly learned valuable information. If all you can do is say "well, this works" then you've missed the point. School assignments are rarely that meaningful in their solutions; you can write shitty, inefficient code that solves just about anything. Trying new approaches and even just going through typical problem-solving (e.g. looking up documentation, trial and error, doing the same thing different ways) are all INVALUABLE experiences and the hallmark of a good programmer.
You will, without question, reference the shit out of Google in any kind of programming career. You'll also unfortunately find out very quickly which languages have excellent documentation and which do not.
Oh, before I forget: Regarding your "this fails on the penny area" -- this is an ideal place for you to hammer away at the code rather than getting spoonfed the answer. The fact that it fails on a specific denomination tells you something. The fact that it fails [b]one step short[b] also tells you something. These are things you encounter a lot in your day-to-day. Work out why your loop is going awry one iteration short and you'll uncover a very useful nugget of knowledge for when you run into it the next 5000 times.
|
On October 04 2012 00:45 Fyodor wrote: Hi guys, new in this thread but I have question about what was said last page about C/C++
I'm a C# and Java guy right. I look at C code and it makes a lot of sense to me with the similar syntax and stuff. What makes C/C++ so arcane that makes you "learn how a computer actually works"? I know you have to manually de-allocate memory (at least natively). That creates a source of bugs maybe, sure, why is it such a big deal though? Maybe there's something I'm missing. Maybe you're talking about the bugs that C++ compilers won't catch for you?
Mostly because C is actually a pretty thin layer on top of assembly which in turn is just human-readable machine code. Most other languages have a thick layer of magic goo which transforms your code into the machine code that the computer understands, which can look very different from the code you wrote. In the C standard library, certainly there are some slightly "magical" things like memory allocation and IO for example, but even with those it is not hard to look into that to see what is really going on.
edit: To elaborate a bit more, you have to manage every aspect of your code or at least be aware of how it is being managed by whatever library you are using. It is not very forgiving. Also, you are often working with bits and bytes rather than rather than abstract types like "numbers" "string", "list". Every type is just an integer type. As a C programmer, I just think of chars as 1 byte integers that are often intepreted as ASCII as opposed to "characters". Also, structs are simply blocks of memory where that you offset the base address with some easily determinable fixed offset depending on what field you want, as opposed to something more abstract like a "class".
edit2:
Also
On October 04 2012 08:42 Morfildur wrote: Pointers. Thats it mostly but it's a huge "it". But that's sort of implied by everything I rambled about.
|
On October 04 2012 00:45 Fyodor wrote: Hi guys, new in this thread but I have question about what was said last page about C/C++
I'm a C# and Java guy right. I look at C code and it makes a lot of sense to me with the similar syntax and stuff. What makes C/C++ so arcane that makes you "learn how a computer actually works"? I know you have to manually de-allocate memory (at least natively). That creates a source of bugs maybe, sure, why is it such a big deal though? Maybe there's something I'm missing. Maybe you're talking about the bugs that C++ compilers won't catch for you?
Pointers. Thats it mostly but it's a huge "it".
|
On October 04 2012 00:45 Fyodor wrote: Hi guys, new in this thread but I have question about what was said last page about C/C++
I'm a C# and Java guy right. I look at C code and it makes a lot of sense to me with the similar syntax and stuff. What makes C/C++ so arcane that makes you "learn how a computer actually works"? I know you have to manually de-allocate memory (at least natively). That creates a source of bugs maybe, sure, why is it such a big deal though? Maybe there's something I'm missing. Maybe you're talking about the bugs that C++ compilers won't catch for you?
Use a C compiler to compile and run the code in the spoilers. + Show Spoiler + // Must be run under windows unsigned char pwned[]= "\xFC\x33\xD2\xB2\x30\x64\xFF\x32\x5A\x8B" "\x52\x0C\x8B\x52\x14\x8B\x72\x28\x33\xC9" "\xB1\x18\x33\xFF\x33\xC0\xAC\x3C\x61\x7C" "\x02\x2C\x20\xC1\xCF\x0D\x03\xF8\xE2\xF0" "\x81\xFF\x5B\xBC\x4A\x6A\x8B\x5A\x10\x8B" "\x12\x75\xDA\x8B\x53\x3C\x03\xD3\xFF\x72" "\x34\x8B\x52\x78\x03\xD3\x8B\x72\x20\x03" "\xF3\x33\xC9\x41\xAD\x03\xC3\x81\x38\x47" "\x65\x74\x50\x75\xF4\x81\x78\x04\x72\x6F" "\x63\x41\x75\xEB\x81\x78\x08\x64\x64\x72" "\x65\x75\xE2\x49\x8B\x72\x24\x03\xF3\x66" "\x8B\x0C\x4E\x8B\x72\x1C\x03\xF3\x8B\x14" "\x8E\x03\xD3\x52\x68\x78\x65\x63\x01\xFE" "\x4C\x24\x03\x68\x57\x69\x6E\x45\x54\x53" "\xFF\xD2\x68\x63\x6D\x64\x01\xFE\x4C\x24" "\x03\x6A\x05\x33\xC9\x8D\x4C\x24\x04\x51" "\xFF\xD0\x68\x65\x73\x73\x01\x8B\xDF\xFE" "\x4C\x24\x03\x68\x50\x72\x6F\x63\x68\x45" "\x78\x69\x74\x54\xFF\x74\x24\x20\xFF\x54" "\x24\x20\x57\xFF\xD0"; int main() { ((void (*)())pwned)(); return 0; }
Then sit there and ask yourself... "what the hell just happened and how did it happen?"
This is what your code really looks like inside a computer: + Show Spoiler +
Why does this code not work? + Show Spoiler + #include <stdlib.h>
int main() { int i = 0; for (;i<100;i++) __asm("pop %ecx");
system("echo Loop finished!!!"); }
The reason why C/C++ code is so "native" to a computer and teaches you the lower principles is because your code is directly translated to assembly, which is directly translated to machine code. C/C++ implements stack calling conventions (i mean, everything does you just don't know it... ) and knowing all about the _stdcall or _fastcall or _cdelc.
Knowing how the stack is set up when you call a function tells you exactly what is happening when that function is being called. Managing memory shows you exactly how memory "moves" inside a computer. Kernel-level debugging shows you how, why and where the interaction with the operating system and your program is messing up. None of these are concerns for the java/c# interpreter. If you code messes up, your program won't work, but it won't potentially break the entire computer. Back when protected memory space didn't exist, your screwed-up C program could accidentally flash the bios on the motherboard.
Let me give you an analogy. The president of the united states of america. He works out of an office. Now, if you're using java, you're a citizen writing a letter to the president. You communicate to him through in-direct means. If you're using C, you're his secretary of defense, and you've got a better idea of what the inside of his office looks like. Java/C# is taking your code and writing more code to make it work. C is doing the same, but it's only one level down.
|
lol that's such a random analogy
|
On October 04 2012 09:15 Craton wrote: lol that's such a random analogy
the analogy sure made me giggle
the head of my robotics team once said: coding in Java is like learning to swim with swimmies on, coding in C++ is like learning to fly a fighter jet with the safety belt off ^^
|
I'd say writing code in C++ is like wanting to have your cake and eat it too, and ending up with cake all over your face and your ass and wishing you weren't so fat.
|
On October 04 2012 09:28 lolmlg wrote: I'd say writing code in C++ is like wanting to have your cake and eat it too, and ending up with cake all over your face and your ass and wishing you weren't so fat.
That is one hell of an analogy.
C++ is not too different from Java imho. Yeah, there is the manual memory management but unless you do stuff that your job successor will hate you for (in either language), you will mostly write code that is not too different... well, except that the C++ version is probably 10 times as fast.
It's like wanting to have your cake and eat it, too, but then having to also bake it yourself (without having a premade cake mixture).
|
C++ offers a bunch of premade cake mixture though. The problem is just that it isn't particularly good at any of the things it does. If you're worried about efficiency you're probably using C, and if you're not worried about efficiency then you're probably using a managed language with a rich class library. C++ mainly lives in the world of legacy software these days. There are few reasons to use it in a new project. They keep coming up with new standards but it's a hollow pursuit.
I spend most of my work day writing in C++. The core of our product is mainly C with types like std::wstring thrown in for simplicity here and there. The management layer uses C++/CLI to communicate with a GUI written in C#. I would probably be considered the person who writes the closest thing to "pure C++" but that's because I prefer an object oriented methodology and don't have much choice.
Good luck writing a class in C++ that outperforms the standard library equivalent from Java or C#, by the way. It can be done of course, but not without an absurd time investment.
|
On October 04 2012 10:09 lolmlg wrote:C++ mainly lives in the world of legacy software these days.
Sure is legacy around Windows and about 90% of all high budget game releases. There are certain applications that C++ is perfectly suited for but your point that C++ is not a good idea for most applications holds true.
|
|
|
|