matrix problem

This is a discussion on matrix problem within the C++ Programming forums, part of the General Programming Boards category; i am trying to make a matrix, and gradually make it more complex, currently i am running into the problem ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    matrix problem

    i am trying to make a matrix, and gradually make it more complex, currently i am running into the problem of it not actually making the matrix.

    Code:
    #include <vector>
    #include <iostream>
    #include <string>
    #include "input.h"
    using namespace std;
    
    void mak_matrix(int cols,int rows);
    
    void mak_matrix(int cols,int rows)
    {
       vector <vector<float> >matrix;
       vector<float>col;
       
       for(int i=0;i<cols;i++)
       {col.push_back(0.0);}
       for(int j=0;j<rows;j++)
       {matrix.push_back(col);}
       col.clear();
    }
    
    int main()
    {
       vector <vector<float> >matrix;
       vector<float>col;
       int cols=readInt("Number of columns is ",false);
       int rows=readInt("Number of rows is ",false);
       cout<<"done inputing data"<<endl;
       void mak_matrix(int cols,int rows);
       cout<<"done making matrix"<<endl;
       
       cout<<matrix[cols][rows]<<endl;
       cout<<"size of matrix"<<endl;
       
       matrix[0][0]=3.0;
       matrix[0][1]=2.0;
       matrix[1][0]=1.0;
       matrix[1][1]=3.0;
       matrix[2][0]=4.0;
       matrix[2][1]=6.0;
       cout<<"done input values"<<endl;
       
       cout<<matrix[0][0]<<" "<<matrix[0][1]<<endl;
       cout<<matrix[1][0]<<" "<<matrix[1][1]<<endl;
       cout<<matrix[2][0]<<" "<<matrix[2][1]<<endl;
       return 0;
    }
    it used to get to the inputing matrix values before i had it try to check the size of the matrix, I am not accustomed with trying to do things like this, any help is appreciated.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    You are making the matrix. And then throwing it away when mak_matrix() returns. You need some way to return the matrix you make into main. One possibility is to pass it in by reference.
    Code:
    void mak_matrix(vector<vector<float> >& matrix, int cols, int rows)
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    yea, that makes alot of sense, something which i knew, but was forgetting to use, thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting orientation and position in opengl
    By ting in forum Game Programming
    Replies: 9
    Last Post: 07-07-2008, 05:13 PM
  2. Matrix Inverse Problem
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 03-10-2008, 08:02 AM
  3. Music Programming - Serial Matrix Display (Help needed)
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-13-2007, 04:28 PM
  4. Music Programming - Serial Matrix Display
    By CrazyHorse in forum C Programming
    Replies: 1
    Last Post: 11-12-2007, 04:16 PM
  5. Replies: 3
    Last Post: 12-22-2004, 07:29 PM

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