/****************************/
/* ベクトルの内積と大きさ */
/* coded by Y.Suganuma */
/****************************/
#include <iostream>
#include <math.h>
using namespace std;
/**********************/
/* Vectorクラスの定義 */
/**********************/
class Vector {
public:
int n;
double *v;
Vector (int n) // コンストラクタ
{
this->n = n;
v = new double [n];
}
~Vector() // デストラクタ
{
if (n > 0)
delete [] v;
}
double norm(); // 絶対値
double naiseki(Vector &); // 内積
void input(); // 要素の入力
};
/**********/
/* 大きさ */
/**********/
double Vector::norm()
{
double x = 0.0;
for (int i1 = 0; i1 < n; i1++)
x += v[i1] * v[i1];
return sqrt(x);
}
/*********************/
/* 内積 */
/* b : ベクトル */
/*********************/
double Vector::naiseki(Vector &b)
{
double x = 0.0;
for (int i1 = 0; i1 < n; i1++)
x += v[i1] * b.v[i1];
return x;
}
/********/
/* 入力 */
/********/
void Vector::input()
{
for (int i1 = 0; i1 < n; i1++) {
cout << " " << i1+1 << "番目の要素は? ";
cin >> v[i1];
}
}
/******************/
/* mainプログラム */
/******************/
int main()
{
Vector a(2), b(2);
cout << "a\n";
a.input();
cout << "b\n";
b.input();
cout << "a と b の内積 " << a.naiseki(b) << endl;
cout << "a,及び,b の大きさ " << a.norm() << " " << b.norm() << endl;
return 0;
}