returning an array from a function and using again in function

This is a discussion on returning an array from a function and using again in function within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to write a function which multiplies two 2d arrays and it stores a result in a ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    7

    returning an array from a function and using again in function

    Hi, I am trying to write a function which multiplies two 2d arrays and it stores a result in a third 2d array. Now I want to multiply this result with another 2d array.
    I have tried to write a function which does this multiplication and returns a pointer. but I can not do multiplication second time. Can anyone help me plz. Thank you.

    I am posting my code here:



    Code:
    //matrix multiplication
    
    #include<iostream>
    #include<cmath>
    using namespace std;
    double** matmulti(int rows1,int columns1,double dynarray1[][3],int rows2,int columns2,double dynarray2[][3]);
    int main()
    {
    	int rowsx=3;
    	int columnsx=3;
    	int rowsy=3;
    	int columnsy=3;
    	double alpha=1.2;float beta=2.3;float gamma=1.4;
    	double x,y,z;
    	
    	double rotx[3][3]={{1.00,0,0},
    		  {0,cos(alpha),-sin(alpha)},
    		  {0,sin(alpha),cos(alpha)}	
    		 } ;		
    	double roty[3][3]={{cos(beta),0,sin(beta)},
    		  {0,1.00,0},	
    		  {-sin(beta),0,cos(beta)}
    		 }; 
    	double rotz[3][3]={{cos(gamma),-sin(gamma),0},
    		  {sin(gamma),cos(gamma),0},
    		  {0,0,1.00}
    		 }; 
    					  
    	
    	double** result=matmulti(rowsx,columnsx,rotx,rowsy,columnsy,roty);
    	
    	//now I want to multiply this result with the third matrix which I can not do
    	
    	double** result1=matmulti(rowsx,columnsx,result,rowsy,columnsy,rotz);	
    }
    
    
    double** matmulti(int rows1,int columns1,double dynarray1[][3],int rows2,int columns2,double dynarray2[][3])
    {
    
    	int rows=rows1;
    	int columns=columns2;
    	double** result;
    	result=new double* [rows];
    	for(int i=0;i<rows;i++)
    	{
    		result[i]=new double[columns];
    	}	
    	
    
    	//cout<<"\n\n\nresult is \n\n"; 
    	if(columns1==rows2){	
    	for(int i=0;i<rows1;i++){
    	for(int j=0;j<columns2;j++){
    	for(int k=0;k<rows2;k++){
    	result[i][j]+=(dynarray1[i][k])*(dynarray2[k][j]);
    	}
    	//cout<<result[i][j]<<" ";
    	}
    	//cout<<"\n";
    	}
    	}else
    	{
    	cout<<"Matrices can not be multiplied\n";
    	exit(-1);
    	}
    
    		
    	return result;
    	for(int i=0;i<3;i++)
    	{
    	delete[] result[i];
    	}
    	delete result;
    	
    	
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Anything after "return" doesn't happen, so there's no reason for it to be there. Also double** and double[][] are not the same type, and cannot be smushed, folded, spindled, or mutilated to be forced to agree. Therefore you must either choose all double** or all double[][]. (Edit: Or you can decide that you are writing C++ and use vector.)

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    I would suggest that you rewrite the above code using object oriented programming.
    IMHO, it's not worth fixing anything else until you've done that. You are shooting yourself in the foot already.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    I'd suggest using std::vector for all your arrays and pass them by reference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    7
    Thank you all for helping. I will try using vectors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. returning int array from a function
    By rubu in forum C Programming
    Replies: 15
    Last Post: 05-11-2011, 10:56 AM
  2. c++ -- returning an array from function
    By p4plus2 in forum C++ Programming
    Replies: 25
    Last Post: 08-18-2008, 02:48 PM
  3. Returning an array from a function
    By cjschw in forum C++ Programming
    Replies: 10
    Last Post: 07-20-2003, 01:03 AM
  4. returning an array from a function
    By BubbleBoy in forum C Programming
    Replies: 1
    Last Post: 02-20-2003, 12:41 PM
  5. returning a 2D array in a function
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 01-05-2002, 08:56 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21