在 C++ 中使用 malloc 添加数组

adding arrays using malloc in c++

本文关键字:malloc 添加 数组 C++      更新时间:2023-10-16

我不知道我的代码出了什么问题,但我在 屏幕。。 假设 m 和 n 输入相等....在此处输入图像描述

#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n,m;
int *ptr1, *ptr2, *sum;
cout<<" enter the size of 1st and 2nd array : "<<endl;
cin>>n>>m;
ptr1=(int*)malloc(n*sizeof(int));
ptr2=(int*)malloc(m*sizeof(int));
sum=(int*)malloc((n)*sizeof(int));
cout<<"enter 1st array element :";
for(int i=0;i<n;i++)
{
cin>>*(ptr1+i) ;
}
cout<<"enter 2st array element :";
for(int i=0;i<m;i++)
{
cin>>*(ptr2+i);
}
for(int j=0;j<m||j<n;j++)
{
*(sum+j) =  (*(ptr1) +  *(ptr2)) ;
}
cout<<" the sum is "<<endl;
for(int j=0;j<m||j<n;j++)
{
cout<<*(sum+j)<<endl;
}
}

首先,你得到相同数字的原因源于你形成总和的地方。 在此循环中

for (int j = 0; j<m || j<n; j++)
{
*(sum + j) = (*(ptr1)+*(ptr2));
}

你会发现ptr1ptr2的内容的总和一遍又一遍,永远不会改变 - 这总是前两个数字。

因此,我们可以通过按如下方式在j中索引来迭代数组

for (int j = 0; j<m || j<n; j++)
{
*(sum + j) = (*(ptr1 + j) + *(ptr2 + j));
}

但是如果m!=n会发生什么?您将离开数组的末尾。

如果将循环更改为

for (int j = 0; j<m && j<n; j++)
{
*(sum + j) = (*(ptr1 + j) + *(ptr2 + j));
}

然后,您可以找到数字对的总和,最多为mn中的较小者。 您也必须对结果的显示执行相同的

操作
for (int j = 0; j<m && j<n; j++)
{
cout << *(sum + j) << endl;
}

但是,我相信您希望显示n数字,无论哪个更大,或者如果没有元素,则可能假设0。另外,我注意到您已经错位并且没有释放 - 也许使用 C++ 数组而不是 C 样式数组更好?我一会儿会谈到这一点。

让我们做 C appraoch 并有一个0如果我们超出数组的末尾。 这将起作用,但可以整理 - 关于一些重要事情的内联评论

#include<stdlib.h>
#include <algorithm> //for std::max
#include <iostream>
using namespace std;
int main()
{
int n, m;
int *ptr1, *ptr2, *sum;
cout << " enter the size of 1st and 2nd array : " << endl;
cin >> n >> m;
ptr1 = (int*)malloc(n * sizeof(int));
ptr2 = (int*)malloc(m * sizeof(int));
sum = (int*)malloc((std::max(n, m)) * sizeof(int)); 
//                     ^--- sum big enough for biggest "array"
// data entry as before - omitted for brevity    
for (int j = 0; j<m || j<n; j++)
{
*(sum + j) = 0;
if (j < n)
*(sum + j) += *(ptr1 + j);
if (j < m)
*(sum + j) += *(ptr2 + j);
}
cout << " the sum is " << endl;
for (int j = 0; std::max(n, m); j++)//however big it is
{
cout << *(sum + j) << endl;
}
free(ptr1); //tidy up
free(ptr2);
free(sum);
}

我知道你说你想使用 malloc,也许这是一种带有指针的做法,但请考虑使用C++习语(至少你不会忘记以这种方式释放你已经掌握的东西)。

让我们推动您的代码使用std::vector: 首先是包含和输入:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n, m;
vector<int> data1, data2, sum;
cout << " enter the size of 1st and 2nd array : " << endl;
cin >> n >> m;
cout << "enter 1st array element :";
for (int i = 0; i<n; i++)
{
int number;
cin >> number;
data1.push_back(number); //there is a neater way, but start simple
}
cout << "enter 2st array element :";
for (int i = 0; i<m; i++)
{
int number;
cin >> number;
data2.push_back(number);
}

这篇文章展示了一种整理数据输入的方法。但是,让我们做一些简单的事情并得到总和:

for (int j = 0; j < std::max(m, n); j++)
{
int number = 0;
if (j < n)
number += data1[j];
if (j < m)
number += data2[j];
sum.push_back(number);
}

现在为一种C++输出方式

cout << " the sum is " << endl;
for (auto item : sum)
{
cout << item << 'n';
}
}

最后,让我们简单考虑一下总和。

如果您现在#include <iterator>您可以使用算法将总和放入sum

std::transform(data1.begin(), data1.end(),
data2.begin(), std::back_inserter(sum), std::plus<int>());

但是,请注意,这不会用零填充。您可以使向量大小相同,先用零填充,或者查找/发现压缩不同大小的向量的方法。或者像我上面演示的那样在循环中坚持使用 ifs。

避免在C++中使用马洛克。只是说。

我强烈建议您使用现代 cpp 数据结构(如向量)来存储数据。因此,您不必担心malloc,并且可以更轻松地访问它们。

但是现在回答你的问题:你的循环总和被打破了。用

for(int j=0;j<m||j<n;j++)
{
*(sum+j) =  (*(ptr1+j) +  *(ptr2+j)) ;
}

最佳毕业生,乔治