Implementin this function into my program

Hey guys I have been having trouble implementing this New_Position function into my code, that I have already made. We are supposed to use the New_Position function and not make up our own, like I did :/.

New_Position Function:
1
2
3
4
5
6
7
//This function calculates the new position of an object. The old
//position, velocity, and time are passed to it.

float New_Position (float Old_Position, float v, float t)
{
return Old_Position + v*t;
}

Here is my original code without the function:
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
//Vector Displacement Between Frames 

#include <iostream>
#include <cmath>

using std::cout;
using std::cin;
using std::endl;

void g(float [], float [], float);

int main()
{
	float frame , t = 1.5, time, i[] = {0,0}, a[] = {0,0};

	//user input
	cout << "Enter in intial X pixel point: "<<endl;
	cin >> i[0];
	cout << "Enter in intial Y pixel point: "<<endl;
	cin >> i[1];
	cout << "Enter in acceleration for the X vector: "<<endl; //pixels / sec^2
	cin >> a[0];
	cout << "Enter in acceleration for the Y vector: "<<endl; //pixels / sec^2
	cin >> a[1];
	cout<<endl<<endl;
	cout << "Intial position (x,y) " << "(" << i[0] << "," << i[1] << ")"<<endl; //This is the intial position at frame 0.

	for(frame = 1; (time = frame/30) <= t; frame++)
	{
		g(i,a,time); //calls the function g
		
		//output for the program
		cout << endl << "Frame: " << frame << " The point is now at: " << "(" << i[0] << "," << i[1] << ")"  << endl;
	}

	return 0;
}
void g(float i[], float a[], float t)//this function calculates the vector displacement using accel and time.
{
	//Goes back to lab 3
	int n = 2; //n must be equal to 2 so arrays work
	for (int k=0; k <= n-1; k++)
		{
			i[k] = i[k] + a[k]*t; //formula that finds where the new point is from old point given accel and time.
		}
}


And here it is modded with the New_Position:
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
#include <iostream>
#include <cmath>

using std::cin;
using std::endl;
using std::cout;

float New_Position(float[],float[],float);

int main()
{
	float time, frame, t = 1.5, i[] = {0,0}, a[] = {0,0},new_Position[] = {0,0};	

	//user input
	cout << "Enter initial Pixel Point for the X coordinate: "; // intial Pixel Point for X coordinate
	cin >> 	i[0];
	cout << "Enter initial Pixel Point for the Y coordinate: "; // intial Pixel Point for Y coordinate
	cin >> 	i[1];
	cout << "Enter Acceleration for the X vector: "; // Acceleration for X Vector (pixels / sec^2)
	cin >> a[0];
	cout << "Enter Acceleration for Y vector: "; // Acceleration for Y vector (pixels / sec^2)
	cin >> a[1];
	cout << endl << endl;
	cout << "Initial point (x,y): " << "(" << i[0]<< "," << i[1]<< ")" << endl << endl; //dsplays the initial point at frame 0
	
	//for loop to cycle throug 45 frames while showing time in seconds.
	for ( frame=1; (time = frame/30) <= t; frame++)
	{	
		new_Position[] = New_Position(i,a,time); //calls function g, which does the calculation for new point
		
		//output for the program
		cout << endl << "Frame: " << frame << " The point is now at: " << "(" << new_Position[0] << "," << new_Position[1] << ")"  << endl;
		//if we wanted time too:
		//cout<<"it took: "<<time<<" secs"<<endl;
	}
	return 0;
}

//function based on final point being calculated by accumilating 
//the initial point and adding acceleration*time.
float New_Position(float i[], float a[], float t)
{
	int n=2; // going back to lab 3, n must = 2 for arrays to work properly
	//float newPosition = 0;

	for (int k=0; k <= n-1; k++)
		{
			 i[k] = i[k] + a[k]*t;//formula that finds where the new point is from old point given accel and time.
		}
	return  i[];//newPosition;
}
/*static float New_Position (float Old_Position, float v, float t)
{
	return Old_Position + v*t ;
}*/


when I compile it I get the following errors:
1
2
syntax error : ']'   //this in on the line new_Position[] = New_Position(i[],a[],time);
syntax error : ']'  //this is on the return value in the New_Position function 


I can't figure out how to implement this function using parallel arrays as in my original code. Please help me out :D
Last edited on
Your variable k only has scope within the loop
1
2
for (int k=0; k <= n-1; k++)
{}
just declare the variable at the top of the function next to int n
awsome that worked but im still getting the
1
2
 
syntax error : ']'   //this in on the line new_Position[] = New_Position(i[],a[],time); 
Which line is the error on? I think that in your
1
2
3
4
5
New_Position(..)
{
    ...
    return i[]; //you need to return a particular element of i rather than the whole array
}


Last edited on
Well I did some screwy stuff last night and I got it to compile but my output for every frame was (0,0).

here is the updated code:
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
#include <iostream>
#include <cmath>

using std::cin;
using std::endl;
using std::cout;

float New_Position(float[],float[],float);

int main()
{
	float time, frame, t = 1.5, i[] = {0,0}, a[] = {0,0},new_Position[] = {0,0};	

	//user input
	cout << "Enter initial Pixel Point for the X coordinate: "; // intial Pixel Point for X coordinate
	cin >> 	i[0];
	cout << "Enter initial Pixel Point for the Y coordinate: "; // intial Pixel Point for Y coordinate
	cin >> 	i[1];
	cout << "Enter Acceleration for the X vector: "; // Acceleration for X Vector (pixels / sec^2)
	cin >> a[0];
	cout << "Enter Acceleration for Y vector: "; // Acceleration for Y vector (pixels / sec^2)
	cin >> a[1];
	cout << endl << endl;
	cout << "Initial point (x,y): " << "(" << i[0]<< "," << i[1]<< ")" << endl << endl; //dsplays the initial point at frame 0
	
	//for loop to cycle throug 45 frames while showing time in seconds.
	for ( frame=1; (time = frame/30) <= t; frame++)
	{	
		new_Position[2] = New_Position(i,a,time); //calls function g, which does the calculation for new point
		
		//output for the program
		cout << endl << "Frame: " << frame << " The point is now at: " << "(" << new_Position[0] << "," << new_Position[1] << ")"  << endl;
		//if we wanted time too:
		//cout<<"it took: "<<time<<" secs"<<endl;
	}
	return 0;
}

//function based on final point being calculated by accumilating 
//the initial point and adding acceleration*time.
float New_Position(float i[], float a[], float t)
{
	int n = 2; // going back to lab 3, n must = 2 for arrays to work properly
	int k = 0;
	//float newPosition = 0;

	//for (int k=0; k <= n-1; k++)
		//{
			 i[k] = i[k] + a[k]*t;//formula that finds where the new point is from old point given accel and time.
								  //New_Position = Old_Position + acceleration * time
		//}
	return i[2];
}
/*static float New_Position (float Old_Position, float v, float t)
{
	return Old_Position + v*t ;
}*/


I put [2] my return value (return i[2]) and in (new_Position[2] = New_Position(i,a,time);)
that might be whats causing the (0,0) but idk.
bump
Topic archived. No new replies allowed.