用C++编写两个数组运算的值
时间: 2024-05-07 21:20:40 浏览: 61
以下是两个数组相加的示例代码:
```cpp
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 5;
int arr1[SIZE] = {1, 2, 3, 4, 5};
int arr2[SIZE] = {5, 4, 3, 2, 1};
int sumArr[SIZE];
for (int i = 0; i < SIZE; i++)
{
sumArr[i] = arr1[i] + arr2[i];
}
cout << "The sum of the two arrays is: ";
for (int i = 0; i < SIZE; i++)
{
cout << sumArr[i] << " ";
}
cout << endl;
return 0;
}
```
以下是两个数组相乘的示例代码:
```cpp
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 5;
int arr1[SIZE] = {1, 2, 3, 4, 5};
int arr2[SIZE] = {5, 4, 3, 2, 1};
int multArr[SIZE];
for (int i = 0; i < SIZE; i++)
{
multArr[i] = arr1[i] * arr2[i];
}
cout << "The product of the two arrays is: ";
for (int i = 0; i < SIZE; i++)
{
cout << multArr[i] << " ";
}
cout << endl;
return 0;
}
```
阅读全文