C Programming Tutorial

  • Two Dimensional Array in C

Last updated on July 27, 2020

Two-dimensional Array #

The syntax declaration of 2-D array is not much different from 1-D array. In 2-D array, to declare and access elements of a 2-D array we use 2 subscripts instead of 1.

Syntax: datatype array_name[ROW][COL];

The total number of elements in a 2-D array is ROW*COL . Let’s take an example.

This array can store 2*3=6 elements. You can visualize this 2-D array as a matrix of 2 rows and 3 columns.

two dimensional array assignment c

The individual elements of the above array can be accessed by using two subscript instead of one. The first subscript denotes row number and second denotes column number. As we can see in the above image both rows and columns are indexed from 0 . So the first element of this array is at arr[0][0] and the last element is at arr[1][2] . Here are how you can access all the other elements:

arr[0][0] - refers to the first element arr[0][1] - refers to the second element arr[0][2] - refers to the third element arr[1][0] - refers to the fourth element arr[1][1] - refers to the fifth element arr[1][2] - refers to the sixth element

If you try to access an element beyond valid ROW and COL , C compiler will not display any kind of error message, instead, a garbage value will be printed. It is the responsibility of the programmer to handle the bounds.

arr[1][3] - a garbage value will be printed, because the last valid index of COL is 2 arr[2][3] - a garbage value will be printed, because the last valid index of ROW and COL is 1 and 2 respectively

Just like 1-D arrays, we can only also use constants and symbolic constants to specify the size of a 2-D array.

Processing elements of a 2-D array #

To process elements of a 2-D array, we use two nested loop. The outer for loop to loop through all the rows and inner for loop to loop through all the columns. The following program will clear everything.

Expected Output:

How it works:

There is nothing new in this previous program that deserves any explanation. We are just using two nested for loops. The first nested for loop takes input from the user. And the second for loop prints the elements of a 2-D array like a matrix.

Initializing 2-D array #

Initialization of 2-D array is similar to a 1-D array. For e.g:

two dimensional array assignment c

After this initialization, each element is as follows:

Consider another initialization.

The size of my_arr is 4*3=12 , but in the initialization, we have only specified the value of 8 elements. In such cases, the remaining elements will be given the value of 0 .

The individual elements are as follows:

In 2-D arrays, it is optional to specify the first dimension but the second dimension must always be present. This works only when you are declaring and initializing the array at the same time. For example:

As discussed earlier you can visualize a 2-D array as a matrix. The following program demonstrates the addition of two matrices.

Two matrices can be added or subtracted, only if they have the same dimension. In other words, a matrix of size 2*3 can be added to another matrix of 2*3, but you can’t add or subtract it to a matrix of 2*4 or 3*2 . The resultant array will be a matrix of the same dimension as the original two. First two for loops asks the user to enter two matrices. The third for loop adds corresponding elements of mat1 and mat2 in a new array mat3 . Fourth for loop prints the elements of array mat3 .

Arrays of more than two dimension #

You can even create an array of 3 or more dimensions or more, but generally, you will never need to do so. Therefore, we will restrict ourself to 3-D arrays only.

Here is how you can declare an array of 3 dimensions.

3-D array uses three indexes or subscript. This array can store 2*3*2=12 elements.

Here is how to initialize a 3-D array.

You can think of this array as 2 2-D arrays and each of these 2-D array has 3 rows and 4 columns;

Here are individual elements of the array:

Passing Multidimensional Arrays to Functions #

You can pass multi-dimensional arrays to functions just like a 1-D array, but you need to specify the size of the all other dimensions except the first one. For e.g:

If you need to pass arr[2][3] to a function called func_1() , then you need to declare the func_1() like this:

or like this:

It would be invalid to declare formal argument as follows:

Similarly to pass a 3-D array you need to declare the function as follows:

