Login Register


C programmer?? filter_list
Author
Message
C programmer?? #1
write a program in C to print out the entries along the diagonal of a matrix and the sum.

Reply

RE: C programmer?? #2
Will we can't code the entire program for you but if you can provide us with the coding you've done maybe we'll be able to give you some headup
My Blog: http://www.procurity.wordpress.com
Donations: 1HLjiSbnWMpeQU46eUVCrYdbkrtduX7snG

Reply

RE: C programmer?? #3
I can give you the brief idea.
I hope you have already generated the following matrix by now[3x3 (m x m) for example].

[table]
[row]

[cell]a[0][0] [/cell]
[cell]a[0][1] [/cell]
[cell]a[0][2] [/cell]
[/row]
[row]
[cell]a[1][0] [/cell]
[cell]a[1][1] [/cell]
[cell]a[1][2] [/cell]
[/row]
[row]
[cell]a[2][0] [/cell]
[cell]a[2][1] [/cell]
[cell]a[2][2] [/cell]
[/row]
[/table]

i -> row and j -> column
The trick: print a[i][j] only when i=j or i+j==m-1
Hope you got it.
[Image: MUJ8qSW.png]
-----------------------------------
Now learning:
Android Development, Java
Working on:
An FTP Client for Android
-----------------------------------

Reply

RE: C programmer?? #4
hmmm thanks bro i got it

Reply

RE: C programmer?? #5
The solution by @alok9shm is of course correct, but I would add some info just for the sake of correctness.

Please note that, in C, the concept of matrix is vacous since the language does not provide such "type". Programmers can build something representing a matrix in different ways; the most common ones are:
  • array of arrays (which is the solution you were already given)
  • vectorized matrix
  • sparse matrix
If you have to deal with linear algebra, more often than not you will encounter the second one. Vectorization is the operation used to "convert" a matrix into a vector. This is particulary useful because it allows you to exploit data locality (while the array of arrays does not), in fact it is widely used in BLAS (Basic Linear Algebra Subprograms) and other deeply optimized libraries. However, it is a little harder to understand for a beginner.

See Wikipedia - Vectorization.

Reply







Users browsing this thread: