首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >查找点积的函数

查找点积的函数
EN

Stack Overflow用户
提问于 2018-02-14 11:53:59
回答 1查看 407关注 0票数 1

我正在尝试填充两个数组,然后使用一个函数来查找点积。我不确定这是不是我填充数组的方式,或者是我处理函数的方式。此外,如果我将大小设置为6,并输入1,2,3,4,5,6,第一个数组将填充1,2,3,4,1,2……它在4之后重置。第二个数组被正确填充。我不知道有没有人能帮我一把。

代码语言:javascript
复制
#include <iostream>
using namespace std;

int dotProduct(int* array1, int*array2, int size);

int main() {

    int  size = 0;

    int *array_one = new int[size]{};
    int *array_two = new int[size]{};

    cout << "Please enter array size:" << "\n";
    cin >> size;

    while (size <= 0) { // First while loop, checking array size

        cout << "Please enter array size:" << "\n";
        cin >> size;
    }                 //end of first while loop

    cout << "========= Begin Entering Array Elements =========" 
         << "\n";
    cout << "Array 1: "<< "\n";

    for (int i = 0; i<size; i++){ // Filling up first array, first for 
        loop
            cout << "Enter element number "<< i+1 << ": " ;
        cin >> array_one[i];
    }                               // end or first for loop

    cout <<"=================================================" << 
        "\n";
    cout << "Array 2:" << "\n";

    for (int i = 0; i<size; i++){ // Filling up first array, first for 
        loop
            cout << "Enter element number "<< i+1 << ": " ;
        cin >> array_two[i];
    }

    cout << "The dot product is: " << 
        dotProduct(array_one,array_two,size);
}

int dotProduct(int *arrayUno, int *arrayDos, int size){

    int total = 0;

    for (int i =0; i <= size ; i++ ){
        total = total + (*arrayUno)*(*arrayDos);
        arrayUno++;
        arrayDos++;
    }

    return total;
}
EN

回答 1

Stack Overflow用户

发布于 2018-02-14 11:56:38

代码语言:javascript
复制
int  size = 0;

int *array_one = new int[size]{};
int *array_two = new int[size]{};

cout << "Please enter array size:" << "\n";
cin >> size;

while (size <= 0) { // First while loop, checking array size

    cout << "Please enter array size:" << "\n";
    cin >> size;
}                 //end of first while loop

在获得大小之前,需要先分配数组。你得先弄清楚尺码。将此代码更改为:

代码语言:javascript
复制
int size = 0;

cout << "Please enter array size:" << "\n";
cin >> size;

int *array_one = new int[size]{};
int *array_two = new int[size]{};
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48779391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档