Load Comments

  • Intro to C Programming
  • Installing Code Blocks
  • Creating and Running The First C Program
  • Basic Elements of a C Program
  • Keywords and Identifiers
  • Data Types in C
  • Constants in C
  • Variables in C
  • Input and Output in C
  • Formatted Input and Output in C
  • Arithmetic Operators in C
  • Operator Precedence and Associativity in C
  • Assignment Operator in C
  • Increment and Decrement Operators in C
  • Relational Operators in C
  • Logical Operators in C
  • Conditional Operator, Comma operator and sizeof() operator in C
  • Implicit Type Conversion in C
  • Explicit Type Conversion in C
  • if-else statements in C
  • The while loop in C
  • The do while loop in C
  • The for loop in C
  • The Infinite Loop in C
  • The break and continue statement in C
  • The Switch statement in C
  • Function basics in C
  • The return statement in C
  • Actual and Formal arguments in C
  • Local, Global and Static variables in C
  • Recursive Function in C
  • One dimensional Array in C
  • One Dimensional Array and Function in C
  • Pointer Basics in C
  • Pointer Arithmetic in C
  • Pointers and 1-D arrays
  • Pointers and 2-D arrays
  • Call by Value and Call by Reference in C
  • Returning more than one value from function in C
  • Returning a Pointer from a Function in C
  • Passing 1-D Array to a Function in C
  • Passing 2-D Array to a Function in C
  • Array of Pointers in C
  • Void Pointers in C
  • The malloc() Function in C
  • The calloc() Function in C
  • The realloc() Function in C
  • String Basics in C
  • The strlen() Function in C
  • The strcmp() Function in C
  • The strcpy() Function in C
  • The strcat() Function in C
  • Character Array and Character Pointer in C
  • Array of Strings in C
  • Array of Pointers to Strings in C
  • The sprintf() Function in C
  • The sscanf() Function in C
  • Structure Basics in C
  • Array of Structures in C
  • Array as Member of Structure in C
  • Nested Structures in C
  • Pointer to a Structure in C
  • Pointers as Structure Member in C
  • Structures and Functions in C
  • Union Basics in C
  • typedef statement in C
  • Basics of File Handling in C
  • fputc() Function in C
  • fgetc() Function in C
  • fputs() Function in C
  • fgets() Function in C
  • fprintf() Function in C
  • fscanf() Function in C
  • fwrite() Function in C
  • fread() Function in C

Recent Posts

  • Machine Learning Experts You Should Be Following Online
  • 4 Ways to Prepare for the AP Computer Science A Exam
  • Finance Assignment Online Help for the Busy and Tired Students: Get Help from Experts
  • Top 9 Machine Learning Algorithms for Data Scientists
  • Data Science Learning Path or Steps to become a data scientist Final
  • Enable Edit Button in Shutter In Linux Mint 19 and Ubuntu 18.04
  • Python 3 time module
  • Pygments Tutorial
  • How to use Virtualenv?
  • Installing MySQL (Windows, Linux and Mac)
  • What is if __name__ == '__main__' in Python ?
  • Installing GoAccess (A Real-time web log analyzer)
  • Installing Isso
  • Now Trending:
  • How do I check if a list...
  • How do I concatenate two...
  • How to find the index of...
  • How to make a dictionary...

2D Arrays in C language – How to declare, initialize and access elements

In this C programming tutorial, we will discuss how to declare, initialize, access, and iterate over two-dimensional arrays and implement a program using 2D arrays.

We already know that arrays are a collection of the same type of data that have a fixed size (in C programming language as in other languages we can increase the size of an array at runtime).

Arrays can also be classified based on their dimensions, like:

  • 1D arrays or one-dimensional array
  • 2D arrays or two-dimensional arrays
  • 3D arrays or three-dimensional arrays
  • and so on…

In this tutorial, we will learn more about the 2D array or two-dimensional arrays.

Table of Contents

1. What is a 2D array in C?

A 2D array is like a matrix and has a row and a column of elements ( Although in memory these are stored in contiguous memory locations) .

A 1-D array, as we saw in the previous tutorial, is a linear list of data and needed only one index to access the element like a[2]. To access a two-dimensional array we need a pair of indices one for the row and another for the column like a[1][2].

Similar to a one-dimensional array, we have the same name for all the elements present in the matrix.

The difference that we have here is that a two-dimensional array is not linear in nature.

The following figure illustrates the difference between a one-dimensional array and a two-dimensional array:

2d-arrays

In the above figure, we can clearly see that the 2D array has two dimensions just like any two-dimensional figure such as a square or a rectangle.

Also, the number of rows and the number of columns in the 2D array are represented by this format:

For the above 2D array we have the number of rows= 3 and number of columns= 3 , so we represent it as  a 3×3 array or a 3×3 matrix.

A matrix can also have specifications like 3×4 , 2×1 , etc. Let us learn more about the 2D arrays .

2. How to declare a 2D Array in C?

A 2D array needs to be declared so that the compiler gets to know what type of data is being stored in the array.

Similar to 1D array, a 2D array can also be declared as an  int , char , float , double , etc. Here is how we declare a 2D array (here integer array):

The ‘ int’ specifies that the data stored in the array will be of integer type. ‘num’ is the variable name under which all the data is stored. [ 10 ] refers to the number of rows of the array and [ 5 ] refers to the number of columns of the array.

This is also a static memory allocation, that is, we are allocating the array a size equal to 10 x 5 , that is, in this array, we can store 10 x 5 = 50 number of elements.

The actual size it will occupy in memory will depend on the data type of the array i.e. ( size of array = number of elements it can hold x Size of datatype ).

The two brackets i.e. [][] specify that the array is two-dimensional.

3. How to initialize and store data in a 2D array in C?

