# Thread: how to create a 3 x 3 matrix

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. What have you tried so far?

3. 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!

4. agree with stdq ,use two-dimensional array