# Thread: math functions

1. ## 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. 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

3. 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. He! *smack me*

5. ## #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! )

6. 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...

7. 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.)

Popular pages Recent additions