数组乘法任务

时间:2012-12-03 22:40:50

标签: c++ c arrays

我想要绕过如何将2个数组(作为输入)中的值相乘以获得输出。我遇到的问题是如何增加循环以实现下面显示的任务

#include <iostream>

using namespace std;

main()
{
 int* filter1, *signal, fsize1 = 0, fsize2 = 0, i = 0;

 cout << " enter size of filter and signal" << endl;
 cin >> fsize1 >> fsize2;

filter1 = new int [fsize1];
signal = new int [fsize2];

cout << " enter filter  values" << endl;
for (i = 0; i < fsize1; i++)
cin >> filter1[i];
 cout << " enter  signal values" << endl;
for (i = 0; i < fsize2; i++)
cin >> signal[i];

/*

这两个数组应由用户填充,但使用下面的数组进行测试:

int array1[6] = {2, 4, 6, 7, 8, 9};
int array2[3] = {1, 2, 3};

The output array should be

array3[8]= {1 * 2, (1 * 4 + 2 * 2), (1 * 6 + 2 * 4 + 3 * 2), (1 * 7 + 2 * 6 + 3 * 4), (1 * 8 + 2 * 7 + 3 * 6), (1 * 9 + 2 * 8 + 3 * 7), (2 * 9 + 3 * 8), 3 * 9}


*/


 return 0;
 }

这是关于滤波采样信号的一项更大任务的一部分,但正是这种乘法我无法完成。

4 个答案:

答案 0 :(得分:1)

请点击此处查看有关convolution的信息。

一旦你理解了这个过程,那么编码就不那么困难了。本网站还有一个用于二维卷积的C ++算法。

答案 1 :(得分:0)

假设您有三个长度为N的数组:

  • input1,第一个输入数组
  • input2,第二个输入数组
  • output,输出数组

然后,对于从i0的{​​{1}},循环并将N - 1的结果放入input1[i] * input2[i](假设为dot product)。

答案 2 :(得分:0)

在7年的时间里,我还没有读过有关卷积的文字。以下代码(适用于您的测试值)完全基于您的示例。由于您没有以任何正式方式阐述所需的算法,因此我无法确定此代码是否会为其他输入生成所需的输出。

我更改了一些变量名称,并重新排序了您提供的一些代码,因此我可以仔细思考。

#include <iostream>
using namespace std;

void main ()
{
    int *signal, *filter, signalLength = 0, filterLength = 0, i = 0;

    cout << "Enter size of signal and filter" << endl;
    cin >> signalLength >> filterLength;

    signal = new int [signalLength];
    filter = new int [filterLength];

    cout << "Enter  signal values" << endl;
    for (i = 0; i < signalLength; i++)
    {
        cin >> signal[i];
    }

    cout << "Enter filter  values" << endl;
    for (i = 0; i < filterLength; i++)
    {
        cin >> filter[i];
    }

    // It was a stated requirement that the filter array be smaller than
    // the signal array.
    // add a check here and act accordingly

    int outputLength = signalLength + filterLength - 1;
    int *output = new int[outputLength];
    int signalLeft = 0;
    int signalRight = 1;
    int filterLeft = 0;
    int filterRight = 1;
    int outputIndex = 0;
    while (signalLeft < signalLength)
    {
        int indexWidth = signalRight - signalLeft;


        // I sure hope I've interpretted your question correctly.
        // I recommend you read over this loop
        // carefully to ensure it matches your understanding of the problem at hand.
        output[outputIndex] = 0;
        int j = 0;
        while (j < indexWidth)
        {
            output[outputIndex] += signal[signalLeft + j] * 
                                   filter[filterRight - j - 1];

            ++j;
        }


        // The left and right indexes will start the loop 1 index apart
        // The right indexes will increment until the distance between
        //    left and right is equal to the length of the filter array.
        // Then, the signal indexes will increment until the right signal
        //   index is equal to the length of the signal array.
        // Then, both left indexes will increment until the left and right
        //   indexes are again 1 index apart.
        if (filterRight < filterLength)
        {
            ++signalRight;
            ++filterRight;
        }
        else if (signalRight < signalLength)
        {
            ++signalLeft;
            ++signalRight;
        }
        else
        {
            ++signalLeft;
            ++filterLeft;
        }

        ++outputIndex;
    }


    for (i = 0; i < outputLength; ++i)
    {
            cout << i << ": " << output[i] << endl;
    }


    char dummy;
    cin >> dummy;
}

答案 3 :(得分:0)

int newLength = fSize1 + fSize2 - 1;

int *array3 = new int[newLength];
int count = 0;

// initialize all array3 value to 0
for(int k = 0; k<newLength; k++)
{
    array3[k] = 0;
}

for(int i = 0; i<newLength; i++)
{
    for(int j = 0; j<fSize2; j++)
    {
        // to avoid outofbound error
        if(i==0)
            array3[i] = array2[i] * array1[j];
        else
            array3[i] = array3[i- 1] + (array2[i] * array1[j]);
    }
}

希望这会有所帮助。