Thread: how to create a 3 x 3 matrix

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    1

    how to create a 3 x 3 matrix

    how do i create a 3 x 3 matrix and find the sum of elements in all rows?

  2. #2
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    What have you tried so far?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    You could declare the matrix like this (assuming the elements are of type int):

    Code:
    int matrix[3][3];
    Then, you would have to initialize the elements of the matrix so that they have the values you want; in order to do that, you could use an external for loop to select, for example, a row, and then an internal for loop, to select a column. To sum all the elements from the matrix and store the result in a single variable, you can declare that variable first, and, as you assign a value to an element, add the value of that element to the variable you declared to hold the sum. In the end, the variable will have the sum of all elements of the matrix. You can also, alternatively, create the two nested for loops again and make the summation, after the for loops to assigning values are finished.

    Regards!
    Last edited by stdq; 05-16-2012 at 02:19 PM.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    9
    agree with stdq ,use two-dimensional array

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-25-2009, 04:25 AM
  2. Create Sparse Matrix
    By kavka3 in forum C Programming
    Replies: 12
    Last Post: 02-08-2009, 03:23 PM
  3. How to create a matrix(movie) animation in C
    By yeahmen in forum C Programming
    Replies: 2
    Last Post: 11-24-2008, 11:38 PM
  4. How to create a matrix in the heap....
    By Tyrant in forum C Programming
    Replies: 10
    Last Post: 12-02-2007, 11:10 PM
  5. Create a matrix with user-defined dimensions
    By EliasK in forum C Programming
    Replies: 2
    Last Post: 04-08-2003, 07:09 AM