将数组中的每个元素与第二个数组中的每个元素相乘

时间:2021-05-10 13:42:59

So we have to use functions to fill, multiply and print arrays. So I created 3 functions: one to fill the arrays, one to print the arrays, and one to multiply the first 2 arrays between them. I have to use the print function 3 times: to print the initial 2 arrays and to print the array resulting from multiplication of the first 2 arrays. The problem I have is when I want to use "multiplication" function in order to multiply each element from the first array with every element from the second array. I am doing something wrong with this function because when I try to launch it freezes.

所以我们必须使用函数来填充,乘法和打印数组。所以我创建了3个函数:一个用于填充数组,一个用于打印数组,另一个用于将前两个数组相乘。我必须使用打印功能3次:打印最初的2个数组并打印由前2个数组的乘法产生的数组。我遇到的问题是当我想使用“乘法”函数时,为了将第一个数组中的每个元素与第二个数组中的每个元素相乘。我正在做这个函数的错误,因为当我尝试启动它冻结。

It should work like this: First array: 5, 5, 5; Second array: 5, 10; I should get this: 25, 25, 25, 50, 50, 50;

它应该像这样工作:第一个数组:5,5,5;第二阵列:5,10;我应该得到这个:25,25,25,50,50,50;

This is my code so far:

到目前为止这是我的代码:

#include <iostream>
using namespace std;
void remplir(int array[], int dim); //function to fill array
void aficher(int array[], int dim); //function to print array
int multiplier(int array11[], int array22[], int dim1, int dim2, int   dim3); //function to multiply array


int main() {
int a1, a2, a3, array1[a1], array2[a2];
cout << "La dimension de la premiere table?" << endl;
cin >> a1;
while (a1 > 20) {
    cout << "La dimension maximum est 20! Reessayez!" << endl;
    cin >> a1;

}
remplir(array1, a1);
aficher(array1, a1);

cout << "La dimension de la deuxieme table?" << endl;
cin >> a2;
while (a2 > 20) {
    cout << "La dimension maximum est 20! Reessayez!" << endl;
    cin >> a2;

}
a3 = a1 * a2;
remplir(array2, a2);
aficher(array2, a2);

multiplier(array1, array2, a1, a2, a3);

return 0;

}

void remplir(int array[], int dim) {
int i = 0;
for (i = 0; i <= dim - 1; i = i + 1) {
    cout << "Entrez la case numero " << i << endl;
    cin >> array[i];

}
}

void aficher(int array[], int dim) {
int i;
for (i = 0; i <= dim - 1; i = i + 1) {
    cout << "indice " << i << " = " << array[i] << endl;
}
}

int multiplier(int array11[], int array22[], int dim1, int dim2, int      dim3) {
int i = 0, a = 0, resultat[dim3];
for (i = 0; i <= dim1 - 1; i = i + 1) {
    for (i = 0; i <= dim2 - 1; i = i + 1) {

        resultat[i] = array11[a] * array22[i];

    }
    a = a + 1;
}

return resultat[i];

}

1 个解决方案

#1


Despite your question not actually explaining what kind of problem you're having, I can already see a big mistake. This line:

尽管你的问题并没有真正解释你遇到了什么样的问题,但我已经看到了一个很大的错误。这一行:

int a1 = 0, a2 = 0, a3 = 0, array1[a1], array2[a2], array3[a3];

int a1 = 0,a2 = 0,a3 = 0,array1 [a1],array2 [a2],array3 [a3];

You've created three zero-sized arrays. And no matter what you do with the a1, a2, a3 variables afterwards, those arrays will continue to remain the same size. Raw arrays do not dynamically resize in C++.

你已经创建了三个零大小的数组。而且无论你如何使用a1,a2,a3变量,这些数组将继续保持相同的大小。原始数组不会在C ++中动态调整大小。

So anything else you're doing trying to index into those arrays is running off into other memory and undoubtedly giving weird results and causing trouble.

所以你正在尝试索引到那些数组的任何其他东西都会运行到其他内存中,毫无疑问会产生奇怪的结果并导致麻烦。

#1


Despite your question not actually explaining what kind of problem you're having, I can already see a big mistake. This line:

尽管你的问题并没有真正解释你遇到了什么样的问题,但我已经看到了一个很大的错误。这一行:

int a1 = 0, a2 = 0, a3 = 0, array1[a1], array2[a2], array3[a3];

int a1 = 0,a2 = 0,a3 = 0,array1 [a1],array2 [a2],array3 [a3];

You've created three zero-sized arrays. And no matter what you do with the a1, a2, a3 variables afterwards, those arrays will continue to remain the same size. Raw arrays do not dynamically resize in C++.

你已经创建了三个零大小的数组。而且无论你如何使用a1,a2,a3变量,这些数组将继续保持相同的大小。原始数组不会在C ++中动态调整大小。

So anything else you're doing trying to index into those arrays is running off into other memory and undoubtedly giving weird results and causing trouble.

所以你正在尝试索引到那些数组的任何其他东西都会运行到其他内存中,毫无疑问会产生奇怪的结果并导致麻烦。