07/05/09 01:10:57
もうおとなしくC++ & STL使うといいよ
#include <vector>
#include <iostream>
typedef std::vector<double> vec_t;
typedef unsigned int UINT;
double inner_product (const vec_t& a, const vec_t& b) {
typedef vec_t::size_type vec_sz;
vec_sz len(a.size());
std::cout << "length of a:" << (UINT)len << ", size of a[0]:" << (UINT)sizeof(a[0]) << '\n';
if (len != b.size()) {
std::cerr << "can not define inner product of these vectors" << std::endl;
return 0;
}
double ip(0);
for (vec_sz i = 0; i < len; ++i)
ip += a[i]*b[i];
return ip;
}
int main () {
// 1.3,0,1
vec_t v1;
v1.push_back(1.3); v1.push_back(0); v1.push_back(1);
vec_t v2;
v2.push_back(2); v2.push_back(3); v2.push_back(4);
std::cout << "length of a:" << (UINT)v1.size() << ", size of a[0]:" << (UINT)sizeof(v1[0]) << '\n';
std::cout << inner_product(v1,v2);
return 0;
}