2d ARRAY C code example

Example 1: 2d array of strings in c

language[0] => "Java";
language[1] => "Python";
language[2] => "C++";
language[3] => "HTML";
language[4] => "SQL";

Example 2: c fill 2d array

// Either
int disp[2][4] = {
  {10, 11, 12, 13},
  {14, 15, 16, 17}
};

// Or 
int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

// OR
int i, j;
for (i = 0; i < HEIGHT; i++) { // iterate through rows
  for (j = 0; j < WIDTH; j++) { // iterate through columns
    disp[i][j] = disp[i][j];
  }
}

Example 3: matrix c declaration

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Example 4: double array in c

int disp[2][4] = {
    {10, 11, 12, 13},
    {14, 15, 16, 17}
};

Example 5: c program to represent 2d matrix in 1d matrix

#include<stdio.h>
#define n 3
int main()
{
int a[n][n],b[n*n],c[n*n],i,j,k=0,l=0;
printf(“\n Enter elements of 2D array :);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf(%d”,&a[i][j]);
}
}
printf(“\n Given 2D array : \n“);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(%d ”,a[i][j]);
}
printf(“\n”);
}
printf(“\n Row wise \n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[k]=a[i][j];
k++;
}
}
printf(“\n Column wise \n”);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[l]=a[j][i];
l++;
}
}
}

Example 6: 2D Array In C

// Array of size n * m, where n may not equal m
for(j = 0; j < n; j++)
{
    for(i = 0; i < m; i++)
    {  
        array[i][j] = 0;
    }
}

Tags:

Java Example