GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 29-Sep-2007, 18:17
Vordican Vordican is offline
New Member
 
Join Date: Sep 2007
Posts: 3
Vordican is on a distinguished road

C++ math problem


hello all,
im trying to code what should be a pretty simple math problem as an asignment for my c++ class but it isnt working out so well.
so here is the code i have now .. as you can see its to compute area and circumference of and ellipse.

CPP / C++ / C Code:
#include<fstream.h>
#include<iomanip.h>
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
int main()

{
    double pi, a, b, area, circ;
    ofstream output ("g:result.dat");
    cout <<"name"<<endl;
    output <<"name"<<endl;
    pi=acos(-1);
    a=18.253;
    b=15.624;
    circ= 2 * pi * (pow ((pow(a, 2.0) + pow(b, 2.0)) / 2.0),0.5);
    area= pi * a * b;
    cout <<"The circumference of the ellipse is "<<circ<<endl;
    output <<"The circumference of the ellipse is "<<circ<<endl;
    cout <<"The area of the ellipse is "<<area<<endl;
    output <<"The area of the ellipse is "<<area<<endl;
    return 0;
}

when i try to compile this i get
"too few arguments to function 'double pow(double, double)"
on the circumference line.
im guessing that means the () are wrong somewhere but i cant seem to find where.
i dont really need a coded response or anything, im just trying to figure out what that error as i have gotten the same thing on another similar program im working on.

any help would be appreciated ... and its my first post so be gentle if i made a stupid mistake
  #2  
Old 29-Sep-2007, 21:31
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,244
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: c++ math problem


Break this statement
CPP / C++ / C Code:
    circ= 2 * pi * (pow ((pow(a, 2.0) + pow(b, 2.0)) / 2.0),0.5);
into sub-equations. Make each pow() a separate statement.
__________________

Age is unimportant -- except in cheese
  #3  
Old 01-Oct-2007, 07:32
Vordican Vordican is offline
New Member
 
Join Date: Sep 2007
Posts: 3
Vordican is on a distinguished road

Re: C++ math problem


ah ok ill give that a try to see how it works out. it also works when its entered like this,
CPP / C++ / C Code:
circ= 2 * pi * (sqrt ((pow(a, 2.0) + pow(b, 2.0)) / 2.0));

is that the compiler just getting confused? or is it just a better programming practice to split things like that.
  #4  
Old 01-Oct-2007, 07:55
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: C++ math problem


Quote:
is that the compiler just getting confused? or is it just a better programming practice to split things like that.
No, it's you getting confused. And I guess whatever prevent you from getting confused is a good practice for you.

CPP / C++ / C Code:
circ= 2 * pi * (pow ((pow(a, 2.0) + pow(b, 2.0)) / 2.0),0.5); // one argument
circ= 2 * pi * pow( ( (pow(a, 2.0) + pow(b, 2.0)) / 2.0), 0.5);  // two arguments

CPP / C++ / C Code:
circ= 2 * pi * (sqrt ((pow(a, 2.0) + pow(b, 2.0)) / 2.0));  // one argument
circ = 2 * pi * sqrt( ( pow(a, 2.0) + pow(b, 2.0) ) / 2.0 );  // still one argument
__________________
Music, programming, endless learning..
  #5  
Old 01-Oct-2007, 08:24
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ math problem


Quote:
Originally Posted by Vordican
is that the compiler just getting confused?
No. The compiler accurately diagnosed your problem with the original statement:

CPP / C++ / C Code:
    circ= 2 * pi * (pow ((pow(a, 2.0) + pow(b, 2.0)) / 2.0),0.5);

For purposes of human clarification, I will replace pow(a,2.0) by powa, and I will replace pow(b,2.0) by powb

CPP / C++ / C Code:
    circ= 2 * pi * (pow ((powa + powb) / 2.0),0.5);


If you still don't see the problem, then substitute powab in place of powa + powb

CPP / C++ / C Code:
    circ= 2 * pi * (pow ((powab) / 2.0),0.5);

If you still don't see it, substitute powabover2 for (powab)/2.0

CPP / C++ / C Code:
    circ= 2 * pi * (pow (powabover2),0.5);
Quote:
Originally Posted by Vordican
...is it just a better programming practice...

That's a value judgment that I will leave to you. Some people can spot errors and write correct code for very complicated expressions. I promise you, the compiler doesn't "get confused," (If it does, then demand your money back!) but people who have to maintain the code may not be as sophisticated as the people who write the code. If I have to spend more than three seconds debugging an incorrect expression, then simplify it; that's my rule of thumb.

As far as better programming practice:
I rarely call pow(x, 2.0) for simple variables. I just use x*x. Some would call that better (since multiplication is likely to be "more efficient" than calling a mathematical function). Some would call it worse (for a number of reasons).

I can't see me ever calling pow(x, 0.5); I would (probably) use sqrt(x) with the reasoning that the library function sqrt is specifically optimized for square roots, whereas the library function pow works as a square root when its second argument is 0.5, but may not be as "efficient".

Note that optimizing compilers and optimized library functions may very well take care of special cases and create code more "efficient" that you could dream of.

Bottom line: if it makes sense to you (and if it works), then that's OK. If you, the person writing the code, have problem parsing a complicated expression, consider the difficulty of maintaining the code, and take whatever steps you deem appropriate.

On the other hand, I do have some other suggestions that I consider "good programming practice"

1. Use standard C++ headers. The deprecated headers that you use may not be supported in future revisions of C++ compilers.

2. Always test state of streams that your program attempts to open.

3. (More a matter of personal taste.) Use constants instead of calling library functions to calculate constants at run time. The value of pi is #defined as M_PI in some <math.h> headers, but not all of them have it. I think that best programming practice is probably to use the value in the program. (I usually just use M_PI, for my own code, since I know that my <math.h> has it #defined to be the value of pi to a precision that is at least as good as the double variables, but if I am going to ship the code to someone else, I just paste the value into the program.) Note that, traditionally, programmers typically use all caps for manifest constants.

So, the first part of my program might look like

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>

// Maybe I would just use M_PI here
const double PI = 3.14159265358979323846;

using namespace std;

int main()
{
    double a, b, area, circ;
    char * outname = "result.dat";

    ofstream output(outname);
    if (!output) {
        cout << "Can't open file " << outname << " for writing." << endl;
        exit(EXIT_FAILURE);
    }
    cout << "Opened " << outname << " for writing." << endl;

    a = 18.253;
    b = 15.624;

    circ = 2 * PI * sqrt((a*a + b*b) / 2.0);
    area = PI * a * b;
//  etc.


There are other possible styles that might considered to be "good programming practice". I invite comments.

Regards,

Dave

Footnote: Some people always print error messages to cerr rather than cout to take into consideration the possibility that standard output may be redirected to a file or piped to another program.
  #6  
Old 02-Oct-2007, 12:25
Vordican Vordican is offline
New Member
 
Join Date: Sep 2007
Posts: 3
Vordican is on a distinguished road

Re: C++ math problem


Great responses, thx for the help folks
- ill keep all those suggestions in mind for sure.... as for now im still at the stage where its the professor saying do it this way... which accounts for the pi=acos-1 and some of the other stuff...
but again, thx for the help
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 2) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Math problem: Total Noob Targsmom C Programming Language 3 20-Oct-2006 05:05
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:15.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.