Finding Three Dimensional Force Vector

I was looking for a way to a find three dimensional force vector. I currently in high school taking physics and thought is would be cool if I created a program that would be able to do this. The only bad part is that I'm not sure how to begin. I'm a beginner in c++ so I want to learn how it is done.

Thanks <3
Can you do it on paper?
If you want to make a vector, not an array, then you declare it like:
vector <variable type> name

Obviously you choose what the name and type is.

Example: vector <int> myArray;

To declare an array, you write:
variable type name [how many sections you want];

Example: int myArray[5];

You can access both my using the [] accessor.

Example: myArray[x];

To make the array multi-dimensional, when you declare it write int myArray[5][5][5];
That creates an array with 125 members: [0][0][0], [0][0][1], [0][0][2]... ...[4][4][3], [4][4][4].

Multidimensional vectors are a tad more confusing. To declare one:
vector <vector <vector <int> > > myArray

This makes a vector, where each section is a vector, where each section is a vector, where each section is an integer, or whole number. This is accessed just like the multi-dimensional array.

I'm not sure if this is what you wanted, but that's what I understand 3-dimensional vectors/arrays to be.

Sorry if this wasn't what you needed,
hnefatl
Thank you for your help.

@Moschops yes I able to solve the the problems by onn paper. If needed I can provide an example.

Thank you <3
I'm assuming you mean that you'll enter the force components in three separate dimensions (let's call them x, y and z) and you want to write a C++ program to calculate the single resultant force's magnitude and direction?
Yes. Do you have advice or resources in which I could attempt to do this?
maybe start by doing a program that finds a 2D vector first? That would be simpler and then you could add on that.
If you can do it on paper using no more than common trig functions, turning it into C++ code should be easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
  double xForce, yForce, zForce;
  cout << "Enter x force" << endl;
  cin >> xForce;
  cout << "Enter y force" << endl;
  cin >> yForce;
  cout << "Enter z force" << endl;
  cin >> zForce;

  // Whatever you do on paper

  cout << "Resultant is " << resultantForce << " in direction " << resultantDirection;
}
I made a little program which uses the polar coordinates of two 2D vectors to calculate the polar coordinates of a third 2D vector.
polar coordinates are really a LOT better for calculating 2D vectors than Cartesian xy coordinates, because for those you need to know how to add, subtract, multiply, and divide complex numbers(a + bi), while for polar coordinates all you need to know is how to add, subtract, multiply and divide real numbers and how to find the average of angles(easy).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	double angle1 = 0;
	cout<<"Vector->1 angle(degrees): ";
	cin>>angle1;
	double distance1 = 0;
	cout<<"Vector->1 distance(arbitrary units): ";
	cin>>distance1;
	double angle2 = 0;
	cout<<"\nVector->2 angle(degrees): ";
	cin>>angle2;
	double distance2 = 0;
	cout<<"Vector->2 angle(degrees): ";
	cin>>distance2;
	double angle3 = (angle1 + angle2)/2;
	double distance3 = distance1 + distance2;
	cout<<"\nV->1 + V->2 angle = "<<angle3<<" degrees"<<endl;
	cout<<"V->1 + V->2 distance = "<<distance3<< " arbitrary units"<<endl;
	system("pause");
	return 0;
}


hope this helps!
Last edited on
Topic archived. No new replies allowed.