Passing 2D array to and from a function in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Passing 2D array to and from a function in C++

  1. #1
    Join Date
    Jul 2013
    Posts
    8

    Passing 2D array to and from a function in C++

    Hello,

    I am new to C++ and having problem passing arguments between main and a function.
    I would like to pass three values to a function, have it calculate a 2D matrix and return it back to main. I would like it to accept variable matrix size. I appreciate any help I can get on this. Here is my code:

    #include "stdafx.h"
    #include <cstdlib>
    #include <stdlib.h>
    #include <math.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <iomanip>
    #include <ios>
    #include <vector>
    #include <sstream>
    #include <iterator>
    #include <algorithm>
    #include <sstream>
    #include <deque>
    #include "split.h"
    #include <cstdlib>
    #include "Input_line_of_doubles.h"
    #include "test_pad.h"

    using std::cin;
    using std::cout;
    using std::endl;
    using std::setprecision;
    using std::cerr;
    using std::ifstream;
    using std:fstream;
    using std::string;
    using namespace std;

    int main()
    {
    int k=0, j=0;
    double roll=0.1;
    double pitch=0.2;
    double heading=1.0;
    double **rr=0;

    double Cb_n( double roll, double pitch, double heading, double **rr);

    for (int k =0 ; k<3;k++)
    {
    for(int j =0 ;j <3;j++)
    {
    cout<<rr[k][j];
    }
    cout<<endl;
    }
    return 0;
    }

    double Cb_n(double roll, double pitch, double heading,double **r)
    {
    // function for calculating the rotation body---->navigation Cbn
    double Ch,Sh,Cr,Sr,Cp,Sp;


    Sh=sin(heading);
    Ch=cos(heading);
    Sr=sin(roll);
    Cr=cos(roll);
    Sp=sin(pitch);
    Cp=cos(pitch);
    r[1][1] = Ch*Cp;
    r[1][2] = Ch*Sp*Sr - Sh*Cr;
    r[1][3] = Ch*Sp*Cr + Sh*Sr;
    r[2][1] = Sh*Cp;
    r[2][2] = Sh*Sp*Sr + Ch*Cr;
    r[2][3] = Sh*Sp*Cr - Ch*Sr;
    r[3][1] = -Sp;
    r[3][2] = Cp*Sr;
    r[3][3] = Cp*Cr;

    return **r;
    }
    Last edited by qabdullah; July 19th, 2013 at 09:04 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: Passing 2D array to and from a function in C++

    In your previous thread you were asked a lot of times to use Code tags!
    Why do you still ignore it?
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,308

    Re: Passing 2D array to and from a function in C++

    Please use code tags as you have been previously requested. Go Advanced, select code and then click '#'.

    First things first. Where are you allocating and initialising the memory for your 2d array rr? You have the function declaration for Cb_n in the middle of your main function when it goes before the definition of main. To call the function Cb_n in main you would use

    Code:
    Cb_n( roll, pitch, heading, rr);
    You have several statements specifying the elements of std namespace that you are using then say you are using all elements in namespace std! If you have 'using namespace std', you don't need the proceeding using statements. You also have a bunch of includes which are not required for the code shown.

    In answer to your specific question re the 2d array. You don't need to return it back to main. You are passing a pointer to the start memory of the 2d array to Cb_n (r) so that the calculations you perform in Cb_n are actually performed on the array rr defined in main. So the function Cb_n can just return void.

    Also in Cb_n, the array r bounds start at 0 and end at 2 - NOT 1 and 3!

    Is this an assignment?
    Last edited by 2kaud; July 19th, 2013 at 10:04 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  4. #4
    Join Date
    Jul 2013
    Posts
    8

    Re: Passing 2D array to and from a function in C++

    I did not really do this on purpose as I still did not figure out how it works. There was a window for tag and it tells me you have max of 5 and should be seperated by comma. I cut and pasted the code there but it took only the first line. Could you please be kind enought to explain to me how to send code using tag? Thanks.

    Qassim

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: Passing 2D array to and from a function in C++

    Quote Originally Posted by qabdullah View Post

    Could you please be kind enought to explain to me how to send code using tag?
    Could you please be smart enought to read what you had to before posting?
    Announcement: Before you post....
    Victor Nijegorodov

  6. #6
    Join Date
    Jul 2013
    Posts
    8

    Red face Re: Passing 2D array to and from a function in C++

    Code:
    #ifndef GUARD_Cb_n_h
    #define GUARD_Cb_n_h
    double** Cb_n(double roll, double pitch, double heading);
    #endif // !GUARD_Cb_n_h
    is this the correct use of tag? Thanks.

    Qassim

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,352

    Re: Passing 2D array to and from a function in C++

    Yes, this is the correct use of tags.
    But note, that you also have to set the proper indentations!
    It means that if you copy from your .cpp/.h file then it should be proper. But if you copied from your previous post where no code tags were used then you would get just the unformatted copy of the previous "text"
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center