mardi 29 décembre 2020

How to pair 2D array and integer in the output of the function?

I'm struggling to create multi-variable output from a function: I want to return 2D array sites(16x15) and the integer number N.

I tried:

  1. std::make_tuple here
  2. std:make_pair here

My problem is that I probably do not know how to define a 2D array in the declaration of the function std::pair <int [][],int> correctly.

Piece of code named function.cpp:

#include <iostream>

std::pair <int[16][15],int> sites_diamond()
{
    int sites[16][15]={0};
    int N=0;
    for (int r=0; r<7; r++) {
        N=N+1+2*r;
        for (int c=0; c<(7-r);c++){
            sites[r][c]=0;
            sites[15-r][c]=0;
            sites[r][14-c]=0;
            sites[15-r][14-c]=0;
        }
    }
    N=2*(N+15);
    return std::make_pair(sites, N);
}

using namespace std;

int main(){
    std::pair <int[16][15], int> result = sites_diamond();
    cout << " sites \n"<<result.first<< endl;
    cout << "number \n"<<result.second<< endl;

    return 0;
}

Error I'm getting:

function.cpp: In function ‘std::pair<int [16][15], int> sites_diamond()’:
function.cpp:21:26: error: could not convert ‘std::make_pair<int (&)[16][15], int&>(sites, N)’ from ‘std::pair<int (*)[15], int>’ to ‘std::pair<int [16][15], int>’
     return std::make_pair(sites, N);

Thank you ahead for any suggestions. I work primarily in Python, but I want to rewrite a code to C++.

Aucun commentaire:

Enregistrer un commentaire