2D array initialization can be done while declaring the array as well. In simple words, we can store certain elements in the array while writing the program i.e. we know which values this array will always store.

Here are a few examples of initializing a 2D array :

Another important fact is that when initializing a 2D array , specifying the number of rows of the array is optional but specifying the number of columns of the array is important and mandatory .

Like a 1D array , the user can also input the values in the array using a for loop . The only difference is that we use a nested for loop for inserting the elements in a 2D array.

There are two ways of insertion: row-wise insertion and column-wise insertion . The typical way of insertion is row-wise insertion .

Here is an example of row-wise insertion format (for a 3×3 matrix):

4. How to access and read data in a 2D array in C?

In case of 2D arrays , we use index numbers(like 1D arrays) in the subscript to access the array elements. The outer loop indicates the row index and the inner loop indicates the column index.

The following figure shows how the array elements are indexed:

2d-array-indexing

We already see that the index format is [row number][column number] . Suppose we need to access the element in row 1 and column 2 , we just need to write arrayName[1][2] .

In order to access all the array elements, we use nested for loops. Here is the syntax to access all the array elements in a matrix format (for a 3×3 matrix):

5. Program to initialize 2D array with User input and print it

Here is a simple program of 2D array which adds two arrays and stores the result in another array. One array has already been initialized and the other one will have data input by the user.

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples . All examples are hosted on Github .

Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

guest

Thnks for information

  • Algorithm Tutorials
  • C Programming Examples
  • C Programming tutorials
  • Complete Python Tutorials – Beginner to Advanced
  • Data Structure Tutorials
  • Python Programming Examples – Basic to Advanced
  • Subscribe to our Newsletter !!

PrepBytes Blog

ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING

Sign in to your account

Forgot your password?

Login via OTP

We will send you an one time password on your mobile number

An OTP has been sent to your mobile number please verify it below

Register with PrepBytes

Two dimensional array in c.

' src=

Last Updated on December 28, 2023 by Ankit Kochar

two dimensional array assignment c

Two-dimensional arrays are fundamental data structures in programming languages like C, allowing the storage and manipulation of data in a grid-like format. They are an arrangement of elements in rows and columns, forming a matrix structure that enables efficient organization and access to data. In C, two-dimensional arrays are extensively used to work with matrices, images, tables, and more, providing a versatile way to handle data in a structured manner. This article delves into the fundamentals of two-dimensional arrays in C, explaining their syntax, usage, and common operations

What is a 2D Array?

A two dimensional array in c is one type of array. The 2d array is an array that is organized in rows and columns. We can identify each element in a 2d array using the position of rows and columns, we also call them indices. For example, in a 3×3 2D array, the element in the second row and third column would be accessed using the indices (1, 2) or row 1, column 2.

Syntax of 2D Array

In this blog section, we will explain the syntax of the two dimensional array in c.

  • data_type: It will tell the type of data that has to be stored in each element.
  • array_name: It will tell the name of the array with which it will be referenced in the whole program.
  • m: It is the number of rows.
  • n: It is the number of columns.

The array will have a total of m x n elements. For example, the 2D array in c of 20 rows and 10 columns will be declared like this.

Declaration and Initialization of Two Dimensional Array in C

We can declare the 2D array in c by using two methods.

  • Using Initializer List
  • Using Loops

Initializing using Initializer List In the initializer list we have two methods to declare the two dimensional array in C.

First Method In the first method we can write all the elements together as shown in the below example.

Here the array contains 4 rows and 3 columns. We read the elements in the matrix from left to right so in this also the elements are read from left to right so after every 3 elements we go to the new row, and the new row starts to be filled.

Second Method This method is comparatively better as we have clearly specified the elements in each row. Let’s look at the example shown below.

In the above example we have nested brackets which shows the element of each row individually.

Initializing Two Dimensional Array in C using Loops We can declare the array as normal after that we can initialize them with the help of loops.

Example of two-dimensional array in C Let’s understand this better with the help of an example.

In the above example we have declared the array with of size m x n and we have initialized it by using loops.

Accessing the Elements of the 2D Array in C

You can access the elements of Two dimensional array in C using the rows and columns or indices shown below.

  • x: is the row index.
  • y: is the column index

Pointers and 2D Array in C

We can use the pointers for accessing the elements of the array. The single pointer points to an entire 1D array so to use the pointer in 2D array we have to create a new pointer for every row.

Example Let’s understand the relations between pointers and 2D array with the help of an example.

Code Implementation

Explanation of the above example Here we have used pointers to traverse in the row and for every new row we have created a new pointer.

Examples of Two Dimensional Array in C

In this section, we will discuss various examples of two dimensional arrays in c.

Example 1: Taking the Input from the user and printing it. In the above example, we will see the implementation of the above-mentioned example.

