Reading:  

Getting started with C Language


Multi dimensional arrays

Multi dimensional arrays

Multi dimensional arrays are the arrays with a single variable name and more than one subscript. Declaration is as like one dimensional but only change is there in the subscript. It need data type array name and the subscript (indices).

The general syntax of multidimensional arrays :

Data-type array-name[index1][index2][index3]…..[indexn];

Example of 4 dimensional array :

int multi[10][10][10];

here the multidimensional array ‘multi’ is an integer array the sizes are given as the subscript.

Matrix

A two dimensional array is called a matrix. Thus it is two dimensional it has two subscripts. A matrix is used to store a table of values. As it is similar to a table a matrix has rows and columns. Thus the first subscript refers as the rows and second index as the columns.

Syntax :

Data-type matrix-name[rows][columns];

Example :

int a[2][2];

here a is a matrix with 2 rows and 2 columns. We can say ‘a’ is a 2  x 2 (read 2 by two) matrix.

                                       

 

Column 0 

Column 1

Row 0

a[0][0]

a[0][1]

Row 1

a[1][0]

a[1][1]

 

 

Assigning values to the matrix

A matrix can be assigned values by specifying bracketed values for each row as  Follows.

int a[2][2] = {0,1,2,3};

The other method which is more readable is like

int a[2][2] = { 
  {0, 1} ,   //first row
{2,3}      //second row
};

Here the values enclosed in brackets denotes the rows of the matrix.

This two initializations results as a matrix like

 

                              

     0

   1

     2

   3

 

 

 

For accessing matrix variables we use the subscripts along with the name of the matrix. But make sure that we number the values of rows and columns from 0. In the above  example to access the value 1 we use a[0][1] that is the position of 1 is in the 0th row and the 1st column.

 

The dynamic initialization of matrix, need two loops one for row values and another for column values.

Program to assign the values to the matrix and print that matrix

#include <stdio.h>
void main()
{
int i, j, m, n, a[10][10];
printf("Enter the number of rows and columns of matrix ");
scanf("%d%d", &m, &n);
printf("Enter the elements of matrix ");
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
scanf("%d", &a[i][j]);
}
}
printf("The elements of matrix a is");
for ( i = 0 ; i < m ; i++ )
{
for ( j = 0 ; j < n ; j++ )
{
printf("%d", a[i][j] “ “);
}
printf(“\n”) // for new line( for rows)
}
}

When execute this program it reads the size and then assign the values to the matrix as , if m=2,n=3 then the values assigned is like

a[0][0] = 0;
a[0][1] = 10;
a[0][2] = 8;
a[1][0] = 4;
a[1][1] = 9;
a[1][2] = 12;

     0

    10

    8

 

    4

 

     9

 

  12

 

The values are assigned like this and it displays the matrix as

0   10   8
4    9   12

Operations on matrices

As on arrays a set of operations are performed using matrices, they may be

  • Addition
  • Subtraction
  • Multiplication
  • Transpose etc.

For the first three we need two or more arrays , transpose is done on that same matrix, it is changing the values on rows to columns and columns as rows.

Program to add two matrices.

#include <stdio.h>
int main()
{
   int m, n, i, j, first[10][10], second[10][10], sum[10][10];
   printf("Enter the number of rows and columns of matrix");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix");
   for (i = 0 ; i < m ; i++ )
   {
        for ( j= 0 ;  j < n ; j++)
          {
         scanf("%d", &first[i][j]);
         }
}
   printf("Enter the elements of second matrix ");
   for ( i = 0 ; i< m ; i++ )
    {
      for ( j = 0 ; j < n ; j++ )
      {
          scanf("%d", &second[c][d]);
  }
}
  for ( i = 0 ; i < m ; i++ )
   {
      for ( j = 0 ; j < n ; j++ )
         sum[i][j] = first[i][j] + second[i][j];
}
   printf("Sum is") ;
   for ( i = 0 ; i < m ; i++ )
   {
      for ( j = 0 ; j < n ; j++ )
       {
         printf("%d\t", sum[i][j]);
        }
      printf("\n");
   }
}

first

   10

     5

   20

    10

 

second

     9

 10

   3

25

 

sum

     19

 15

   23

35

 

 

 

 

                                                   

                                      

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!