CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2011
    Posts
    7

    maximum, minimum and average of a matrix?

    std::vector<std::vector<unsigned long> > matrix(15); // 15 rows
    typedef std::vector<std::vector<unsigned long> >::iterator it_type;

    it_type row = matrix.begin();

    for (int w=0;w<10;w++)
    {
    //some lines of code
    unsigned long num = x.to_ulong();
    cout <<"Decimal form is: " << num<< end;
    // if we've hit 15 items in the current row (i.e. 15 columns), then shift to the next row
    if (row->size() == 15)
    ++row;

    // add this to the next column in the current row
    row->push_back(num);
    }
    // resize the last row
    row->resize(15); // this will ensure that there are 15 columns in the last row


    for(size_t i=0; i < matrix.size(); ++i)
    {
    for(size_t j=0; j < matrix[i].size(); ++j)
    cout << matrix[i][j] << " ";
    cout << endl;
    }

    Now I want to find maximum, minimum and average of this matrix. What shall I do?

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: maximum, minimum and average of a matrix?

    It would be a lot easier to achieve if you were to wrap a 1D vector in a Matrix class.

    See http://www.parashift.com/c++-faq-lit...html#faq-13.10
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  3. #3
    Join Date
    Feb 2011
    Posts
    7

    Re: maximum, minimum and average of a matrix?

    what? how?

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: maximum, minimum and average of a matrix?

    A simple example.

    Code:
    #include <vector>
    #include <algorithm>
    
    class matrix
    {
    public:
    
        matrix()
            : n_columns(0),
              n_rows(0)
        {
        }
    
        matrix(int columns, int rows)
            : n_columns(columns),
              n_rows(rows),
              data(columns * rows)
        {
        }
    
        int operator ()(int row, int column)
        {
            return data[column + (row * n_columns)];
        }
    
        std::vector<int>::iterator begin()
        {
            return data.begin();
        }
    
        std::vector<int>::iterator end()
        {
            return data.end();
        }
    
    private:
    
        std::vector<int> data;
        int n_columns;
        int n_rows;
    };
    
    int main()
    {
        matrix m(10, 20);
    
        // Fill the matrix here.
    
        // Find the maximum value in the matrix.
        int maximum = *std::max_element(m.begin(), m.end()); 
    
        // Get element at 4,6
        int value = m(4, 6);
        
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #5
    Join Date
    Feb 2011
    Posts
    7

    Re: maximum, minimum and average of a matrix?

    thanks. but if i implement it this way, it gives lots of errors.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: maximum, minimum and average of a matrix?

    What errors?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: maximum, minimum and average of a matrix?

    Quote Originally Posted by JohnW@Wessex View Post
    What errors?
    I couldn't find any errors, however:

    Quote Originally Posted by JohnW@Wessex View Post
    A simple example.

    Code:
    class matrix
    {
        matrix(int columns, int rows)
            : n_columns(columns),
              n_rows(rows),
              data(columns * rows)
        {
        }
    ...
    private:
    
        std::vector<int> data;
        int n_columns;
        int n_rows;
    };
    You might want to be careful with your order of initialization.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: maximum, minimum and average of a matrix?

    Quote Originally Posted by monarch_dodra View Post
    You might want to be careful with your order of initialization.
    The constructor initialises all of the members with the constructor parameters, so everything should be fine. Now if I'd used 'n_rows' & 'n_columns' to initialise 'data', that would be different.
    I avoid dependencies of member declaration; it's too open to accidental error.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

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
  •  





Click Here to Expand Forum to Full Width

Featured