gpt4 book ai didi

c++ - 我需要一些帮助来创建函数

转载 作者:行者123 更新时间:2023-11-28 04:17:42 24 4
gpt4 key购买 nike

我是新手,对功能不是很好,我正在尝试解决这个问题:

  1. Suppose A, B, C are arrays of integers of size [M], [N], and [M][N], respectively. The user will enter the values for the array A and B. Write a user defined function in C++ to calculate the third array C by adding the elements of A and B. If the elements have the same index number, they will be multiplied. C is calculated as the following: -

    Use A, B and C as arguments in the function.

This is the picture that came with it

下面是我对这个问题的尝试。

     #include<iostream>
using namespace std;

void Mix(int(&A)[], int(&B)[], int(&C)[][100], int N, int M);
//dont understand why you used Q

int main()
{
//variable declaration
int A[100], B[100], C[100][100], n, m, l = 0;


//input of size of elements for first ararys
cout << "Enter number of elements you want to insert in first array: ";
cin >> n;
cout << "-----------------" << endl;
cout << "-----------------" << endl;
cout << "Enter your elements in ascending order" << endl;
//input the elements of the array
for (int i = 0; i < n; i++)
{
cout << "Enter element " << i + 1 << ":";
cin >> A[i];
}
cout << endl << endl;
//input of size of elements for first ararys
cout << "Enter number of elements you want to insert in second array: ";
cin >> m;
cout << "-----------------" << endl;
cout << "-----------------" << endl;
cout << "Enter your elements in descending order" << endl;
//input the elements of the array
for (int i = 0; i < m; i++)
{
cout << "Enter element " << i + 1 << ":";
cin >> B[i];
}

Mix(A, B, C, n, m);

cout << "\nThe Merged Array in Ascending Order" << endl;


for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
{
cout << C[i][j] << " ";
}
cout << "\n"; //endline never use endl its 10 times slower
}

system("pause");
return 0;
}

void Mix(int(&A)[], int(&B)[], int(&C)[][100], int N, int M)
{
// rows is the index for the B array, cols is index for A array
int rows = 0;
int cols = 0;
while (rows < M) {
while (cols < N) {
if (rows == cols) { // remember ==
C[rows][cols] = B[rows] * A[cols];
}
else {
C[rows][cols] = B[rows] + A[cols];
}
cols++; // increment here
}
rows++; // increment here
}
return;
}

这是一个输出示例:

enter image description here

最佳答案

为了使C数组成为二维的,需要将其表示为C[100][100],而不是C[200 ]。这是第一步。接下来,在您的 Mix() 函数中,您需要循环遍历 AB 的每个元素(例如,两个 for 循环)。您的行随着 B 的变化而变化,您的列随着 A 的变化而变化。包括对相同索引的检查,以确定是将两个值相加还是相乘。

void Mix(int A[], int B[], int C[][], int N, int M) {
// rows is the index for the B array, cols is index for A array
for (int rows = 0; rows < M; rows++) {
for (int cols = 0; cols < N; cols++) {
if (rows == cols) { // remember ==
C[rows][cols] = B[rows] * A[cols];
} else {
C[rows][cols] = B[rows] + A[cols];
}
}
}
}

确保您的数组已正确定义并按行和列打印出 C 数组以符合规范。

更新:如果您想使用 while 循环,我会默认解构 for 循环并应用相同的逻辑:

void Mix(int A[], int B[], int C[][], int N, int M) {
// rows is the index for the B array, cols is index for A array
int rows = 0;
int cols = 0;
while (rows < M) {
while (cols < N) {
if (rows == cols) { // remember ==
C[rows][cols] = B[rows] * A[cols];
} else {
C[rows][cols] = B[rows] + A[cols];
}
cols++; // increment here
}
rows++; // increment here
}
}

我肯定会推荐 for 循环方法,因为它更紧凑,但执行完全相同的操作。

关于c++ - 我需要一些帮助来创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56241400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com