Module 5. Arrays

Lesson 16

ARRAYS in ‘C’ – PART II

(Multi-Dimensional Arrays and Pointers)

16.1  Multi-dimensional Arrays

‘C’ allows arrays to have dimensions more than two. Multi-dimensional arrays have two or more index values, which specify the elements in the array. The general syntax is:

type variable-name[i][j]

The type specifies the data type of the elements that will be contained in the array, such as int, float or char and the first index value i specifies the row index, while j specifies the column index of the variable.

Illustration

int matrix1[3][3];

Here, matrix1 is a two-dimensional array of the type, integer having three rows and three columns; and, hence, can store up to nine (3×3) elements. The matrix1 can be initialised by assigning value to each element as follows:

int matrix[3][3]={{2,4,7}, {4,9,0},{3,9,6}};

It will appear as shown below:

matrix1[0][0]=2

matrix1[0][1]=4

matrix1[0][2]=7

matrix1[1][0]=4

matrix1[1][1]=9

matrix1[1][2]=0

matrix1[2][0]=3

matrix1[2][1]=9

matrix1[2][2]=6

Example 1: Programme to multiply two 3 × 3 matrices.

#include <stdio.h>

#include <conio.h>

void main()

{

 int a[3][3], b[3][3], c[3][3], i, j, k;

 clrscr();

 printf("Enter the elements of first 3x3 matrix");

 for (i = 0;i < 3; i++)

 {

   for (j = 0; j < 3; j++)

   {

      scanf("%d",&a[i][j]);

   }

 }

 printf("\nEnter the elements of second 3×3 matrix");

 for(i = 0; i < 3; i++)

 {

   for (j = 0; j < 3; j++)

   {

      scanf("%d", &b[i][j]);

   }

 }

 printf("\nThe first matrix is :-\n");

 for (i = 0; i < 3; i++)

 {

   for (j = 0; j < 3; j++)

   {

      printf("\t%d", a[i][j]);

   }

   printf("\n");

 }

 printf("\nThe second matrix is :-\n");

 for (i = 0; i < 3; i++)

 {

   for (j = 0; j < 3; j++)

   {

      printf("\t%d", b[i][j]);

   }

   printf("\n");

 }

 printf("\nMultiplication of the two matrices is as

         follows:\n");

 for (i = 0;i < 3; i++)

 {

   printf("\n");

   for (j = 0; j < 3; j++)

   {

     c[i][j]=0;

     for(k=0;k<3;k++)

       c[i][j] = c[i][j]+a[i][k] * b[k][j];

     printf("\t%d", c[i][j]);

   }

 }

 getch();

}

Test Output

Enter the elements of first 3x3 matrix1

2

3

4

5

6

7

8

9

Enter the elements of second 3x3 matrix1

2

3

4

5

6

7

8

9

The first matrix is :-

        1       2       3

        4       5       6

        7       8       9

The second matrix is :-

        1       2       3

        4       5       6

        7       8       9

Multiplication of the two matrices is as follows:

        30      36      42

        66      81      96

        102     126     150

Example 2: Programme to transpose a 3 × 3 matrix.

#include < stdio.h >
#include < conio.h >
void main()
{
   int arr[3][3],i,j;
   clrscr();
   printf("Enter elements for the array \n");
   for(i=0;i < 3;i++)
   {
     for(j=0;j < 3;j++)
     {
         scanf("%d",&arr[i][j]);
     }
   }
   printf("Original array entered by the user is \n");
   for(i=0;i < 3;i++)
   {
     for(j=0;j < 3;j++)
     {
       printf("%d ",arr[i][j]);
     }
     printf("\n");
   }
   printf("\n Transpose of the array is \n");
   for(i=0;i < 3;i++)
   {
      for(j=0;j < 3;j++)
        printf("%d ",arr[j][i]);
      printf("\n");
   }
   getch();
}

Test Output:

Enter elements for the array

5

6

7

3

5

4

1

7

8

Original array entered by the user is

5 6 7

3 5 4

1 7 8

 Transpose of the array is

5 3 1

6 5 7

7 4 8

Example 3: Programme to compute sum of diagonal elements of a matrix.

#include<stdio.h>

int main()

{

  int a[10][10],i,j,sum=0,m,n;

  printf("\nEnter the row and column of matrix: ");

  scanf("%d %d",&m,&n);

  printf("\nEnter the elements of matrix: ");

  for(i=0;i<m;i++)

      for(j=0;j<n;j++)

           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");

  for(i=0;i<m;i++){

      printf("\n");

      for(j=0;j<m;j++){

      printf("%d\t",a[i][j]);

      }

 }

 for(i=0;i<m;i++){

     for(j=0;j<n;j++){

          if(i==j)

              sum=sum+a[i][j];

     }

 }

 printf("\n\nSum of the diagonal elements of the matrix

        is: %d",sum);

 return 0;

}

Test Output

Enter the row and column of matrix: 3 3

Enter the elements of matrix: 2

3

5

6

7

9

2

6

7

The matrix is

2       3       5

6       7       9

2       6       7

Sum of the diagonal elements of the matrix is: 16

Example 4: Programme to find determinant of a 3 × 3 matrix.

#include<stdio.h>

int main()

{

  int a[3][3],i,j;

  long determinant;

  printf("Enter the 9 elements of matrix: ");

  for(i=0;i<3;i++)

      for(j=0;j<3;j++)

           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");

  for(i=0;i<3;i++){

      printf("\n");

      for(j=0;j<3;j++)

           printf("%d\t",a[i][j]);

  }

  determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1]*(a[1][0]*a[2][2] - a[2][0]*a[1][2]) + a[0][2]*(a[1][0]*a[2][1] - a[2][0]*a[1][1]);

printf("\nDeterminant of 3×3 matrix: %ld",determinant);

  return 0;

}

Test Output

Enter the 9 elements of matrix: 1

2

3

4

5

6

7

8

9

The matrix is

1       2       3

4       5       6

7       8       9

Determinant of 3×3 matrix: 0

Example 5: Programme to find inverse of a 3×3 matrix.

#include<stdio.h>

int main()

{

  int a[3][3],i,j;

  float determinant=0;

  printf("Enter the 9 elements of matrix: ");

  for(i=0;i<3;i++)

      for(j=0;j<3;j++)

           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");

  for(i=0;i<3;i++){

      printf("\n");

      for(j=0;j<3;j++)

           printf("%d\t",a[i][j]);

  }

  for(i=0;i<3;i++)

      determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*

                    a[2][(i+2)%3] - a[1][(i+2)%3]

                    *a[2][(i+1)%3]));

  printf("\nInverse of matrix is: \n\n");

  for(i=0;i<3;i++){

      for(j=0;j<3;j++)

           printf("%.2f\t",((a[(i+1)%3][(j+1)%3] *

                  a[(i+2)%3][(j+2)%3]) –

                  (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/

                  determinant);

           printf("\n");

   }

   return 0;

}

Test Output:

Enter the 9 elements of matrix: 3

5

2

1

5

8

3

9

2

The matrix is

3       5       2

1       5       8

3       9       2

Inverse of matrix is:

0.70    -0.25   0.07

-0.09   -0.00   0.14

-0.34   0.25    -0.11

Illustration:

Consider the following code segment demonstrating initialisation of an array with characters to form a string and also, some important tips to properly handle the strings in ‘C’. 

            static char name1[] = {'H','e','l','l','o'};

            static char name2[] = "Hello";

            printf("%s\n", name1);

            printf("%s\n", name2);

Output (the typical output shown below was produced as a result of executing the above code using the Turbo C++ Version 3.0 compiler by Borland International, Inc.):

HelloHello

Hello

Note that the difference between the two arrays name1 and name2 is that the latter has a null value placed automatically at the end of the string, during the initialisation process, i.e., null value is stored at the location, name2[5]; however, the array, name1 does not get a null character placed after initialisation, thereby often leading to some garbage (superfluous) characters being printed at the end, e.g., in this example, HelloHello is printed instead of Hello. This can be remedied by inserting a null character at the end of the array, name1 as follows:

static char name1[] = {'H','e','l','l','o','\0'};

Revised output (the revised output shown below was produced as a result of executing the modified code as discussed above, which is the desired and proper result):

Hello

Hello.

16.2  Pointers

A pointer is a variable suitable for keeping memory addresses of other variables; the values assigned to a pointer are memory addresses of other variables or other pointers. ‘C’ pointers are characterised by their value and data type. The value is the address of the memory location the pointer points to, the type determines as to how the pointer will be incremented and/or decremented in pointer or subscript arithmetic.

Pointers are used to manipulate arrays and they can be used to return more than one value from a function. Pointers are declared by using the asterisk ‘*’, e.g., see the following statement showing a pointer:

int *p;

Each variable has two attributes: address and value. The address is the location in memory. In that location, the value is stored. During the lifetime of the variable, the address is not changed; however, the value may change.

#include 
void main (void)

{
 int i;        
 int * a;     
 i = 10;       
 a = &i;  
 printf (" The address of i is %8u \n", a);         
 printf (" The value at that location is %d\n", i);  
 printf (" The value at that location is %d\n", *a);
}

Output (the typical output shown below was produced as a result of executing the above code using the Turbo C++ Version 3.0 compiler by Borland International, Inc.):
The address of i is   65524

The value at that location is 10

The value at that location is 10