Explanation of the above example In the above example we have declared an array of size 3 x 3. Then we have asked the user to give the input after receiving each input we have shown the matrix in the output.

Example 2: Summation in Two Dimensional Array in C Now we will discuss the implementation of the above-mentioned example.

Explanation of the above example In the above example we have taken two matrixes as input and then have added the two matrixes and printed the resulting matrix as output.

Common Applications of 2D Arrays

2D arrays in c have many applications that make them so useful. Some of the applications are explained below.

  • Storing and manipulating images or other graphical data.
  • Manipulating and storing matrices in linear algebra computations.
  • Implementing board games like checkers and chess.
  • Manipulating and representing data in spreadsheet programs.

Conclusion Two-dimensional arrays in C serve as powerful tools for handling structured data efficiently. Understanding their syntax, declaration, initialization, and manipulation is crucial for developing applications that involve matrices, tables, and grid-based datasets. Mastery of these arrays empowers programmers to efficiently work with multidimensional data structures, facilitating various computational tasks and data processing operations in C programming.

FAQs (Frequently Asked Questions) about Two-Dimensional Arrays in C:

Below are some of the frequently asked questions on two dimensional arrays.

1. How do you declare a two-dimensional array in C? To declare a two-dimensional array in C, you specify the data type of the elements and the size of rows and columns using the syntax: data_type array_name[rows][columns];.

2. How are elements accessed in a two-dimensional array? Elements in a two-dimensional array are accessed using indices for rows and columns, like array_name[row_index][column_index];.

3. Can the size of rows and columns in a two-dimensional array be dynamic in C? In C, the size of arrays is determined at compile time. However, you can simulate dynamic behavior using pointers and dynamic memory allocation.

4. How can you initialize a two-dimensional array in C? You can initialize a two-dimensional array during declaration by providing the initial values in a nested brace-enclosed list for rows and columns, such as int array[2][3] = {{1, 2, 3}, {4, 5, 6}};.

5. What are the common operations performed on two-dimensional arrays in C? Common operations include traversing the array elements, performing matrix operations (addition, multiplication, etc.), finding specific elements, and manipulating data within the array.

6. Can a two-dimensional array store elements of different data types in C? No, in C, all elements within an array must be of the same data type.

7. How is memory allocated for a two-dimensional array in C? Memory for a two-dimensional array in C is allocated in a contiguous block, calculated as the product of rows and columns multiplied by the size of the data type.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Save my name, email, and website in this browser for the next time I comment.

  • Linked List
  • Segment Tree
  • Backtracking
  • Dynamic Programming
  • Greedy Algorithm
  • Operating System
  • Company Placement
  • Interview Tips
  • General Interview Questions
  • Data Structure
  • Other Topics
  • Computational Geometry
  • Game Theory

Related Post

Null character in c, assignment operator in c, ackermann function in c, median of two sorted arrays of different size in c, number is palindrome or not in c, implementation of queue using linked list in c.

C Functions

C structures, c multidimensional arrays, multidimensional arrays.

In the previous chapter, you learned about arrays , which is also known as single dimension arrays . These are great, and something you will use a lot while programming in C. However, if you want to store data as a tabular form, like a table with rows and columns, you need to get familiar with multidimensional arrays .

A multidimensional array is basically an array of arrays.

Arrays can have any number of dimensions. In this chapter, we will introduce the most common; two-dimensional arrays (2D).

Two-Dimensional Arrays

A 2D array is also known as a matrix (a table of rows and columns).

To create a 2D array of integers, take a look at the following example:

To create a two-dimensional array of integers, add each array within its own set of curly braces:

numbers is now an array with two arrays that contains three integers each.

The first dimension represents the number of rows [2] , while the second dimension represents the number of columns [3] . The values are placed in row-order, and can be visualized like this:

To visualize the example above, think of the numbers array as a table with two rows , where each row has three columns :

two dimensional array assignment c

Access the Elements of a 2D Array

To access an element of a two-dimensional array, you must specify the index number of both the row and column.

This statement accesses the value of the element in the first row (0) and third column (2) of the matrix array.

Remember that: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

Change Elements in a 2D Array

To change the value of an element, refer to the index number of the element in each of the dimensions:

The following example will change the value of the element in the first row (0) and first column (0) :

Loop Through a 2D Array

To loop through a multi-dimensional array, you need one loop for each of the array's dimensions.

The following example outputs all elements in the matrix array:

Good To Know

Omit first dimension size.

Sometimes, when looking at other people's code, you will see that the size of the first dimension of a multidimensional array is not specified. This is valid, as C is smart enough to understand how big it is by looking at the values inside the curly braces. However, you cannot omit the size of other dimensions.

A Three-Dimensional Array

An example on how to declare a three-dimensional array:

When To Use Multidimensional Arrays

Multi-dimensional arrays are great at representing grids.

