E
E
Evgeny Yakushov2019-11-22 14:05:35
css
Evgeny Yakushov, 2019-11-22 14:05:35

How to pass a 2D array to a returned function?

How to pass an array to a function and return a value?

double maxElemUnderD(double matric, int size)
{
  double elem = 0;
  
  for (int i = 1; i < size; i++)
  {
    for (int j = i; j < size; j++)
    {
      if (matric[i][j] > elem)
      {
        elem = matric[i][j];
      }
    }
  }

  return elem;
}

void main()
{
  setlocale(LC_ALL, "rus");
  double eps = 0.01;
  const int size = 3;
  double maxElem;
  double matric[size][size] =
  {
    2, -1, 3,
    -1, 4, 2,
    3, 2, 5
  };

  maxElem = maxElemUnderD(matric, size);
}

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Sokolov, 2018-11-24
@jack_azizov

Something like this:

A
Andrej Sharapov, 2018-11-23
@Madeas

line + circle repeat

D
dicem, 2018-11-23
@dicem

I offer you an excellent and simple library for creating both static and dynamic charts: Chartist.js

W
WinPooh32, 2019-11-22
@yevgenyyakushov

#include "iostream"

const int size = 3;
  
double maxElemUnderD(double &matric[size][size])
{
  double elem = 0;
  
  for (int i = 1; i < size; i++)
  {
    for (int j = i; j < size; j++)
    {
      if (matric[i][j] > elem)
      {
        elem = matric[i][j];
      }
    }
  }

  return elem;
}

int main()
{
  double eps = 0.01;
  double maxElem;
  double matric[size][size] =
  {
    {2, -1, 3},
    {-1, 4, 2},
    {3, 2, 5}
  };

  maxElem = maxElemUnderD(matric);
  
  std::cout << maxElem << std::endl;
  
  return 0;
}

R
Roman, 2019-11-22
@myjcom

How to pass a two-dimensional array to the returned function ?

#include<iostream>
#include<limits>
#include<algorithm>

const int size = 3;

auto maxElemUnderD()
{
  return [](double(&mtx)[size][size]){
    double maxelm = std::numeric_limits<double>::min();
    for(auto const& row : mtx)
    {
      double max = *std::max_element(std::cbegin(row), std::cend(row));
      if(max > maxelm) maxelm = max;
    }
    return maxelm;
  };
}

int main()
{
  double matric[size][size] =
  {
    {2, -1, 3},
    {-1, 4, 2},
    {3, 2, 5}
  };
  double maxElem = maxElemUnderD()(matric);
  std::cout << maxElem << std::endl;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question