i need help

Pages: 12
im writing a program that is form one my physic problem and wanted to do some more to it

i want to do a vector product for these in my program but every time i did it
it would only give me a positive,but not the negative
j*i=-k
k*j=-i
i*k=-j
and sorry for the sloppyness
here my program
any help is welcome and thank you
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
const  double basePower =10;
const double electron = -1.6*pow(10,-19);
const double proton =1.6*pow(10,-19);
double scalarProduct(vector<double> velocity, vector<double> magneticFeild);
int main()
{

    double velocityOne;
    double magneticFeildMagnitude;
    double velocityOneTimePower;
    char letterOne,letterTwo, letterThree, letterFour, letterFive,letterSix;
    double power;
    double answer;
    char cont;
    vector<double> veca(1);
    vector<double> vecb(1);
    //#1
    cout << "This problem is without deflection,so Fnet = FB + FE=0.";
    cout << "\nYou need to find the electric field and the vector. ";
    cout << "\nremember FB is the vector of F";
    cout << "\nwhich is equal to the q to the charge which is either the electron or proton.";
    cout << "and your V is a vector and B is also a vector ";
    cout << "\nEnter any letter or symbol to continue: ";
    cin >> cont;
    cout << endl;
    cout << endl;
    cout << "please enter the velocity of the electron or proton going in the direction : " << endl;
    cin >> velocityOne;
    cout << endl;
    cout << "please enter the power that you want to use for the velocity, for example 10 to 2 power: ";
    cin >> power;
    cout << endl;
    cout << "please enter magnitude of magnetic field that into the board :";
    cin >> magneticFeildMagnitude;
    cout << endl;
    cout <<"please enter the letter that correspond with the problem in unit vector notation using i,j,k in the direction you wish to make them: ";
    cin >> letterOne;
    cout << endl;
    cout <<"please enter the second letter that correspond with the problem in unit vector notation using i,j,k in the direction you wish to make them: ";
    cin >> letterTwo;





    switch(letterOne*letterTwo)
    {
          case 'i'*'j':
          letterThree = 'k';
          break;

          case 'j'*'k':
          letterThree = 'i';
          break;

          case 'k'*'i':
          letterThree ='j';
          break;

         default:
            break;
    }
   


    if(power > 0)
    {
        velocityOneTimePower = velocityOne*pow(basePower,power);

    }
    else if (power == 0)
    {
            velocityOneTimePower =velocityOne;

    }

    veca[0] = velocityOneTimePower;
    vecb[0] =magneticFeildMagnitude;

    answer =scalarProduct(veca,vecb) *electron;
    cout << endl;
    cout << endl;
    cout<<"This give you the solution for FB=q*VxB: "<< answer << "N" << letterThree <<endl;
    cout << "\nTo find the electric field with take the electron or proton and divide it by the answer form FB";
    cout << "and the electric field is: " << answer/electron << "n/c" << letterThree << endl;
    
    //problem 2
    cout << endl;
    cout << " Enter two vector point in unit vector notation"
    cout << letterFive << letterSix
    cout << endl;
    
    switch(letterFive*letterSix)
    {
          case 'i'*'j':
          letterFour = 'k';
          break;

          case 'j'*'k':
          letterFour = 'i';
          break;

          case 'k'*'i':
          letterFour ='j';
          break;

         default:
            break;
    }


    return 0;
}
double scalarProduct(vector<double> velocity, vector<double> magneticFeild )
{

    double product = 0;

    if(velocity.size()!=magneticFeild.size()){
        cout << "Vectors are not of the same size and hence the scalar product cannot be calculated" << endl;
        return -1;
    }


    for (int i = 0; i < velocity.size(); i++)
    {
        product = product + velocity[i]*magneticFeild[i];
    }


    return product;
}
Last edited on
Hi,

With this:

1
2
const double electron = -1.6*pow(10,-19);
const double proton =1.6*pow(10,-19);


Doubles can already do exponents:

const double electron = -1.6e-19;

Are you sure you do this calculation with a scalar product, I was just looking at the wiki page, it talks about cross product? And you have : FB=q*VxB: which is notation that has cross product . It looks like you are doing scalar product of q and V, but the equation is scalar( q,(V cross B) ) , cross product isn't distributable like that, and it isn't reversible with division

Also not seeing where a 3 dimensional vector is being initialised, only a power and magnitude. The power isn't necessary as already mentioned. You need 3 values to do vector math.

Interesting use of a switch, the trouble is that it could overflow the largest value a char can hold, probably producing a negative number. Could use const int values though.

Hope all this helps :+)
ok so the problem dealing with fb=q*V cross B is only dealing with line 22 to line 90 that was a problem where u are only looking for the electric field and along with the scalar product notation which it cout to a 'j', but i want to do another example using cross product and also use the scalar product where u can ask for to input like for example if k*i should equal 'j' or i*k equal '-j' so im kinda suck is there any type of way i could use a switch statement to output a '-j'
Ok, I kind of understand the first part - they are all in the same direction. The confusing bit was that it looked like vector math, with asking for components and the notation in the comments. If it is all in the same direction, then you could have used plain double variables.

Any way with the second part, I don't understand why you want to do this:
.... where u can ask for to input like for example if k*i should equal 'j' or i*k equal '-j' .....


It is because I have probably forgotten what the significance of that is, long time since I did any physics. Perhaps it would make more sense to me if i,j,k were vectors (not components of them), because if k cross i = j, then i cross k = -j .Can you post the exact text of the assignment?

Can you write a function that does cross product, and one that does scalar product? Your existing scalar product function isn't a scalar product because it should return a 3 dimensional vector. You actually have a dot product function.
closed account (D80DSL3A)
Your abuse of the switch structure is ingenious, but it doesn't consider argument order.
switch based on 1st direction, then handle cases.
This seems to work:
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
#include <iostream>
using std::cout;

void crossProd( char A, char B )// displays direction
{
    if( A == B )
    {
        cout << "vectors are parallel\n";
        return;// we are done here
    }

    switch( A )
    {
        case 'i':
            if( B == 'j' ) cout << 'k';
            else if( B == 'k' ) cout << "-j";
            break;
        case 'j':
            if( B == 'i' ) cout << "-k";
            else if( B == 'k' ) cout << 'i';
            break;
        case 'k':
            if( B == 'i' ) cout << 'j';
            else if( B == 'j' ) cout << "-i";
            break;
    }
    cout << '\n';
}

int main()
{
    // user input
    char v1 = 'j', v2 = 'i';
    cout << "Give 2 directions (i,j,k): ";
    std::cin >> v1 >> v2;

    // output
    cout << v1 << 'x' << v2 << " = ";
    crossProd( v1, v2 );

    return 0;
}


However, if you're going to do anything serious with math vectors in c++ I would devise a class to model them. They could be used with any defined operator overloads so that coding looks almost like written vector expressions. Eg:
1
2
3
4
5
6
7
8
double q = 0.5;// in Coulombs
// vec3d is our math vector class
vec3d v =(2e7,0,0);// in m/s
vec3d E(1.0, 2.0, -0.5);// electric field
vec3d B(0.0, 0.0, 1.0e-4);// magnetic field

// calculate Lorentz force on q
cout << "Force on q = " << q*( E + v.cross(B) ) << " Newtons\n";

edit: velocity = v is a vector, not a scalar.
Last edited on
thank you so much for the dot product ,but i dont understand what u mean by showing the second code do u mean create class and operator overload and if so should i drop what i have in my main for f=q*vxb
closed account (D80DSL3A)
You're welcome. Your approach to the problem is ok if only vectors with 1 non-zero component are used. For a more general solution involving vectors with arbitrary components I suggest developing a vector class. Vector classes by others can surely be found as well.
To start your own you could include just those features currently needed.

construct
add
subtract
dot product
cross product
multiply by scalar - as left or right operand

ie. minimally:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class vec3d
{
    public:
        double x, y, z;// the components

        vec3d( double X, double Y, double Z );// construct

        vec3d operator*( double k )const;// vec3d*scalar
        vec3d operator+( vec3d v )const;// vec3d + vec3d
        vec3d operator-( vec3d v )const;// vec3d - vec3d

        double length()const;// find magnitude
        double dot( const vec3d& v )const;// inner product
        vec3d cross( const vec3d& v )const;// outer product
};

// non-member multiplication function. Req'd as scalar is left operand
vec3d operator*( double k, vec3d v );// scalar*vec3d 
how would i do something like that i know i would have to make a class but how many function would need to be in my class

1
2
3
vec3d v =(2e7,0,0);// in m/s
vec3d E(1.0, 2.0, -0.5);// electric field
vec3d B(0.0, 0.0, 1.0e-4);// magnetic field 
closed account (D80DSL3A)
The code in my last post lists those functions, including full prototypes for each one.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "vector3D.h"


using namespace std;

