hey everyone, i new to c++ programming. i have a program due soon in vector addition.
i have done some work that I've shared below. the program that i am to design should be able to add vectors with certain magnitude and direction and give its resultant magnitude and direction (i know how to do this mathematically but programming is not working. the user should be able to select how many vector he/she wants (i don't know how to do this, so i added 3 vectors). here's my work - i did this purely algebraically. it's still incomplete but it compiles. my "if else" statement also doesn't respond correctly.
any help will be greatly appreciated.
#include <iostream>
#include <string>
#include <vector>
#define PI 3.14159
#include <math.h>
std::vector<int> v; // declares a vector of integers
using namespace std;
int main ()
{
float i, i1, i2;
float x, x1, x2;
float result;
cout << "enter the 1st magnitude ";
cin >> i;
cout << endl;
cout << "enter the 2nd magnitude ";
cin >> i1;
cout << endl;
cout << "enter the 3rd magnitude ";
cin >> i2;
cout << endl;
#include <iostream>
#include <cmath>
usingnamespace std;
class Vector2d
{
float x, y;
public:
Vector2d() : x(0), y(0) {}
Vector2d(float mag, float ang) { setMagAng(mag, ang); }
void setMagAng(float in_magnitude, float in_angle)
{
x = in_magnitude * cos(in_angle);
y = in_magnitude * sin(in_angle);
}
float getMag() { return sqrt(x*x + y*y); }
float getAng() { return atan2(y ,x); }
voidoperator+= (Vector2d& rhs)
{
this->x += rhs.x;
this->y += rhs.y;
}
};
int main()
{
Vector2d output;
int num_to_add;
cout << "How many vectors do you want to add?";
cin >> num_to_add;
for (int i = 0; i < num_to_add; ++i)
{
float mag, ang;
cout << "Vector" << i << ": What's the magnitude?";
cin >> mag;
cout << "What's the angle?";
cin >> ang;
Vector2d temp(mag, ang);
output += temp;
}
cout << "Sum of magnitude is: " << output.getMag() << endl;
cout << "Sum of angles is: " << output.getAng() << endl;
}
I'll look through your code and make some changes, though I already see some issues: else (((x, x1, x2) < 90), ((mx, x1, x2) > 270)); {
first off, else ifs look like: elseif (condition) {, not else (condition); {
second: ((x, x1, x2)< 90),. I'm not entirely sure what you're doing here. WHat is your intention. I'm guessing you mean to do something like: (x < 90 || x1 < 90 || x2 < 90) &&
But you really don't want to do that either because though the syntax is correct, the three angles may be in different quadrants and the signs may change for each one. Right now you are batching them all togeather.
wow... just wow...
i spend a day trying to figure this out... and you helped me in the fraction of that time..
thank you stewbond...thank you very much!!!
The fact that you spent an entire day on this is good. No pain, no gain. It meant that you are really looking deep into it, running into dead-ends, and learning what doesn't work. That's essential for learning. As you figure out what doesn't work, you'll be more familiar with way of making it really work.