Home > OS >  C : Defining 2D matrix in a function and use it in main()
C : Defining 2D matrix in a function and use it in main()

Time:03-31

I am toying with the idea of learning C to expand my digital horizons. For this I would like to transform the following Scilab/Matlab/Octave minimal example


function M=funM(c)
   if c==1
     M=[1,2,3;4,5,6]
   else
     M=[10,20,30;40,50,60]
   end
 endfunction
 c=1;
 M=funM(c);
 disp(M(2,2))

into C - successless by now.

My umpteenth attempt


#include <iostream>
using namespace std;

int func(int c);
int M[2][3];

//function
int func(int c)
{
  if (c==1)
    {
    int M[2][3]={{1,2,3},{4,5,6}};
    }
  else
    {
    int M[2][3]={{10,20,30},{40,50,60}};
    }
return M[2][3];
}//end of function

int main()
{
    int c=1;
    int M[2][3]={func(c)};// Ausgabe: 0 [:(]
    cout << "M[1][1]: " << M[1][1] << "\n";// Ausgabe 0 statt 5
    return 0;
}

fails too. I also tried it with pointers - again only generating error messages.

I would be happy if someone would post a runnable version. The above code failure is hopefully at least by single lines apt to simplify answering via copy&paste. I use CodeBlocks

Regards Rosestock

CodePudding user response:

Yunnosch, who are you to decide what is a programming question? Bjarne Stroustrup?

If you want to translate code from Matlab (world of matrices) to C , I recommend you to use std::vector. For example:

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

int main()
{
    // declare an empty vector
    vector<int> v;

    // add elements in the vector one by one
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

    // declare another vector in a different way
    vector<int> w{4, 5, 6};
     
    // declare a "matrix"
    vector<vector<int>> M;
    
    // put your vectors in your matrix
    M.push_back(v);
    M.push_back(w);

    // display an element of your "matrix"
    int i = 0;
    int j = 1;
    cout<<M[i][j]<<endl;
    
    return 0;
}

The result you should get is the second element (j=1) of your first vector (i=0), i.e. 2.

https://www.geeksforgeeks.org/initialize-a-vector-in-cpp-different-ways/

CodePudding user response:

Thanks Clement for your constructive answer! Based on it I could code the approximate "translation" of the given Matlab code:

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

int Case=1;//input 1|2
int cpprow=0;//input 0|1 for M[2][3]
int cppcol=2;//input 0|1|2 for M[2][3]

vector<int> v1;
vector<int> v2;
vector<vector<int>> M;//M as a vector of vectors

int func(int Case)//Case-dependent definition of M
    {
      if(Case==1)
          {
          v1={1, 2, 3};
          v2={4, 5, 6};
          }
      else if(Case==2)
          {
          v1={10, 20, 30};
          v2={40, 50, 60};
          }
      else
          {
          v1={100, 200, 300};
          v2={400, 500, 600};
          }
      M.push_back(v1);
      M.push_back(v2);
    }//end of func(Case)

int main()
    {
      func(Case);
      cout<<"M["<<cpprow<<"]["<<cppcol<<"]="<<M[cpprow][cppcol]<<endl;
      return 0;
    }//end of main() and cpp

Comparing the codes with respect to their intuitiveness and compactness I will rethink whether I should go further with C . To me it looks like a low level language. If I were to enjoy something like this, maybe I should get into assembler right away.

  •  Tags:  
  • c
  • Related