Get Certified

COLOR PICKER

colorpicker

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

[email protected]

Top Tutorials

Top references, top examples, get certified.

  • Interview Problems on Matrix
  • Practice Matrix
  • MCQs on Matrix
  • Tutorial on Matrix
  • Matrix Traversal
  • Sorting in Matrix
  • Matrix Rotation
  • Transpose of Matrix
  • Inverse of Matrix
  • Determinant of Matrix
  • Matrix Application
  • Adjoint & Inverse Matrix
  • Sparse Matrix
  • Matrix Exponentiation

Related Articles

  • Solve Coding Problems
  • Program to Interchange or Swap Columns in a Matrix
  • Applications, Advantages and Disadvantages of Matrix Data Structure
  • Number of times path is crossed
  • C Program for Program to Interchange Diagonals of Matrix
  • What is Matrix in Data Structure?
  • Removing trailing newline character from fgets() Input
  • Add and Remove vertex in Adjacency Matrix representation of Graph
  • How does C allocate memory of data items in a multidimensional array?
  • Generic Linked List in C
  • Find all adjacent elements of given element in a 2D Array or Matrix
  • Add Matrix in C
  • Minimum queens required to cover all the squares of a chess board
  • Implementing Bit Matrix in Java
  • Java Program to Display Lower Triangular Matrix
  • C Program for Kronecker Product of two matrices
  • Postmaster Letters Collection
  • Matrix Index Validation and Update
  • Difference Between Call by Value and Call by Reference in C
  • Count submatrices having only 'X' in given Matrix

How to declare a Two Dimensional Array of pointers in C?

A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element.

How to create a 2D array of pointers:

A 2D array of pointers can be created following the way shown below.

int *arr[5][5];        //creating a 2D integer pointer array of 5 rows and 5 columns.

The element of the 2D array is been initialized by assigning the address of some other element. In the example, we have assigned the address of integer variable ‘n’ in the index (0, 0) of the 2D array of pointers.

int n;                       //declared a variable arr[0][0] = &n;        //assigned a variable at position (0, 0)

Below is the implementation of the 2D array of pointers.

A two-dimensional array of pointers can also be created using Dynamic Memory Allocation.

We can use the malloc() function to dynamically allocate memory.

Below is the implementation of a 2D array of pointers using Dynamic Memory Allocation.

Please Login to comment...

  • germanshephered48
  • ishankhandelwals
  • 10 Best ChatGPT Prompts for Lawyers 2024
  • What is Meta’s new V-JEPA model? [Explained]
  • What is Chaiverse & How it Works?
  • Top 10 Mailchimp Alternatives (Free) - 2024
  • Dev Scripter 2024 - Biggest Technical Writing Event By GeeksforGeeks

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IncludeHelp_logo

  • Data Structure
  • Coding Problems
  • C Interview Programs
  • C++ Aptitude
  • Java Aptitude
  • C# Aptitude
  • PHP Aptitude
  • Linux Aptitude
  • DBMS Aptitude
  • Networking Aptitude
  • AI Aptitude
  • MIS Executive
  • Web Technologie MCQs
  • CS Subjects MCQs
  • Databases MCQs
  • Programming MCQs
  • Testing Software MCQs
  • Digital Mktg Subjects MCQs
  • Cloud Computing S/W MCQs
  • Engineering Subjects MCQs
  • Commerce MCQs
  • More MCQs...
  • Machine Learning/AI
  • Operating System
  • Computer Network
  • Software Engineering
  • Discrete Mathematics
  • Digital Electronics
  • Data Mining
  • Embedded Systems
  • Cryptography
  • CS Fundamental
  • More Tutorials...
  • 📚Tech Articles
  • 🤩Full Forms
  • ☕Code Examples
  • 📰Guest Post
  • ⌨Programmer's Calculator
  • 🥰XML Sitemap Generator
  • 🥰Tools & Generators

IncludeHelp

Home » C programs

C language Two Dimensional (Matrix) solved programs/examples

A two-dimensional array is an array of arrays that has two values 1) number of rows and 2) number of columns in each row. It can be considered as a matrix with rows and columns.

Syntax to declare a two-dimensional array in C,

This section contains solved C programs on two-dimensional arrays , practice these programs to learn the concept of array of arrays or two-dimensional array (matrix) in C language. Each program has solved code, output, and explanation.

