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++

Threaded View

  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 10:04 AM.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center