找到奇数向量元素C ++

时间:2015-01-22 08:08:04

标签: c++ vector

所以我编写了一个使用向量计算A[0]*B[0] - A[1]*B[1] - A[2]*B[2] - ... - A[n-1]*B[n-1]的小程序。

#include <iostream>
#include <string>
#include <vector>

using namespace std;

double CalcProduct(vector<double> v1,vector<double> v2){

    size_t size; int i = 0;
    vector<double> product;

    double input, input2;

    cout << "Enter vector size: ";
    cin >> size;

    cout << "Enter values for first vector: ";
    while (i++ != size) {
        cin >> input;
        v1.push_back(input);
    }
    i = 0;
    cout << "Enter values for second vector: ";
    while (i++ != size) {
        cin >> input2;
        v2.push_back(input2);
    }

    double result = 0.0;
    if (size >= 1) {
        result += v1[0] * v2[0];
        for (int i = 1; i < size; ++i)
            result -= v1[i] * v2[i];
    }

    cout << "\nThe 'happy' product of both vectors is: " << result << endl;
}

int main()
{
    vector<double> v1;
    vector<double> v2;

    CalcProduct(v1,v2);

    return 0;
}

但是,如果我想计算A[1]*B[1] - A[3]*B[3] - A[5]*B[5] - ....,会发生什么?我尝试在v1之前添加+1 v2result,但我的逻辑似乎是错误的。样本输入: 5 1 2 3 4 5 4 5 6 7 5 预期输出:-18

2 个答案:

答案 0 :(得分:3)

您正在此循环中进行计算

for(int i = 1; i < size; ++i)
    result -= v1[i] * v2[i];

因此,您可以跳过2:

,而不是递增i
for(int i = 1; i < size; i+=2) {
    result -= v1[i] * v2[i];
}

请注意,确保您没有超出界限是一种很好的做法,我也喜欢用for和{{1}围绕{循环体即使它包含一行。

编辑:我没有注意到你已经在循环之前添加了第一个元素,你可以从}而不是1开始轻松解决这个问题。

答案 1 :(得分:1)

首先,calcProduct一次做三件事:读取值,计算产品并将结果写入输出。因此,您应该将其拆分为两种方法,并且可以从main()执行打印输出。

其次,一旦生成了向量,就可以通过检查循环计数器来使用vector.size()来避免出界情况。

// give vector as reference 
void ReadInput(vector<double>& v1,vector<double>& v2)
{
    size_t size = 0; // use size_t here as this will be well defined
    size_t i = 0;    // on 32 and 64 bit architectures

    // maybe you should have more error handling here as
    // one might desire size to be in some application 
    // specific range
    cout << "Enter vector size: ";
    cin >> size;

    cout << "Enter values for first vector: ";
    while (i++ != size) 
    {
        cin >> input;
        v1.push_back(input);
    }

    i = 0;
    cout << "Enter values for second vector: ";
    while (i++ != size) 
    {
        cin >> input2;
        v2.push_back(input2);
    }
}

// giving the parameters as (const) references avoids unneccessary copying
// of data
double CalcProduct(const vector<double>& v1,  const vector<double>& v2,
                   size_t start, size_t step)
{
    double result = 0.0;

    // do parameter checking before the calculation in order to keep
    // the code more structured (e.g. this can be move to a seperate
    // function if it gets too complex
    if (   v1.size() != v2.size() 
        || start > v1.size() 
        || start+step > v1.size()) 
        || start < 1 )
    {
          throw out_of_range("CalcProduc(): parameter mismatch");
    }


    result = v1[start] * v2[start];

    for (size_t i = start+step; i < v1.size(); i+=step)
    {
        result -= v1[i] * v2[i];
    }

    return result;
}

int main()
{
    vector<double> v1;
    vector<double> v2;

    ReadInput(v1, v2);
    cout << "\nThe 'happy' product of both vectors is: " << CalcProduct(v1, v2, 1, 2) << endl;

    return 0;
}