954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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?

blessed87
Newbie Poster
2 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

Go through each element and keep track of the minimum and maximum values as you go. Also, add the element to a running sum and at the end divide by the number of elements.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
Go through each element and keep track of the minimum and maximum values as you go. Also, add the element to a running sum and at the end divide by the number of elements.


Load the first value of the matrix into your min and max variables first. That gives you the first 'guess' at the answer. After all, it could be...

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: