【问题标题】:adding arrays using malloc in c++在 C++ 中使用 malloc 添加数组
【发布时间】:2017-06-22 08:31:41
【问题描述】:

我不知道我的代码有什么问题,但我在 屏幕.. 假设输入的 m 和 n 相等 ....enter image description here

#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;
}

}

【问题讨论】:

  • malloc.h 已弃用。让你知道。
  • 不要在 C++ 中使用 malloc 或 C 风格的数组。 malloc h 是非标准的。获取book on C++
  • 不要使用malloc & free 但至少new & delete,但更喜欢smart pointers
  • 另外,尽可能使用standard containers
  • 您的文字图片isn't very helpful。它无法大声朗读或复制到编辑器中,并且索引不是很好,这意味着有相同问题的其他用户不太可能在这里找到答案。请edit您的帖子直接合并相关文本(最好使用复制+粘贴以避免转录错误)。

标签: c++ arrays sum malloc


【解决方案1】:

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

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。另外,我注意到您已经 malloced 并且没有释放 - 也许使用 C++ 数组而不是 C 样式数组更好?稍后我会谈到这一点。

如果我们超出数组的末尾,让我们使用 C 方法并有一个0。 这可行,但可以整理 - cmets 内联一些重要的事情

#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);
    }

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

    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 &lt;iterator&gt;,您可以使用算法将您的总和放入sum

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

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

避免在 C++ 中使用 malloc。只是说说而已。

【讨论】:

    【解决方案2】:

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

    但是现在你的问题是:你的求和循环被打破了。使用

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

    最好的毕业生,乔治

    【讨论】:

    • 第一个问题已解决 - 尝试使用 m 和 n 不匹配的代码。不要忘记释放你的 malloc
    猜你喜欢
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 2011-12-15
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多