Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- using namespace std;
- struct vector{
- double a,b,c;
- };
- double len(vector v1){
- double len=sqrt(v1.a*v1.a+v1.b*v1.b+v1.c*v1.c);
- return len;
- }
- double scalar(vector v1,vector v2){
- return (v1.a)*(v2.a)+(v1.b)*(v2.b)+(v1.c)*(v2.c);
- }
- vector difference(vector v1,vector v2)
- {
- vector v3;
- v3.a=(v1.a-v2.a);
- v3.b=(v1.a-v2.b);
- v3.c=(v1.c-v2.c);
- return v3;
- }
- double cos(vector v1,vector v2){
- if( abs(len(v1)*len(v2))<1e-9) { return 0; }
- return (scalar(v1,v2)/(len(v1)*len(v2)));
- }
- bool collinearity(double cos){
- return (abs(cos-1)<1e-9);
- }
- void insertv(vector *m,int N)
- {
- cout << " Insert element of array " <<endl;
- for (int i=0;i<N;i++) {
- cin >> m[i].a >> m[i].b >> m[i].c;
- }
- }
- void printv(vector *m,int N)
- {
- for (int i=0;i<N;i++)
- cout << m[i].a <<" " <<m[i].b<<" "<<m[i].c <<endl;
- cout << endl;
- // system("pause");
- }
- void insertmas(int *mas,int N)
- {
- cout << " Insert element of array n " <<endl;
- for (int i=0;i<N;i++) {
- cin >> mas[i];
- }
- }
- void printmas(int *mas,int N)
- {
- for (int i=0;i<N;i++) { cout <<" "<< mas[i];}
- cout << endl;
- // system("pause");
- }
- double max_len(vector *m,int N){
- if(N <= 0) { return 0; }
- double max = len(m[0]);
- for (int i=1;i<N;i++)
- if (len(m[i]) > max)
- { max=len(m[i]);
- }
- return max;
- }
- int search(vector *m,int N){
- vector vc;
- cout << endl;
- cout << "Insert a,b,c for your vector which you search";
- cin >> vc.a>>vc.b>>vc.c;
- for (int i=0;i<N;i++)
- {
- if ( (abs(m[i].a-vc.a)<1e-9) && (abs(m[i].b-vc.b)<1e-9) && (abs(m[i].c-vc.c)<1e-9) )
- {return i;
- }
- }
- return -1;
- }
- vector sum(vector *m,int N){
- vector sum1;
- sum1.a=0;
- sum1.b=0;
- sum1.c=0;
- for (int i=0;i<N;i++)
- {
- sum1.a+=m[i].a;
- sum1.b+=m[i].b;
- sum1.c+=m[i].c;
- }
- return sum1;
- }
- vector linear_comb(vector *m,int *mas,int N)
- {
- vector lin_comb;
- lin_comb.a=0;
- lin_comb.b=0;
- lin_comb.c=0;
- for (int i=0;i<N;i++)
- {
- lin_comb.a+=m[i].a*mas[i];
- lin_comb.b+=m[i].b*mas[i];
- lin_comb.c+=m[i].c*mas[i];
- }
- return lin_comb;
- }
- int perpendicularity(vector *m,int N,vector v1)
- {
- int count=0;
- for (int i=0;i<N;i++)
- if (abs(cos(m[i],v1))<1e-9){count++;}
- return count;
- }
- void main()
- {
- int N;
- vector v1,v2;
- vector v3;
- cout << " Insert a,b,c for v1 ";
- cin >> v1.a>>v1.b>>v1.c;
- /*cout << len(v1);
- cout << " Insert a,b,c for v2 ";
- cin >> v2.a>>v2.b>>v2.c;
- cout << scalar(v1,v2);
- v3=difference(v1,v2);
- cout << " Difference v1 - v2 " << v3.a<<" " << v3.b<<" " << v3.c<<endl;
- cout <<"collinearity "<<collinearity(cos(v1,v2));
- */
- cout << " Enter elements of array ";
- cin >> N;
- vector *m = new vector[N];
- int *mas = new int[N];
- insertv(m,N);
- cout << endl;
- printv(m,N);
- insertmas(mas,N);
- cout << endl;
- printmas(mas,N);
- cout <<"Max_len "<< max_len(m,N);
- cout<<endl;
- cout<<" Index necessery element "<<search(m,N);
- cout<<endl;
- vector summy=sum(m,N);
- cout<<summy.a<<" "<<summy.b<<" "<<summy.c;
- cout<<endl;
- vector linearry=linear_comb(m,mas,N);
- cout<<linearry.a<<" "<<linearry.b<<" "<<linearry.c;
- cout<<endl;
- cout<<"perpendicularity"<<perpendicularity(m,N,v1);
- delete[]m;
- delete[]mas;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement