Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C++ Function and matrix

Status
Not open for further replies.

ireon

Junior Member level 2
Joined
Mar 28, 2013
Messages
21
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,455
I should write a function that returns a two-dimensional array. I used a dinamic allocation of memory:

Code:
#include <iostream>
#include "imagine.h"
#include "function1"
#include "functrion2"


using namespace std;


int main()
{
int s = 3;
int m=(sizeof(*A)/4);
int n=(sizeof(A)/4)/m;
int*matrix=NULL;
int*v=NULL;
int*v1=NULL;

matrix=(int*)calloc(n*m,sizeof(int));

for(i=0;i<n;i++)

    {

        for(j=0;j<m;j++)

        {

            *(matrix+i*m+j)=A[i][j];

        }

    }


a=&(A[0][0]);

v=function1(a,n,m,s);

v1=function2(v,n,m,s);

    for(i=0;i<n;i++)

    {

        for(j=0;j<m;j++)

        {

            A[i][j]=*(v1+i*m+j);

        }

    }

return 0;

}

This code works if I call individually function1 or function2, but if I call both the functions it doesn't work and the result is wrong. How could I resolve the problem? Could I use an other method to return a two-dimensional array?
 

Your post title states "C++", but your code is very much C-style. If you are using C++, you should consider using something like a vector, valarray, queue or deque. For example, consider this:
Code:
#include <vector>

...

vector<vector<int> > matrix;
This gives you a vector of vectors.

Also, I'm not sure what you intend to do in your code when you #include "function1" and #include "function2". This looks very strange to me and may not do what you think it does.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top