math functions

This is a discussion on math functions within the C++ Programming forums, part of the General Programming Boards category; This is probably a really simple question, but is there a way in a program to have it calculate say ...

  1. #1
    Unregistered
    Guest

    math functions

    This is probably a really simple question, but is there a way in a program to have it calculate say the log of an inputted number and if so, how?

  2. #2
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Create a dynamic array where you store the numbers that get input.

    I don't know how to dynamically assert dimensions. So I can only give you an example when you get to know at runtime the number of inputs you will get...

    Considering only integer numbers are allowed:

    Code:
    int* pnumbers = 0;        //init pointer to array
    int max = 0;                   //init number of inputs
    
    cout << "How many numbers will you input?";
    cin >> max;                   //and you now know the dimensions for the array
    
    if(!(pnumbers = new int[max])) //Create the array dynamically
    {
        cout << "Out of memory!";
        exit(1);   //if there is no memory left, abort
    }
    
    for(int i=0; i<max;i++)
    {
        cout << "Insert a number" << endl;
        cin >> *(pnumbers+i)  //this adds to the array the number introduced
    }
    
    //... do something with the array
    
    delete [] pnumbers;    //Free up the memory. IMPORTANT!!
    pbnumbers = NULL;
    Another option, to avoid you the trouble of dynamically creating the array. Would be, of course to create some array with 1000 dimensions and hope the user gets bored after introducing seven hundred or so numbers
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    66
    Try log functions found in math.h

    Code:
    double log( double x );
    float logf( float x );
    double log10( double x );
    float log10f ( float x );

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    He! *smack me*
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362

    #include <cmath>

    The <cmath> header file contains a number of the more complex math functions that you may be looking for.

    Specifically, log(x) and log10(x) are included in this group assuming you are looking for either the natural log (base e) or common log (base 10).

    Simply pass the input number as an argument to the function:
    Code:
    double x;
    double y;
    :
    :
    y = log(x);
    Arguments and return values are, technically, double-precision floating-point numbers, though single-precision values are acceptable.

    You'll need to do some "homework" to see what the file has available and what functions are available to meet your needs.



    (Mario,

    Keep wrestling with your three-year-old! You need a break, my friend. No biting!! P.S. Re-read the question! )
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  6. #6
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Really, i'm going crazy here. Almost finishing what the writter calls C++ basics. Hah! funny guy. Right now i'm dealing with polymorphism and inheritance and some more crazy pointers...

    I'm telling you; If I get past this with my mind still in place, it will for sure attack some other part of my body. Wonder if C++ programmers invented viagra...
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Viagra doesn't work on C++ programmers, Mario. We keep thinking that we've found an important solution rather than recognizing what's really going on!

    (Occupational hazard.)

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-26-2009, 12:21 AM
  2. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 08:22 PM
  3. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 07:39 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21