在C++

2017-06-22 46 views
-1

我不知道用malloc加入陣列什麼錯我的代碼,但IM的 屏幕上會出現「總和」的值相同.. 假設m和n等於進入.... enter image description here在C++

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

} 
+1

'malloc.h'已被棄用。就這樣你知道。 – StoryTeller

+1

不要在C++中使用malloc或C風格的數組。 'malloc h'是非標準的。獲取[C++書籍](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 –

+0

使用索引而不是指針算術,你會看到它。 ('* p'等價於'p [0]')。 – molbdnilo

回答

2

首先,你得到相同數字的原因是從你構成總和的地方開始。 在這個循環

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如果沒有元素的顯示也這樣做。另外,我注意到你有mableced,沒有釋放 - 也許使用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); 
    } 

post顯示了一種方法來neaten了數據錄入。然而,讓我們做一些簡單而得到的總和:

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++中使用malloc。只是說。

1

我強烈建議您使用像矢量這樣的現代cpp數據結構來存儲數據。因此,您不必擔心malloc,並且可以更輕鬆地訪問它們。

但現在你的問題:你的循環總結被打破。使用

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

最佳regrads,喬治

+0

喬治thanx問題解決了! –

+0

第一個問題已經解決了 - 你的代碼中m和n不匹配。別忘了釋放你的malloc – doctorlove