Dear all, is there is a way to go trough a matrix using just one "for" loop?, I mean I have this snippet:

#define WIDTH 5
#define HEIGHT 3

int mat [HEIGHT * WIDTH];
int n,m;

int main ()
{
  for (n=0;n<HEIGHT;n++)
    for (m=0;m<WIDTH;m++)
    {
      mat[n*WIDTH+m]=(n+1)*(m+1);
    }
  return 0;
}

But I was wondering if it is possible to go trough that pseudo-multidimensional array with one "for" loop. Actually I think that if the matrix is a square matrix may be is possible. Is it possible to

Any help would be appreciated, Thank you very much in advanced.

Is this what you are thinking of?

for( int i = 0; i < WIDTH*HEIGHT; i++ )
{
	int col = i % WIDTH;
	int row = i / WIDTH;
	mat[i]=(col+1)*(row+1);
}

Yes that is it! :D
Thanks!!!

I just did some changes to get woring with my code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.