List of C Two-dimensional Arrays Programs

  • C Program to Read and Print a RxC Matrix, R and C must be input by the User This program will read a two dimensional array (Matrix), number of rows (R) and number of columns (C) will be read through the User.
  • C Program to Read a Matrix and find Sum and Product of all elements This program will read a matrix and prints sum and product of all elements of the two dimensional array.
  • C Program to find Sum of all elements of each row of a matrix This C program will read a Matrix (two dimensional arrays) and print the sum of all elements of each row.
  • C Program to Transpose a Matrix This C program will read a matrix and print its transpose matrix.
  • C Program to Read a Matrix and Print Diagonals This C program will read a matrix of MxN dimensions and prints only diagonal’s elements of the matrix.
  • C Program to find sum and subtraction of two matrices This C program will read two matrices and find their sum (addition) and subtraction, in addition matrix addition of both matrixes’ elements will be assigned and in the subtraction matrix, subtraction of both matrixes’ elements will be assigned.
  • C Program to find multiplication of two matrices This C program will read two matrices and make a third matrix, which will contain the multiplication of both input matrices.
  • C Program to print lower diagonal of a matrix This C program will read a square matrix and print its lower diagonal.
  • C program for matrix multiplication using recursion Given two matrices, we have to find their multiplication using the recursion.
  • C program to check two matrices are identical or not Given two matrices, we have to check whether they are identical or not using C program.
  • C program to check a given matrix is an identity matrix or not Given a matrix, we have to check whether the matrix is an identity matrix or not using C program.
  • C program to check a given matrix is a sparse matrix or not Given a matrix, and we have to check whether the matrix is a sparse matrix or not using C program.
  • C program to interchange the rows in the matrix Given a matrix, and we have to interchange the specified rows in the matrix using C program.
  • C program to interchange the columns in the matrix Given a matrix, and we have to interchange the specified columns using C program.
  • C program to arrange row elements in ascending order Given an array, we have to arrange the row elements in ascending order using C program.
  • C program to arrange column elements in ascending order Given a matrix, we have to arrange column elements in ascending order using C program.
  • C program to find the frequency of even numbers in matrix Given a matrix, we have to find the frequency of even numbers in matrix using C program.
  • C program to find the sum of main and opposite diagonal elements of a matrix Given a matrix, we have to find the sum of main and opposite diagonal elements of a matrix using C program.
  • C program to find the normal of a matrix Given a matrix, we have to find the normal of a matrix using C program.
  • C program to find the trace of matrix Trace of a n x n square matrix is sum of diagonal elements. Given a square matrix, we have to find the trace of matrix.
  • C program to print the upper triangular matrix Given a 3x3 matrix, we have to print the upper triangular matrix using C program.

Comments and Discussions!

Load comments ↻

  • Marketing MCQs
  • Blockchain MCQs
  • Artificial Intelligence MCQs
  • Data Analytics & Visualization MCQs
  • Python MCQs
  • C++ Programs
  • Python Programs
  • Java Programs
  • D.S. Programs
  • Golang Programs
  • C# Programs
  • JavaScript Examples
  • jQuery Examples
  • CSS Examples
  • C++ Tutorial
  • Python Tutorial
  • ML/AI Tutorial
  • MIS Tutorial
  • Software Engineering Tutorial
  • Scala Tutorial
  • Privacy policy
  • Certificates
  • Content Writers of the Month

Copyright © 2024 www.includehelp.com. All rights reserved.

Precedence and Associativity

Pointers and arrays, pointers and strings, pointers and functions, pointers and structures, handling files, command line arguments, dynamic memory allocation.

two dimensional array assignment c

C - Two Dimensional Arrays

C Programming

In this tutorial we will learn about two dimensional array in C programming language.

We have already covered about what are arrays in the previous tutorial. Now, lets explore the two dimensional arrays.

So, from the previous tutorial we know that when we want to store one dimensional data like score of a single player in 5 matches we use one dimensional array.

Now, if we want to store two dimensional data like storing respective scores of 4 players for the 5 matches then we need the two dimensional array.

Syntax of a 2D Array

To create a two dimensional array we use the following syntax.

In the above example we are creating an array named score of type int . It has 4 rows and 5 columns i.e., total 4x5 = 20 elements.

So, the above code will instruct the computer to allocate memory to store 20 elements of type int and we can represent the score array as shown below.

two dimensional array assignment c

Array indexing starts from 0 so, in the above image there are 4 rows having index 0, 1, 2 and 3. And there are 5 columns having index 0, 1, 2, 3 and 4.

Assigning value to a 2D Array

We can assign value to a 2D array by targeting the cell using the row-index and col-index.

For example, if we want to assign 10 to element at row-index 0 and column-index 0 then we will write the following code.

Similarly, if we want to assign lets say 50 to an element of an array at row-index 1 and column-index 3 then we will write the following code.

Creating and initialising 2D array

There are a couple of ways we can create and initialise a 2D array in C.

In the following code we are creating a 2D array bonus of type int having 2 rows and 3 columns.

In the above code 1, 2, 3 is for the first row and 4, 5, 6 is for the second row of the array bonus.

We can achive the same result by separating the rows with curly brackets { } as show below.

We can also skip elements and they will be auto filled with 0s.

