How to Get Elements of a Matrix in C
A matrix is a bi-dimensional array. In C, a matrix is created and accessed by the use of subscript operators; for example, a 2-by-3 matrix will be matrix[2][3]. The use of subscript operators allows access to any element of the matrix, both systematically (by using a loop) and on demand (by specifying the position of an element). Getting the elements of a matrix is a useful exercise to practice loops and subscript operations.
Instructions
-
-
1
Set up a program to create a matrix. Use the "iostream" library to output the number on the matrix to the computer screen. Enter the code:
#include<iostream>
using namespace std;
int main()
{
-
2
Declare variables. The example will use one variable to store a 3-by-4 matrix. Matrix sizes are stored in [ i ] [ j ] notation. Add this line of code to the program:
int mymatrix[3][4];
-
-
3
Initialize the matrix to any integer values. Use two nested "for" loops to initialize the values for the matrix elements. The subscripts range from zero to the value minus one. For the variable mymatrix[3][4], the first subscript [3] will range from zero to two, and the second subscript [4] ranges from zero to three. Enter the code:
for (int cx=0; cx == 2; cx++) // access the first subscript,
for ( int cy=0; cy == 3; cy++) // access the second subscript,
mymatrix[cx][cy]=(cx+2)*(cy+2) //we use cx+2 and cy+2, to avoid zeroes on the matrix.
cout << endl;
-
4
Use another set of nested loops to get the contents of the matrix. The loops are similar, but the statement they execute will output the contents of the matrix (using the "cout" command) instead of initializing them (using the "=" operator, on Step 3).
for( int ox=0; ox == 2; ox++)// again access to the first and second subscript
for( int oy=0; oy == 3; oy++)
cout << mymatrix[ox][oy] << " "; //outputs the elements of the matrix, adding a space after each element
cout << endl;
-
5
Put numbers on the subscript of the matrix to get elements of the matrix on demand. Make sure that the numbers on the subscript are within the ranges of the matrix size. Add the following lines to the code:
cout << mymatrix[1][2] << " " << mymatrix[3][1]; //outputs positions 1,2 and 3,1 from the matrix.
return(0);
}
-
6
Copy and paste the complete code to compile and execute the program.
#include<iostream>
using namespace std;
int main()
{
int mymatrix[3][4];
for (int cx=0; cx == 2; cx++) // access the first subscript,
for ( int cy=0; cy == 3; cy++) // access the second subscript,
mymatrix[cx][cy]=(cx+2)*(cy+2) //we use cx+2 and cy+2, to avoid zeroes on the matrix.
cout << endl;
for( int ox=0; ox == 2; ox++)// again access to the first and second subscript
for( int oy=0; oy == 3; oy++)
cout << mymatrix[ox][oy] << " "; //outputs the elements of the matrix, adding a space after each element
cout << endl;
cout << mymatrix[1][2] << " " << mymatrix[3][1]; //outputs positions 1,2 and 3,1 from the matrix.
return(0);
}
-
1
References
- Photo Credit Ablestock.com/AbleStock.com/Getty Images