Why are you sqrt()'ing it ? a simple
[cpp]
if (n == 0) { result = 0; }
for (int i = 1; i <= n; i++)
{
result *= i;
}
[/cpp]
would work.
Or is it on purpose, to test absolute errors ?
Are you talking to me? It's an equation with an exception at 0, at which point the equation says 0, but the real value for the factorial is 1.
[QUOTE=sloppy_joes;25022671]Programming in C
Besides the fact that the value for output comes out slightly fucked up.
Abserror and relerror keep making values way over what is expected.
[code]--codesnip--[/code]The factorial for 8 is 40320.
So abserror is expected around 400, relerror is expected at .01
[editline]03:33PM[/editline]
P.S. don't call me a retard because i code terribly, i'm doing next weeks works before we cover it.
[editline]03:38PM[/editline]
Also, please don't quote my code, this is for a course and I can't seem to figure out why its so fucked up.[/QUOTE]
You need to use fabs for floating-point abs. Else it will be converted to an integer type.
You should also use %lf instead of %f to output doubles.
[QUOTE=ROBO_DONUT;25022325]Also, a #define is no different than typing out 3.141592654 manually. It'll be whatever precision it needs to be. IIRC, it "prefers" double if you don't have the "f" suffix.[/QUOTE]
3 -> int
3.0 -> double
3.0f / 3f -> float
This, of course, assumes no implicit casting is taking place.
[QUOTE=ROBO_DONUT;25022325]You totally misunderstood..[/QUOTE]
Yeah, I did. My bad.
[QUOTE=ROBO_DONUT;25022325]Regardless, your posts have a good SNR and my criticism was unwarranted.[/QUOTE]
I gotta admit that was probably my worse nitpick in a long time.
[QUOTE=ZeekyHBomb;25024179]You need to use fabs for floating-point abs. Else it will be converted to an integer type.
You should also use %lf instead of %f to output doubles.[/QUOTE]
fabs of course, thank you.
I'm dealing with an incredibly strange problem right now. I'm working on a socket class and I'm currently working on the function that polls how many bytes are available for reading. Currently it only returns 0 for no bytes to read or 1 for bytes available. The code below prints 0 and 1 as expected:
[cpp]while ( true )
{
if ( client.Available() != available )
{
std::cout << client.Available();
//std::cout << " bytes available";
std::cout << std::endl;
available = client.Available();
}
Sleep( 500 );
}[/cpp]
However, as soon as I uncomment the second std::cout, only "0 bytes available" is printed and nothing else.
So, I'm working on a commission calculator for my Java class.
1. Ask for sales amount
2. Ask for commission code
3. Calculate commission
4. Output
At step 3, it still has the proper amount of commission. Though, in step 4, it displays the commission code as the commission, and I'm not sure why. Any ideas?
[code]
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class commission {
public static void main(String[] args) {
double dollars, answer;
int empCode;
dollars = getSales();
empCode = getCode();
answer = getComm(dollars, empCode);
output(answer, dollars);
finish();
}
public static double getSales() {
double sales = 0.0;
boolean done = false;
while(!done) {
String answer = JOptionPane.showInputDialog(null,
"Enter the sales amount\n(do not use commas or dollar signs)\n or click Cancel to exit:");
if (answer == null) finish();
try {
sales = Double.parseDouble(answer);
if (sales <= 0) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.",
"Error.", JOptionPane.INFORMATION_MESSAGE);
}
}
return sales;
}
public static int getCode() {
int code = 0;
boolean done = false;
while(!done) {
try {
String message = "Enter the commission code:" +
"\n\n1) Telephone Sales\n2) In-Store Sales\n3) Outside Sales\n\n";
code = Integer.parseInt(JOptionPane.showInputDialog(null, message));
if(code < 1 || code > 3) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Please enter a 1, 2, or 3.",
"Error", JOptionPane.INFORMATION_MESSAGE);
}
}
return code;
}
public static double getComm(double employeeSales, int employeeCode) {
double comm = 0.0;
switch(employeeCode) {
case 1:
comm = .10 * employeeSales;
break;
case 2:
comm = .14 * employeeSales;
break;
case 3:
comm = .18 * employeeSales;
break;
}
return employeeCode;
}
public static void output(double comm, double sales) {
DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
JOptionPane.showMessageDialog(null, "Your commission on sales of " + twoDigits.format(sales)
+ " is " + twoDigits.format(comm), "Commission Totals", JOptionPane.INFORMATION_MESSAGE);
}
public static void finish() {
System.exit(0);
}
}
[/code]
return employeeCode;
At the end of getComm()
[editline]12:23AM[/editline]
Surprised this went 42 minutes without an answer...
[QUOTE=RyanDv3;25028325]return employeeCode;
At the end of getComm()
[editline]12:23AM[/editline]
Surprised this went 42 minutes without an answer...[/QUOTE]
You are the best. Thanks bro.
No prob
[QUOTE=ROBO_DONUT;25021082]
The ultimate measure of a program is whether it works. The manner in which you express your code is really nothing but details.[/QUOTE]
This is bullshit. I don't care if your code is hand-optimized, full of clever tricks and farts rainbows: as long as it's clear and concise. If it's easily readable it's easily fixable and that's what really matters. Most programming is maintenance programming, after all.
[QUOTE=ROBO_DONUT;25021082]Please, for once, correct someone on something [i]useful[/i], like suggest a more appropriate approach to a problem in regards to its structure and algorithmic complexity. Your choice of method is far more important than your choice of letters and symbols.[/QUOTE]
It [I]is[/I] bloody useful. The constant doesn't just have type safety advantages (advantages you still don't seem to understand), but it's superior to the macro for a billion other reasons too: it won't reserve the two letters "PI" in sequence whenever it appears (M_PI ring any bells?). you can take the address of the constant without a meaningless, cryptic error. All errors involving the symbol will be much better (because it's an [I]actual[/I] symbol). Uses the C grammar, not the C preprocessor grammar (for lexing tools). The symbol can actually be imported from other code modules if need be. Can't be hijacked by other headers. The list goes on, it's an age-old argument.
All this for free! There are no down-sides to not using the constant over the macro.
(Same goes for C99 inline functions versus macros where applicable, but that's a different story)
do macros support operator overloading
[QUOTE=Jookia;25029590]do macros support operator overloading[/QUOTE]
That doesn't really make sense. Operator overloading is something you do with classes. It has nothing to do with C or the C preprocessor.
[QUOTE=jA_cOp;25029656]That doesn't really make sense. Operator overloading is something you do with classes. It has nothing to do with C or the C preprocessor.[/QUOTE]
ah, i guess using macros is okay then
[QUOTE=jA_cOp;25029534]This is bullshit. I don't care if your code is hand-optimized, full of clever tricks and farts rainbows: as long as it's clear and concise. If it's easily readable it's easily fixable and that's what really matters. Most programming is maintenance programming, after all.[/QUOTE]
Because "PI" is terribly difficult to understand.
[QUOTE=jA_cOp;25029534]It [I]is[/I] bloody useful. The constant doesn't just have type safety advantages (advantages you still don't seem to understand), but it's superior to the macro for a billion other reasons too: it won't reserve the two letters "PI" in sequence whenever it appears (M_PI ring any bells?). you can take the address of the constant without a meaningless, cryptic error. All errors involving the symbol will be much better (because it's an [I]actual[/I] symbol). Uses the C grammar, not the C preprocessor grammar (for lexing tools). The symbol can actually be imported from other code modules if need be. The list goes on, it's an age-old argument.
All this for free! There are no down-sides to not using the constant over the macro.
(Same goes for C99 inline functions versus macros where applicable, but that's a different story)[/QUOTE]
This is entirely academic. In practice, I, and probably most other C programmers, have never had any trouble with macros.
[QUOTE=Jookia;25029590]do macros support operator overloading[/QUOTE]
That makes precisely zero sense.
[QUOTE=ROBO_DONUT;25029748]Because "PI" is terribly difficult to understand.[/QUOTE]
I wasn't talking about this particular case.
[QUOTE=ROBO_DONUT;25029748]This is entirely academic. In practice, I, and probably most other C programmers, have never had any trouble with macros.[/QUOTE]
I don't care how you classify it, it's still obvious that constants are clear-cut superior to preprocessor defines in applicable cases. It comes at no cost so there is no reason not to use it except for legacy reasons. The only argument I can see is "it works for me so fuck you". By all means, use macros, but if you're going to argue for macros over constants, at least come up with some real arguments.
[QUOTE=jA_cOp;25029820]I don't care how you classify it, it's still obvious that constants are clear-cut superior to preprocessor defines in applicable cases. It comes at no cost so there is no reason not to use it except for legacy reasons. The only argument I can see is "it works for me so fuck you". By all means, use macros, but if you're going to argue for macros over constants, at least come up with some real arguments.[/QUOTE]
When did I ever argue for macros over constants?
On an open source game I tried to update the version of zlib it came with but it wouldn't compile. It turns out Lugar- the open source game had an entire file full of #defines for skeletal joints for their model and cunningly had "#define head 0" in it. Well, the newest version of zlib had a variable named 'head' while the older zlib had it named '_head'. Boy oh boy was that a fun night of debugging.
[url=http://pastebin.com/raw.php?i=ZWqBXGtW]Here it is.[/url]
[QUOTE=ZeekyHBomb;25017164]I don't understand what you're trying to do.
What is going to an element?[/QUOTE]
An element is like this in xml: <pants> blah blah blah </pants>. Pants being the element.
It can have other elements in it and attributes. I want to skip to an element by a certain name (such as HP or Mana, which will store the respective values).
[QUOTE=ROBO_DONUT;25029748]
This is entirely academic. In practice, I, and probably most other C programmers, have never had any trouble with macros.[/QUOTE]
Case in point:
[QUOTE=Jookia;25029913]On an open source game I tried to update the version of zlib it came with but it wouldn't compile. It turns out Lugar- the open source game had an entire file full of #defines for skeletal joints for their model and cunningly had "#define head 0" in it. Well, the newest version of zlib had a variable named 'head' while the older zlib had it named '_head'. Boy oh boy was that a fun night of debugging.
[url=http://pastebin.com/raw.php?i=ZWqBXGtW]Here it is.[/url][/QUOTE]
[QUOTE=jA_cOp;25029534]it won't reserve the two letters "PI" in sequence whenever it appears (M_PI ring any bells?)[/QUOTE]
Actually, preprocessor options only operate on whole tokens, so a #define of PI won't affect a reference to M_PI. (And M_PI itself is a #define, btw.)
[QUOTE=jA_cOp;25030901]Case in point:[/QUOTE]
Because naming a const int "head" in the global namespace wouldn't cause problems.
This is a case of asshat programmer, not an argument against the C preprocessor.
[QUOTE=Wyzard;25031035]Actually, preprocessor options only operate on whole tokens, so a #define of PI won't affect a reference to M_PI. (And M_PI itself is a #define, btw.)[/QUOTE]
I know that, it's still a problem. My reference to M_PI is to show that the M_ prefix is used as an extra measure against the problems with macros.
[editline]12:59PM[/editline]
[QUOTE=ROBO_DONUT;25031390]Because naming a const int "head" in the global namespace wouldn't cause problems.[/QUOTE]
It would be immediately obvious what the problem was because "head" would be an actual symbol.
[QUOTE=ROBO_DONUT;25031390]This is a case of asshat programmer, not an argument against the C preprocessor.[/QUOTE]
It's a case of a newbie programmer who isn't familiar with the dangers of gratuitous, careless use of macros. If he had been taught to use constants where applicable this problem would be minor. You could also teach him proper conventions for using macros, but those are workarounds, not fixes.
[QUOTE=jA_cOp;25032004]It's a case of a newbie programmer who isn't familiar with the dangers of gratuitous, careless use of macros. If he had been taught to use constants where applicable this problem would be minor. You could also teach him proper conventions for using macros, but those are workarounds, not fixes.[/QUOTE]
The guy who wrote Lugaru isn't at his first trial of C and wouldn't be what I call a newbie programmer.
[QUOTE=pikzen;25035132]The guy who wrote Lugaru isn't at his first trial of C and wouldn't be what I call a newbie programmer.[/QUOTE]
If he's not literally a newbie, then he hasn't progressed much past the beginner stage... it's also possible that the header file posted is just very, very old.
[QUOTE=WTF Nuke;25030774]An element is like this in xml: <pants> blah blah blah </pants>. Pants being the element.
It can have other elements in it and attributes. I want to skip to an element by a certain name (such as HP or Mana, which will store the respective values).[/QUOTE]
[cpp]TiXmlElement *HPElement = myElement->FirstChildElement("HP");
if(!HPElement)
return ELEMENT_NOT_FOUND;
if(HPElement->NextSiblingElement("HP"))
warning("Duplicate HP element");
//read HPElement[/cpp]
Didn't test it. Don't know TinyXML. Looks straight forward enough though.
How can I do something like this in C#? I dunno how.
[code]
switch(someint)
case 1 or 2 or 3 or 4:
//instructions
break;
[/code]
[cpp]
switch(someint)
{
case 1:
case 2:
case 3:
case 4:
//instructions
break;
}[/cpp]
Oh, thank you.
[QUOTE=jA_cOp;25035196]If he's not literally a newbie, then he hasn't progressed much past the beginner stage... it's also possible that the header file posted is just very, very old.[/QUOTE]
I wouldn't consider this the work of a newbie programmer: [url]http://www.youtube.com/user/WolfireGames[/url]
Sorry, you need to Log In to post a reply to this thread.