In the following example we are creating an array bonus of type int having 2 rows and 3 columns but this time we are initialising only few elements.

The above code will create the bonus array having 2 rows and 3 columns and we will get the following initialisation.

If we want to initialise all elements of a row to lets say 0 then we can write the following.

So, the above code will give us the following result.

Now, before we end this tutorial lets write a program in C to take score of 4 players for 5 matches and print them out.

© 2014 - 2024 dyclassroom . All rights reserved.

Learn C practically and Get Certified .

Popular Tutorials

Popular examples, reference materials, learn c interactively.

  • Find Largest Number Using Dynamic Memory Allocation
  • C Program Swap Numbers in Cyclic Order Using Call by Reference
  • Access Array Elements Using Pointer

Multiply two Matrices by Passing Matrix to a Function

Find Transpose of a Matrix

Multiply Two Matrices Using Multi-dimensional Arrays

  • Add Two Matrices Using Multi-dimensional Arrays
  • Calculate Standard Deviation

Find Largest Element in an Array

  • C Multidimensional Arrays
  • Calculate Average Using Arrays

C Program to Add Two Matrices Using Multi-dimensional Arrays

To understand this example, you should have the knowledge of the following C programming topics:

Program to Add Two Matrices

In this program, the user is asked to enter the number of rows r and columns c . Then, the user is asked to enter the elements of the two matrices (of order r x c ).

We then added corresponding elements of two matrices and saved it in another matrix (two-dimensional array). Finally, the result is printed on the screen.

Sorry about that.

Related Examples

Javatpoint Logo

  • Design Pattern
  • Interview Q

C Control Statements

C functions, c dynamic memory, c structure union, c file handling, c preprocessor, c command line, c programming test, c interview.

JavaTpoint

  • Send your Feedback to [email protected]

Help Others, Please Share

facebook

Learn Latest Tutorials

Splunk tutorial

Transact-SQL

Tumblr tutorial

Reinforcement Learning

R Programming tutorial

R Programming

RxJS tutorial

React Native

Python Design Patterns

Python Design Patterns

Python Pillow tutorial

Python Pillow

Python Turtle tutorial

Python Turtle

Keras tutorial

Preparation

Aptitude

Verbal Ability

Interview Questions

Interview Questions

Company Interview Questions

Company Questions

Trending Technologies

Artificial Intelligence

Artificial Intelligence

AWS Tutorial

Cloud Computing

Hadoop tutorial

Data Science

Angular 7 Tutorial

Machine Learning

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures

DAA tutorial

Operating System

Computer Network tutorial

Computer Network

Compiler Design tutorial

Compiler Design

Computer Organization and Architecture

Computer Organization

Discrete Mathematics Tutorial

Discrete Mathematics

Ethical Hacking

Ethical Hacking

Computer Graphics Tutorial

Computer Graphics

Software Engineering

Software Engineering

html tutorial

Web Technology

Cyber Security tutorial

Cyber Security

Automata Tutorial

C Programming

C++ tutorial

Control System

Data Mining Tutorial

Data Mining

Data Warehouse Tutorial

Data Warehouse

RSS Feed

IMAGES

  1. Two Dimensional Arrays in C++

    two dimensional array assignment c

  2. Two dimensional (2D) arrays in C programming with example

    two dimensional array assignment c

  3. Two Dimensional Array in C++

    two dimensional array assignment c

  4. Two Dimensional Array in C

    two dimensional array assignment c

  5. Two Dimensional Array in C Programming

    two dimensional array assignment c

  6. Two Dimensional Arrays in C Detailed Explanation Made Easy Lec-47

    two dimensional array assignment c

VIDEO

  1. Array,Types of Array , One two dimension array, array in C++., web development,list, programming

  2. Two Dimensional 2D Arrays in C and C++ programming with example

  3. C++ Multidimensional Array

  4. 07

  5. C++ For Beginners

  6. Two-Dimensional Array in C

