to take array elements from function

I need some help to take array values from function:

using namespace std;

int i;
double translational_velocity(double x[100],double y[100], int t);

int main()
{ int SCAN_NUMBER;
int obj_num;
cin>>obj_num;
for(int n = 0; n < obj_num; n++)
{ cin>>SCAN_NUMBER;
int t = SCAN_NUMBER;
int x[t];
int y[t];
for(i =0; i < t; i++)
{ cout<<"x = ";cin>>x[i];
cout<<"y = ";cin>>y[i];
}
for(i = 0; i < t; i++)
if(x[0] == x[i] && y[0] == y[i])
cout<<"g";//VFH(x,y);
else
{ v[i] = translational_velocity(x, y, t); /* problem is here, how to get values */

}}
cin.ignore();
getch();
return 0;
}

double translational_velocity(double x[],double y[], int t)
{
cout<<"translational velocity : ";
double v0[t];
double v[t];
for(i = 0; i < t; i++)
{
v0[i] = sqrt((x[i]-x[i-1])*(x[i]-x[i-1]) + (y[i]-y[i-1])*(y[i]-y[i-1])) / t;
}
for(i = 1; i < t; i++)
{ v[i] = abs(v0[i]); }
return v[i];
}

Last edited on
Array's are constant pointers. That means that you can pass an array to a function, initialize the elements there, and then use those values in the main-loop:

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
#include <iostream>

using namespace std;

//function to set the values of an array
void setValues(int array[], int size)
{
    //for-loop to go through all elements
    for (int i=0;i<size;i++)
    {
        //store information into the elements
        array[i]=i*2;
    }
}

int main()
{
    //declare array and size
    const int SIZE = 5;
    int myArray[SIZE];
    
    //set values
    setValues(myArray,SIZE);
    
    //print values to the screen
    for (int i=0;i<SIZE;i++)
    {
        cout<<myArray[i]<<endl;
    }
    
    //whait before ending the program...
    cin.ignore();
    
    //quit the program
    return 0;
}
Topic archived. No new replies allowed.