Before I deal with your specific question, let me clear up a few things in your code sample:
1) You don't need to include both <cmath> and <math.h>. They contain the same information, but <cmath> is just edited to put everything in the "std::" namespace.
2) You don't need to define PI yourself. It is defined as M_PI in <cmath>.
3) The "^" operator does not perform a power operation in C++. You can do one of two things:
- Use the <cmath> library function pow(double x, double y), but that returns a double, and may result in the wrong value when you convert back to (int) due to floating-point conversions.
- Use your own function for integer powers, such as:
1 2 3 4 5 6 7 8 9 10
|
int ipow(int base, int power) {
if (power == 0) {
return 1;
}
int result = base;
for (int i=1; i<power; i++) {
result *= base;
}
return result;
}
|
4) When you create an array on the stack, the dimensions must be constant. You would have to declare h and w as 'const' when you create and initialize them.
4a) I see what you're trying to do - allocate an array with a maximum size ahead of time, and then allow the user to enter the dimensions they are using, which is presumably smaller - but there's a better and safer way. Use the new[] operator to create the array dynamically on the heap:
1 2 3 4
|
float **array = new float*[h]; //Creates 2D array with h rows
for (int i=0; i<h; i++) {
array[i] = new float[w]; //Creates each row with w columns
}
|
Now you can use the array just as you would have before, but it's only the size you need. Make sure you use delete[] to free the memory later:
1 2 3 4
|
for (int i=0; i<h; i++) {
delete[] array[i]; //Free each row first
}
delete[] array; //Free entire 2D array
|
5) In your for loops, instead of using "p<=h-1", use "p<h". One less calculation to perform.
6) Since the only difference between your for loops is the last two lines, you should avoid re-using all those identical statements. Use something like this instead:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
for(int p=0; p<h; p++)
{
R = array[p][2] + R;
IJ = array[p][1] * array[p][2];
KJ = array[p][3] * array[p][2];
x = IJ / R;
z = KJ / R;
Moi = IJ + Moi;
Mok = KJ + Mok;
if (theta <= 360) {
MO = IJ * sin(theta * PI/180) + KJ * sin(theta * PI/180);
MOL = array[p][1] * cos(theta * PI/180) + array[p][3] * cos(theta * PI/180);
}
}
|
Now... when you want to pass an array to a function, you'll do something like this:
1 2 3 4 5 6 7 8 9
|
//Function definition
float magnitude(float **array, int h, int w) {
//Here, you can access the elements of 'array' just as you normally would
//With C++, you generally would pass a multi-dimensional vector for this purpose, instead
//of worrying about passing the dimensions, but that may be beyond the scope of what
//you're doing right now.
}
//Function call
some_value = magnitude(array,h,w);
|
If you don't want the function to be able to change the values in the array, use
const float **array
instead.
Edit:
Additionally, if the size of an array is guaranteed to be constant, a function can receive the array as follows:
void someFunction(float array[10][5])
or
void someFunction(float array[][5])
(the compiler already knows the first dimension anyway)