2016-05-21 3 views
-2

2次元配列のサイズを関数の引数として取って、配列を受け入れて何とかmain機能を追加します。 ビデオや投稿を見てみましたが、私の答えは得られませんでした。 いつものようにこの問題を助けてください。関数から2次元配列(int型)を受け取り、main関数に返す

int *accept(int a,int b) 
{ 
    int x[50][50]; 
for(int i=0;i<a;++i) 
{ 
    cout<<"Enter elements for "<<i+1<<" th row"; 
    for(int j=0;j<b;++j) 
    { 
    cout<<"Enter "<<j+i<<" th element \n"; 
    cin>>x[i][j]; 
    } 
} 
return *x; 
} 

void main() 
{ 
int arr[50][50],m,n; 
cout<<"Enter the no of rows you want : "; 
cin>>m; 
cout<<"Enter the no of columns you want : "; 
cin>>n; 
arr[50][50]=accept(m,n); //how to copy? 

答えて

0

あなたはmemcpy()

#include <iostream> 
#include <cstring> 
using namespace std; 

void accept(void* arr, int a, int b) 
{ 
    int x[a][b]; 

    for (int i = 0; i < a; ++i) 
    { 
     cout << "Enter elements for " << i+1 << " th row"; 

     for (int j = 0; j < b; ++j) 
     { 
      cout << "Enter " << j+i << " th element \n"; 
      cin >> x[i][j]; 
     } 
    } 

    memcpy(arr, x, sizeof(int) * a * b); 
} 

int main() 
{ 
    int m, n; 

    cout << "Enter the no of rows you want : "; 
    cin >> m; 
    cout << "Enter the no of columns you want : "; 
    cin >> n; 

    int arr[m][n]; 
    accept(&arr, m, n); 

    return 0; 
} 
+0

は、少なくとも関数memcpyのは、何をするのか教えて使用することができます? –

+0

この機能に関する情報は、こちらをご覧ください: http://www.cplusplus.com/reference/cstring/memcpy/ – Ash

関連する問題