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

2D array problem (matrix)

So i have a matrix. (n rows, m cols), and my program has to count the number of rows, where all the cols were >0. For eg. there are n cities, and m bird species. The program has to count the number of cities, where all the bird species (m) were found. (where every m is >0)

So far, i have this, but it's totally wrong:

int c=0;
    for(int i=0; i<m; ++i)
    {
        for(int j=0; j<n; ++j)
        if(bird[i][j]>0) c++;
    }
    cout<<c;


Could you please help me with this?

sparkthesunoff
Newbie Poster
2 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

Your inner loop is going to control the columns (it's a bit confusing because people usually specify dimensions as m rows by n columns, but you've done the opposite), so the inner (j loop) should go to m and your outer (i loop) loop should go to n.

So your loop hits the first row (i = 0)
Then goes and hits each of the columns (j = 0,1,2,..,m)
Second row (i = 1)
Then each of the m columns again
Etc.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

This article has been dead for over three months

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