Need help with program

My assignment is to write a program that asks a user to enter two 1 x 3 vectors and then calculates the product. [a1 a2 a3] = first vector and [b1 b2 b3] = second vector. I have to create a user defined struct to hold each vector and the answer.
Can anyone guide me in the right direction. I'm a complete noob.. Here's what I have so far but it doesn't even work .


#include <iostream>

int main()
{
using namespace std;

struct x{
float a1;
float a2;
float a3;
};

struct y{
float b1;
float b2;
float b3;
};

struct ans{
float c1;
float c2;
float c3;
};

cout << "Please enter the first vector: " << endl;
cin >> x.a1; and cin >> x.a2 >> and cin >> x.a3;

cout << "Please enter the second vector: " << endl;
cin >> y.b1; and cin >> y.b2 >> and cin >> y.b3;

return 0;

}


Without using array
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
#include <iostream>
using namespace std;
struct x{
float a1;
float a2;
float a3;
};

struct y{
float b1;
float b2;
float b3;
};


int main()
{


	x x1;
	y y1;
	
cout << "Please enter the first vector: " << endl;
cin >> x1.a1  >> x1.a2  >> x1.a3;

cout << "Please enter the second vector: " << endl;
cin >> y1.b1 >> y1.b2 >> y1.b3;

cout<<"The resultant vector:"<<endl;
cout<<x1.a1*y1.b1<<" "<<x1.a2*y1.b2<<" "<<x1.a3*y1.b3<<" "<<endl;

system("pause");
return 0;

}
Using array
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
#include <iostream>
using namespace std;
struct x{
float a[3];
};

struct y{
float b[3];
};


int main()
{
	x x1;
	y y1;
	
cout << "Please enter the first vector: " << endl;
for(int i=0;i<3;i++)
{
	cin>>x1.a[i];
}
cout<<endl;
cout << "Please enter the second vector: " << endl;
for(int i=0;i<3;i++)
{
	cin>>y1.b[i];
}
cout<<endl;
cout<<"The result vector"<<endl;
for(int i=0;i<3;i++)
{
 cout<<x1.a[i]*y1.b[i]<<" ";
}
cout<<endl;

system("pause");
return 0;

}
u can put all variables under one struct
1
2
3
4
5
6
7
8
9
10
11
12
13
struct x
{
float a1,a2,a3,b1,b2,b3;
};


or

struct x
{
float a[3],b[3];
};
You don't have to declare three different structures for holding three vectors (both the inputs and the result, type is still same).

Instead use
1
2
3
4
struct vector
{
     float x, y, z;
}v1, v2, vectprod;


then replace x1.a1 with v1.x, x1.a2 with v1.y and x1.a3 with v1.z
same for v2. then store the product into vectprod.

When you say it doesnt work what is actually going wrong? Compilation errors? Run time errors?
Last edited on
Topic archived. No new replies allowed.