int main()
{
    vector3D p1(5,-6,0);
    vector3D p2(1,2,3);
    vector3D total =crossProduct(p1,p2);
    total.print();
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef VECTOR3D_H
#define VECTOR3D_H


class vector3D
{
    public:
        int x,y,z;
        vector3D(int &xIn, int &yIn, int &zIn );
        void print();



};


1
2
3
4
5
6
7
8
9
10
11
12
13
#include "vector3D.h"
#include <iostream>
using namespace std;
vector3D::vector3D(int &xIn, int &yIn, int &zIn)
{
    x = xIn;
    y=yIn;
    z=zIn;
}
void vector3D::print()
{
    cout << x << "," << y << "," << z << endl;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef PHYSICCCC_H_INCLUDED
#define PHYSICCCC_H_INCLUDED
#include "vector3D.h"
#include <iostream>
#include <math.h>
vector3D::crossProduct(vector3D a,vector3D b);
{
    vector3D x(0,0,0);
    x.x =(a.y * b.z)-(a.z*b.y);
    x.y = (a.z*b.x)-(a.x *b.z);
    x.z =(a.x*b.y)-(a.y*b.x);
    return x;
}

#endif // PHYSICCCC_H_INCLUDED 


this the error
'vector3D::vector3D(int, int, int)'

i try to fix it still nothing just want to test it but nothing happening
closed account (D80DSL3A)
Your constructor takes 3 int& = reference to type int as arguments.
You cannot pass a literal value.
vector3D p1(5,-6,0);// error: references expected
Change your constructor from:
vector3D(int &xIn, int &yIn, int &zIn );
to
vector3D(int xIn, int yIn, int zIn );
Don't forget to make this change at the definition too.

You also need a prototype for that CrossProduct function in vector3D.h
Do put the definition for CrossProduct with the other vector3D class function definitions.
Why is it shown in a separate file?

It looks like you have a good start! Nice job!
Last edited on
now im getting an error with
vector3D total =crossProduct(p1,p2);

error
'crossProduct' was not declared in this scope
closed account (D80DSL3A)
CrossProduct is a member function. It must be called by a class instance.
In this case, either p1 or p2, like this:
vector3D total = p1.crossProduct(p2);// usage in main()

I'm sorry I didn't see this last time, but you wrote CrossProduct as if it were a regular function, not a member function, by passing it 2 vector3D arguments. The left operand in the cross product is the vector3D object calling the function.
Changes, then:
in vector3D.h
vector3D crossProduct( vector3D b)const;// calling object not changed
in vector3D.cpp
1
2
3
4
5
6
7
8
vector3D vector3D::crossProduct( vector3D b)const
{
    vector3D x(0,0,0);
    x.x =(y * b.z)-(z*b.y);// no 'a' vector3D
    x.y = (z*b.x)-(x *b.z);// x,y,z are the calling object (left operand) data members
    x.z =(x*b.y)-(y*b.x);
    return x;
}


edit: A tip. Do you find the code harder to read without the a. in front of the x, y and z for the calling vector?
Sometimes I find this is the case - a visual symmetry is broken.
Declare a reference to the calling vector (*this) - have you heard of the 'this' pointer yet?
It points to the object calling the function. Since it's a pointer you must * it to get the object pointed to.
1
2
3
4
5
6
7
8
9
10
11
vector3D vector3D::crossProduct( vector3D b)const
{
    vector3D x(0,0,0);
    const vector3D& a = *this;

    x.x = a.y*b.z - a.z*b.y;// ahh
    x.y = a.z*b.x - a.x*b.z;// symmetry restored
    x.z = a.x*b.y - a.y*b.x;

    return x;
}
Last edited on
well now im getting an error msg now

undefined reference to `vector3D::vector3D(int, int, int)'|
vector3D::vector3D(int, int, int)'|
undefined reference to `vector3D::crossProduct(vector3D) const'|
|12|undefined reference to `vector3D::print()'
yes i have heard of this
im still getting the same errors
closed account (D80DSL3A)
I don't know what your code looks like now so I can't tell what's wrong with it.
It can't find the definitions for the member functions.

Have you ever defined a class with functions in it before?
Please put a bit more effort into presenting your problem so we can solve it without going back and forth a bunch of times.
SHOW YOUR CODE.
alright sorry here it is i try to make variables different i try a multiples things to get it working on my own

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 "vector3D.h"
#include <iostream>
using namespace std;
vector3D::vector3D(int xIn, int yIn, int zIn)
{
    x = xIn;
    y=yIn;
    z=zIn;
}
void vector3D::print()
{
    cout << x << "," << y << "," << z << endl;
}
vector3D vector3D::crossProduct(vector3D b) const
{
    vector3D x(0,0,0)

    const vector3D& a=*this;
    x.x =(a.y * b.z)-(a.z*b.y);
    x.y = (a.z*b.x)-(a.x *b.z);
    x.z =(a.x*b.y)-(a.y*b.x);
    return x;
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef VECTOR3D_H
#define VECTOR3D_H
#include <iostream>
using namespace std;

class vector3D
{
    public:
        int x,y,z;
        vector3D(int xIn, int yIn, int zIn );
        void print();
        vector3D crossProduct(vector3D b) const;


};

#endif // VECTOR3D_H



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "vector3D.h"


using namespace std;

int main()
{
    vector3D p1(1,0,0);
    vector3D p2(0,0,0);
    vector3D total =p1.crossProduct(p2);
    total.print();
    return 0;
}
closed account (D80DSL3A)
Good news. I copied your code and tested it.
I found 1 error. Missing ; line 16 vector3D.cpp
where vector3D x(0,0,0)
should be vector3D x(0,0,0);.
Once I fixed that it ran fine!
Last edited on
im still getting errors -___- try to oprn a new project program to put it into a new project and now im getting an error
||undefined reference to `WinMain@16'|

Pages: 12