passing an array and then some

Greetings and salutations. For an assignment I have to pass a multi-dimensional array around three functions using global variables that calculates for magnitude and resultant in 3d space. I got most of the calculations and functions done, I'm just having problem with the passing of the multi array. Any advice?

For now,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//A program that calculates the equivalent force couple system,
//the magnitude and the point of application in the xz plane
#include<iostream>
#include<fstream>
#include<cmath>
#include <math.h>
//defining PI to be used anywhere rather typing numbers
#define PI 3.14159265

using namespace std;

int main()
{
    //initialing multi-dimensional array to take into account of multiple vectors
    int h = (2^9)-1;
    int w = 5;
    //this array can handle any numbers in a 32-bit system
    float array [h][w];
    float forc, i, j, k, theta;
    int s;
    //the beginning of acquiring information
    cout<<"Enter the number of position vectors "<<endl;
    cin>>s;
    h=s;
    // a for-loop to enquire about all the vectors
    for(int a=0; a<=h-1; a++)
     {
             cout<<"Enter the force of vector "; 
             cin>>forc;
             //Each number entered is assigned to a place in the array
             array[a][0]=forc;
             cout<<"Enter the point i "; 
             cin>>i;
             array[a][1]=i;
             cout<<"Enter the point j "; 
             cin>>j;
             array[a][2]=j;
             cout<<"Enter the point k "; 
             cin>>k;
             array[a][3]=k;
             cout<<"Enter the degree of the vector, if there are none enter 999:    ";
             cin>>theta;
             array[a][4]=theta;
     }                
   
    float IJ = 0;     //the "j" part of an vector being mulitipied to its "i"
    float KJ = 0;     //the "j" part of an vector being mulitipied to its "k"
    float R = 0;      //the sum of all the "j's" of entered vectors
    float z = 0;
    float x = 0;
    float Moi = 0;    //the moment about in reference to i
    float Mok = 0;    //the moment about in reference to k
    float MO = 0;     //the moment about
    float MOL = 0;
    if(theta<=360)    //conditional statement if no degrees are to be entered or will be
    for(int p=0; p<=h-1; p++)
        {
             R = array[p][2] + R;                //summing all the "j's" of entered vectors
             IJ = array[p][1] * array[p][2];     //the "j" part of an vector being mulitipied to its "i"
             KJ = array[p][3] * array[p][2];     //the "j" part of an vector being mulitipied to its "k"
             x = IJ / R;                         //calculating the coordinates for the xz-plane, the magnitude in the x direction
             z = KJ / R;                         //calculating the coordinates for the xz-plane, the magnitude in the z direction
             Moi = IJ + Moi;                     //the "i" portion of moment about
             Mok = KJ + Mok;                     //the  "j" portion of moment about
             MO = IJ * sin(theta * PI/180) + KJ * sin(theta * PI/180);         //trig functions and degrees multiplied to the moment
             MOL = array[p][1] * cos(theta * PI/180) + array[p][3] * cos(theta * PI/180);        //the moment about length wise concerning each vector
        }
    else              //if no degrees are entered
    for(int p=0; p<=h-1; 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;
             }
    //showing the results
    cout<<"The magnitude is "<<R<<endl;
    cout<<"The point of application   "<<IJ<<"   "<<KJ<<endl;
    cout<<"The equivalent force is "<<MO<<"   "<<MOL<<endl;
    system("PAUSE");
    return 0;
}
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)
Last edited on
thanks for help it worked out
Topic archived. No new replies allowed.