Hi,

I am new to C++.. and programming too...

how do I read only diagonal elemets in an array..??

thanks for the response
What is a diagonal element in an array?

Or do you mean a 2D array?
yep...I am calling a matrix as an array...I just only need to read (1,1) (2,2) (3,3).......(m,m) elements of an array

thanks
You just answered your own question.
:)

You will need to read everything and check if x-coordinate == y-coordinate.
Maybe a single for loop is what you need...
You may try the following :
--------------------------------------------------------------------
#define MATRIX_SIZE 10
int main(int argc, char *argv[])
{
int matrix[MATRIX_SIZE][MATRIX_SIZE];
int i;
.....
.....
for(i = 0 ; i < MATRIX_SIZE ; i++)
{
printf("%d\n", matrix[i][i]);
}
}
--------------------------------------------------------------------
The code above prints all the diagonal elemets in a square matrix to
the screen.
Of course, you have to make sure that the matrix elements are valid
before you attempt to read the elements you want.
Last edited on
Topic archived. No new replies allowed.