Function that returns a pointer
A function that takes as input a matrix and a vector and returns a pointer as output.
//============================================================================ // Name : functRetPointer.cpp // Author : chd // Version : // Copyright : // Description : A function that takes as input a matrix and // a vector and returns a pointer.C++ ex //============================================================================ #include <iostream> using namespace std; double* MtrVectMultiply( double** , double* , int, int); int main() { //The function has inputs a matrix and a vector. Hence we need //to create space for a matrix and for a vector int m=6; int n=3; double** a=new double* [m]; for (int i = 0; i < m; ++i) { a[i]=new double [n]; } double* b=new double [n]; //assign some values for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { a[i][j]=1; } } for (int j= 0; j< n; ++j) { b[j]=1; } //caling the function double* c=MtrVectMultiply(a,b,m,n); for (int i = 0; i < m; ++i) { cout<<"|"<<c[i]<<"|"<<endl; } for (int i = 0; i < m; ++i) { delete[] a[i]; } delete[] a; delete[] b; delete[] c; } double* MtrVectMultiply( double** mtr, double* vec, int m, int n) { //allocating memory for the pointer of the returning object (vector) double* res=new double [m]; //double sum=0; for (int i = 0; i < m; ++i) { res[i]=0; for (int j = 0; j < n; ++j) { res[i]+=mtr[i][j]*vec[j]; } } //return the address of the product return res; }