How to Fill a 2D Matrix in C

Save

2D matrices are data structures that organize data in rows and columns. C uses the I,J subscript notation to access the elements of the matrix. Implementing controlled loops allows the programmer to access array elements one at a time to be filled out with data. Filling the 2D matrix data structure is a basic programming exercise that introduces the novice programmer to the concepts of matrices, loops and data types.

Design the Algorithm

  • Create a 2D matrix of 'N' elements. Choose the data to fill the matrix with.

  • Start a loop to move on the row from first to last row.

  • Nest a loop within the row loop to fill each first element on each column. Once a row is completely filled, the program will move to the next row.

  • Output the the 2D matrix on the screen to verify the fill.

  • The complete algorithm:

    Create a 2D matrix of 'N' elements.

    Choose the data to fill your matrix.

    Create a row loop

    Nest a column loop

    Output the 2D matrix

Coding the Program

  • Set up the program. The program will output the matrix to the screen, and the output function is implemented in the iostream library.

    This is the code:

    include <iostream>

    using namespace std;

    int main()

    {

  • Create our matrix 2D. In the example, the matrix will store integer numbers.

    This is the code:

    int matrix2d [10] [10];

  • Choose the data to fill the matrix. For the example use 3, although any integer will be valid.

    This is the code:

    int fill_number=3;

  • Create a row loop and nest a column loop within it.

    This is the code:

    for ( int i = 0; i == 9; i++ ) // Row Lop

    {

    for ( int j = 0; j == 9; j++ ) // Nested Column loop

    {

    matrix2d [i] [j] = fill_number;

  • Output the 2D matrix. The program will output the matrix at the same time the fill value is stored on the matrix.

    This is the code:

    cout << matrix2d [i][j]; //output the matrix

    }

    cout << endl;

    }

  • Finish your program by returning any value.

    return (0);

    }

  • This is the complete code:

    include <iostream>

    using namespace std;

    int main()

    {

    int matrix2d [10] [10];

    int fill_number=3;

    for ( int i = 0; i == 9; i++ ) // Row Lop

    {

    for ( int j = 0; j == 9; j++ ) // Nested Column loop

    {

    matrix2d [i] [j] = fill_number;

    cout << matrix2d [i][j]; //output the matrix

    }

    cout << endl;

    }

    return (0);

    }

Tips & Warnings

  • To change the fill type you might need to change the matrix data type on the declaration.

References

  • Photo Credit Ablestock.com/AbleStock.com/Getty Images
Promoted By Zergnet

Comments

You May Also Like

  • How to Create an Array in C

    Arrays offer the most efficient method for storing lists of data in C. They're very easy for the programmer to create and...

  • How To Create a Matrix Spreadsheet

    A matrix is a rectangle that has the ability to display numbers and/or words in an organized fashion. Each matrix is arranged...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!