• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Introduction to 2 Dimensional Arrays

markokitch

Ad Reach Optimizer
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
131
Likes
155
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 900 XP
2 Dimensional Arrays

In this part you will learn:
1. C syntax
2. 2D arrays
3. Nested for loops
4. Showing output

In this tutorial I will teach you about 2D arrays. 2D arrays can be called array of arrays. First we had a single array that was in just one single dimension and we accessed its elements by writing the index in the subscript. In 2d arrays we have arrays that go in both direction. So now we access its elements by mentioning the row number as well as the column number in the subscript separately.
Matrix Operation
Basic Step:
Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>

These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. int

    main(

    )
  2. {
  3. int

    matrix[

    100

    ]

    [

    100

    ]

    ;

  4. int

    rows,

    columns;
  5. printf

    (

    "Enter the number of rows:"

    )

    ;
  6. scanf

    (

    "%d"

    ,&

    rows)

    ;

  7. printf

    (

    "Enter the number of columns:"

    )

    ;
  8. scanf

    (

    "%d"

    ,&

    columns)

    ;

  9. printf

    (

    "Please enter the elements of matrix:\n

    "

    )

    ;

  10. int

    i,

    j;
  11. for

    (

    i=

    0

    ;

    i<

    rows;

    i++

    )
  12. {
  13. for

    (

    j=

    0

    ;

    j<

    columns;

    j++

    )
  14. {
  15. scanf

    (

    "%d"

    ,&

    matrix[

    i]

    [

    j]

    )

    ;
  16. }

  17. }

In this program we declared a 2d array of integers. We will treat it like a matrix in mathematics. This is because the concept of 2d arrays can be very well understood by with the implementation of matrixes. We will calculate the sum of diagonals of matrix separately in this program.
At first we declared a 2d array of integers with 100 rows and 100 columns. The first number in the subscript is always the number of rows of the matrix and the second in the subscript is the total column number. Then we took input from the user the number of rows and columns. This is because we want the user to tell us how big he wants to build a matrix.
For this program the user should enter same number of rows and columns because the diagonal sum can only be calculated of square matrix. Then we wrote a nested for loop to take input the elements of the matrix.
The first for loop is to shift the rows of the matrix and the second or the inner for loop is to iterate between the columns of the matrix. For every row of the matrix the inner for loop runs column times so we can enter the value of each element of the matrix.
  1. printf

    (

    "The matrix you entered\n

    "

    )

    ;
  2. int

    k,

    l;
  3. for

    (

    k=

    0

    ;

    k<

    rows;

    k++

    )
  4. {
  5. for

    (

    l=

    0

    ;

    l<

    columns;

    l++

    )
  6. {
  7. printf

    (

    "%d"

    ,

    matrix[

    k]

    [

    l]

    )

    ;
  8. printf

    (

    " "

    )

    ;
  9. }
  10. printf

    (

    "\n

    "

    )

    ;

  11. }

Now after taking input all the elements of the matrix we are printing them using the similar nested for loops but this time we are printing the value of the elements columnwise.
  1. int

    sum=

    0

    ,

    m;

  2. for

    (

    m=

    0

    ;

    m<

    rows;

    m++

    )
  3. {
  4. sum=

    sum+

    matrix[

    m]

    [

    m]

    ;
  5. }
  6. printf

    (

    "\n

    Sum of the left diagonal is:%d"

    ,

    sum)

    ;

In this for loop we are calculating the sum of the principal diagonal or the left diagonal of matrix. What we are doing is we are running a single for loop that is adding all the elements of the matrix with same row and column number. Then after the loop we are printing the value of the sum of the diagonal.
  1. j=

    columns-

    1

    ;
  2. sum=

    0

    ;
  3. for

    (

    m=

    0

    ;

    m<

    rows &&

    j>=

    0

    ;

    m++,

    j--

    )
  4. {
  5. sum=

    sum+

    matrix[

    m]

    [

    j]

    ;
  6. }
  7. printf

    (

    "\n

    Sum of the right diagonal is:%d"

    ,

    sum)

    ;
  8. getch

    (

    )

    ;

  9. }

In this part we are calculating the sum of the right diagonal of the matrix. For this we have to increase the row number while decreasing the column number. For this we initialized a variable j with column -1 so that it becomes the last index of the column. The rest of the program is same. We accumulated the sum in a single variable called sum and then at the end we printed the value.
Execute > compile
then Execute > run
Output
matrix.png


Book traversal links for Introduction to 2 Dimensional Arrays

  • ‹ Introduction to Arrays
  • Up
  • Introduction to Functions ›


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,500

350,885

350,895

Top