Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

help problem with functions in C++

  • 27-01-2003 6:28pm
    #1
    Registered Users, Registered Users 2 Posts: 34


    im trying to write a program using functions to creat a matrice manipulator but cant for the life of me figure out how you transfer 2-d array info from one function to another

    here's the code to read in the values
    MAX_ROW and MAX_column have been declared and limited to 10
    row and column are both got in separate functions




    void scan_matrix(int row,int column)
    {
    int matrix[MAX_ROW][MAX_COLUMN];

    for (int i=0; i<row; i++)
    for (int j=0; j<column; j++)
    {
    printf("enter value [%d] [%d]:",i,j);
    scanf("%d",&matrix[j]);

    while(matrix[j] < MIN || matrix[j] > MAX)
    {
    printf("\a\a\aERROR:choice outside possible range\n"
    "Please choose again for position [%d] [%d]:",i,j);
    scanf("%d",&matrix[j]);
    }
    }
    }




    but what i wanna know is how do you print out those values in matrix form
    e.g.
    a b c
    d e f
    g h i

    ?????????


Comments

  • Registered Users, Registered Users 2 Posts: 2,281 ✭✭✭DeadBankClerk


    I will post up a matrix class when i am finished it :D

    since i am programing it for a specific reason (3d graphics) i will be implementing the matrices as a 4x4 float array.


    Until then follow the white rabbit or take the red pill or something.*





    * May not be funny. :)


  • Registered Users, Registered Users 2 Posts: 2,281 ✭✭✭DeadBankClerk


    [php]

    // Early uncompiled untested hacked class...

    #ifndef __MATRIX__
    #define __MATRIX__

    class Matrix
    {
    private:
    float mData[4][4];

    public:
    Matrix (const float** data = 0);
    void load_identity (void);

    friend Matrix operator + (const Matrix& a, const matrix& b);
    friend Matrix operator - (const Matrix& a, const matrix& b);
    friend Matrix operator * (const Matrix& a, const matrix& b);
    float& operator () (const int& i, const int& j) const;

    };


    Matrix::Matrix (const float** data)
    {
    if (!data) // If no data is supplied
    {
    this->load_identity();
    }
    else
    {
    for (int i = 0; i < 4; i++)
    {
    for (int j = 0; j < 4; j++)
    {
    // Assign this from the array of data
    this->mData[j] = data[j];
    }
    }
    }
    }

    void Matrix::load_identity (void)
    {
    this->mData = { {1.0f, 0.0f, 0.0f, 0.0f},
    {0.0f, 1.0f, 0.0f, 0.0f},
    {0.0f, 0.0f, 1.0f, 0.0f},
    {0.0f, 0.0f, 0.0f, 1.0f} };

    }

    Matrix operator + (const Matrix& a, const matrix& b)
    {
    float data[4][4];
    for (int i = 0; i < 4; i++)
    {
    for (int j = 0; j < 4; j++)
    {
    data[j] = a(i,j) + b (i,j);
    }
    }
    return Matrix (data);
    }

    Matrix operator - (const Matrix& a, const matrix& b)
    {
    float data[4][4];
    for (int i = 0; i < 4; i++)
    {
    for (int j = 0; j < 4; j++)
    {
    data[j] = a(i,j) - b (i,j);
    }
    }
    return Matrix (data);
    }

    Matrix operator * (const Matrix& a, const matrix& b)
    {
    // i really couldnt be bothered to work out this function in all fairness
    return Matrix;
    }

    float& operator () (const int& i, const int& j) const
    {
    return this->mData[j];
    }


    #endif
    [/php]


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    how you transfer 2-d array info from one function to another

    [php]
    #include <iostream.h>
    
    void fn1(int*somearray,int type);
    
    int main(int argc,char**argv)
    {
     int la[2][3];
    
     fn1(la,6);
     return 0;
    };
    
    void fn1(int*somearray,int type)
    {
     switch(type)
     {
       case 0:
               cout<<"Array has no elements... thou art a muppet"<<endl;
               return;
      case 6:
               for(int a=0;a<6;a++)
                            cout<<"Array element "<<a<<" is ==> "<<somearray[a]<<endl; 
               return;
       default:
              cout<<"This function does not do anything other then iterate through zero or six arrary elements... deposit ten cents"<<endl;
              return;
     };
    return;
    };
    [/php]
    


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    There are a few different ways to pass arrays to functions, with differing degrees of safety, efficiency and ease-of-use.

    I would generally favour wrapping it in a class, and passing pointers or references to that class. Alternatively a handle-type class so that it would remain efficient if passed by value.

    This forces the array to be accompanied with information about it's size, the constructor and destructor code can prevent memory leaks, and checked operations can prevent buffer overflows.

    If you are using the matrix for mathematical operations then it would probably be best to use a slice_array from the STL. Since this is built on a valarray it is optomised for efficient mathematical processing.

    If you are using the matrix for other operations a vector of vectors, or else a class built on a vector using similar techniques to the STL slice_array would probably have the best balance.

    Beware of the temptation of using C-style arrays "for speed". In reality they aren't that much more efficient than vectors (in some cases less efficient), and unless you have speed requirements expressed in terms of processor cycles efficiency isn't really a reason to rule-out the C++ style.


  • Registered Users, Registered Users 2 Posts: 34 mozilla


    im still only learnin so thanx for all the suggestions im sure something will help


  • Advertisement
Advertisement