COMMENTS

  1. Multidimensional Arrays in C

    Syntax: data_type array_name[size1][size2]....[sizeN]; data_type: Type of data to be stored in the array. array_name: Name of the array. size1, size2,…, sizeN: Size of each dimension. Examples: Two dimensional array: int two_d[10][20]; Three dimensional array: int three_d[10][20][30]; Size of Multidimensional Arrays:

  2. C Multidimensional Arrays (2d and 3d Array)

    In C programming, you can create an array of arrays. These arrays are known as multidimensional arrays. For example, float x [3] [4]; Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can think the array as a table with 3 rows and each row has 4 columns. Two dimensional Array

  3. Two Dimensional Array in C

    Two Dimensional Array in C Last updated on July 27, 2020 Two-dimensional Array The syntax declaration of 2-D array is not much different from 1-D array. In 2-D array, to declare and access elements of a 2-D array we use 2 subscripts instead of 1. Syntax: datatype array_name [ROW] [COL]; The total number of elements in a 2-D array is ROW*COL.

  4. 2D Arrays in C

    What is a 2D array in C? A 2D array is like a matrix and has a row and a column of elements ( Although in memory these are stored in contiguous memory locations). A 1-D array, as we saw in the previous tutorial, is a linear list of data and needed only one index to access the element like a [2].

  5. c

    So basically I want the output to be like the image given. I declared an array with 11,11 size and I am trying to use loops to assign characters in the array. But for some reason it is not working. I know I have to use loops so I drew the pattern on a paper and write down the array location and tried to assign using loops.

  6. Two dimensional (2D) arrays in C programming with example

    The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. Let's take a look at the following C program, before we discuss more about two Dimensional array. Simple Two dimensional (2D) Array Example

  7. C Arrays

    What is Array in C? An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., and also derived and user-defined data types such as pointers, structures, etc. C Array Declaration

  8. Two Dimensional Array in C

    C Programming Two Dimensional Array in C Prepbytes March 28, 2023 Last Updated on December 28, 2023 by Ankit Kochar Two-dimensional arrays are fundamental data structures in programming languages like C, allowing the storage and manipulation of data in a grid-like format.

  9. C Multidimensional Arrays (Two-dimensional and more)

    Two-Dimensional Arrays. A 2D array is also known as a matrix (a table of rows and columns). To create a 2D array of integers, take a look at the following example: int matrix [2] [3] = { {1, 4, 2}, {3, 6, 8} }; The first dimension represents the number of rows [2], while the second dimension represents the number of columns [3].

  10. Two Dimensional Array in C Programming

    Two Dimensional Array in C is the simplest form of MultiDimensional. In Two Dimensional Array, data is stored in rows and column-wise. We can access the record using both the row index and column index (like an Excel File). Declaration of Two Dimensional Array in C

  11. Introduction to Two-Dimensional (2D) Arrays

    C Programming: Introduction to Two-Dimensional (2D) Arrays in C Programming.Topics discussed:1) Definition two-dimensional array.2) Declaration of two-dimens...

  12. C Arrays (With Examples)

    Arrays in C An array is a variable that can store multiple values. For example, if you want to store 100 integers, you can create an array for it. int data[100]; How to declare an array? dataType arrayName[arraySize]; For example, float mark[5]; Here, we declared an array, mark, of floating-point type. And its size is 5.

  13. How to declare a Two Dimensional Array of pointers in C?

    How to create a 2D array of pointers: A 2D array of pointers can be created following the way shown below. int *arr [5] [5]; //creating a 2D integer pointer array of 5 rows and 5 columns. The element of the 2D array is been initialized by assigning the address of some other element.

  14. C language Two Dimensional (Matrix) solved programs/examples

    A two-dimensional array is an array of arrays that has two values 1) number of rows and 2) number of columns in each row. It can be considered as a matrix with rows and columns. Syntax to declare a two-dimensional array in C, type array_name [rows] [columns];

  15. Two Dimensional Arrays

    Syntax of a 2D Array. To create a two dimensional array we use the following syntax. type arrName[rows][cols]; Example. int score[4][5]; In the above example we are creating an array named score of type int. It has 4 rows and 5 columns i.e., total 4x5 = 20 elements.

  16. Two Dimensional Array in C++

    cout<<"Printing a 2D Array:\n"; for(i=0;i<4;i++) { for(j=0;j<2;j++) { cout<<"\t"<<arr[i][j]; } cout<<endl; } } Output: Printing A 2D Array In the above code, We firstly initialize a 2D array, arr [4] [2] with certain values, After that, we try to print the respective array using two for loops,

  17. C Program to Add Two Matrices Using Multi-dimensional Arrays

    In this program, the user is asked to enter the number of rows r and columns c. Then, the user is asked to enter the elements of the two matrices (of order r x c ). We then added corresponding elements of two matrices and saved it in another matrix (two-dimensional array). Finally, the result is printed on the screen.

  18. c

    There is one other answer that makes use of the fact that items in an array are contiguously stored. uint8_t *matrix_ptr = l_matrix[0]; Now, that formally only allows you to access the elements of the first element of the two dimensional array.

  19. Two Dimensional Array in C

    Declaration of two dimensional Array in C. The syntax to declare the 2D array is given below. data_type array_name [rows] [columns]; Consider the following example. int twodimen [4] [3]; Here, 4 is the number of rows, and 3 is the number of columns.

  20. c++

    1. You cannot define an array dynamically the way you do it. You need to use the c++ keyword new: int nmatrix [] [] = new int [r+shiftr] [c+shiftc]; You cannot define arrays the way you did, with non constant int value for dimension, because such static arrays are to be defined for memory at the compile stage.