Professional Documents
Culture Documents
Module - 4
Module - 4
Module - 4
Module-4
TWO – DMENSIONAL ARRAYS
Arrays which are specified with 2 subscripts (2 set of square brackets [ ] [ ]) are
called 2- dimensional arrays.
Arrays with two or more dimensions are called Multi-dimensional arrays.
In 2-Dimensional Array, the first index indicates the “row size”( the number of
rows) and second index indicates the “column size”( the number of columns).
Ex: inta[10][10]; //Two-dimensional array
Declaration of Two-dimensional arrays
As we declare the variables before they are used in a program, an array must also be declared
before it is used using the following syntax.
The array “a” is a 2-dimensional array with 2 rows and 4 columns. This declaration informs the
compiler to reserve 8 locations (2*4=8 locations, 2*8=16 bytes in total) continuously one after
the other.
To write/print the 2-dimensional array elements, we have to use two for loops along with
printf(), where the outer for loop indicates the number of rows to be printed and the inner
for loop indicates the number of columns to be printed.
To print the “n” array elements of Two dimensional array:
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
Example Programs:
1. Write a C Program to read and print 2-dimensional array.
#include<stdio.h>
void main( )
{
int a[10][10],i,j,m,n;
printf(“Enter the number of Rows and Columns of the Array:\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter the Array elements:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Entered array elements are:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
}
printf(“\n”);
}
}
Output:
Enter the number of Rows and Columns of the Array:
22
Enter the Array elements:
1
2
3
4
Entered array elements are:
1 2
3 4
Sum Two matrices that are compatible with each other can be added together thereby storing the
result in the third matrix. Two matrices are said to be compatible when they have the same
number of rows and columns. Elements of the matrices can be added by writing: Ci,j= Ai,j + Bi,j
Difference Two matrices that are compatible with each other can be subtracted thereby storing
the result in the third matrix. Two matrices are said to be compatible when they have the same
number of rows and columns. Elements of the matrices can be subtracted by writing: Ci,j= Ai,j-Bi,j
Product Two matrices can be multiplied with each other if the number of columns in the first
matrix is equal to the number of rows in the second matrix. Therefore, m X n matrix A can be
multiplied with a p x q matrix if n = q.
Elements of the matrices can be multiplied by writing:
Ci,j= Σ Ai,kBj,k for k=1 to k<n.
{
printf ("\n");
for (j=0; j<3; j++)
printf ("\t transposed_mat [%d] [%d]=%d", i, j, transposed_mat [i][j]);
}
return 0;
}
Output
Enter the elements of the matrix
**************************
123456789
The elements of the matrix are
**************************
123
456
789
The elements of the transposed matrix are
147
258
369
#include<stdio.h>
void main( )
{
int a[10][10],b[10][10],c[10][10],i,j,m,n;
printf(“Enter the size of matrix:\n”);
scanf(“%d%d”,&m,&n);
printf(“Enter the elements of Matrix A:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter the elements of Matrix B:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf(“The Resultant Matrix C is:\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,c[i][j]);
}
printf(“\n”);
}
}
Output
Enter the size of matrix: 2 2
Enter the elements of Matrix A:
1 2
3 4
Enter the elements of Matrix B:
5 6
7 8
The Resultant Matrix C is:
6 8
10 12
if(n == p)
{
printf("\n Enter the elements of Matrix A \n");
for(i = 0 ; i< m ; i++)
{
for(j = 0 ; j < n ; j++)
scanf("%d", &a[i][j]);
}
printf("\n Enter the elements of matrix B \n");
for(i = 0 ; i< p ; i++)
{
for(j = 0 ; j < q ; j++)
scanf("%d", &b[i][j]);
}
for(i = 0 ; i< m ; i++)
{
for(j = 0 ; j < q ; j++)
{
c[i][j]=0;
for(k = 0 ; k < n ; k++)
{
c[i][j] += a[i][k] * b[k][j];
}
}
}
printf("\n MATRIX A \n");
for(i = 0 ; i< m ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf(" %d \t", a[i][j]);
}
printf("\n")
}
printf("\n MATRIX B \n");
for(i = 0 ; i< p ; i++)
{
for(j = 0 ; j < q ; j++)
{
printf(" %d \t", b[i][j]);
}
printf("\n");
}
printf("\n MATRIX C \n");
for(i = 0 ; i< m ; i++)
{
for(j = 0 ; j < q ; j++)
{
printf(" %d \t", c[i][j]);
}
printf("\n");
}
}
else
printf("Matrix multiplication is not possible");
getch();
return 0;
}
Passing a Row
A row of a 2D array can be passed by indexing the array name with the row number. When we
send a single row of a two-dimensional array, then the called function receives a one-
dimensional array. Figure illustrates how a single row of a 2D array is passed to the called
function
MULTIDIMENSIONAL ARRAYS
A multidimensional array in simple terms is anarray of arrays. Like we have one index in a
1Darray, two indices in a 2D array, in the same way wehave n indices in an, n-dimensional array
or multidimensional array.Conversely, an n-dimensionalarray is specified using n indices. An n-
dimensionalm1 X m2 X m3 X ... mn, array is a collection m1 * m2 *m, * m3……mn elements. In a
multidimensional array, a particularelement is specified by using n subscripts as A [I 1] [I2]
[I3] . ..In], where,
I1<= M1, I2< = M2, I3<= M3, . In<= Mn
A multidimensional array can contain as many indicesas needed and the requirement of the
memory increaseswith the number of indices used. However, practicallySpeaking we will hardly
use more than three indices inany program. Figure shows a three-dimensional(3D) array. The
array has three pages, four rows, and twocolumns.
STRINGS
“A string is a sequence of characters enclosed within double quotes”.
or
“String is an array of characters and terminated by NULL character which is denoted by
“\0”.
A string is stored as a sequence of characters in an array terminated by „\0‟ (NULL character).
Ex: Consider the String “DAVANGERE”.
This string is stored in the form of an array as shown below:
Length of the String: The „length‟ is the number of characters stored in the string up to but not
including the null character.
Example
1. char name[21];
Size of the string is 21, means that it can store up to 20 characters plus one null character.
2. charstr[10];
Size of the string is 10, means that it can store up to 10 characters plus one null character.
1. char a[9]={„C‟, „O‟, „M‟, „P‟, „U‟, „T‟, „E‟, „R‟, „\0‟};
The compiler allocates 9 memory locations ranging from 0 to 8 and these locations are initialized
with the characters in the order specified.
3. char b[ ]={„C‟, „O‟, „M‟, „P‟, „U‟, „T‟, „E‟, „R‟, „\0‟};
• For this declaration, the compiler will set the array size to the total number of initial
values.
• i.e. 9. The characters will be stored in these memory locations in the order specified as
shown below:
4. char b[ ]= “COMPUTER”;
• Here, the string length is 8 bytes. But string size is 9 bytes. So, the compiler reserves 8+1
memory locations and these locations are initialized with the characters in the order
specified. The string is terminated by “\0” by the compiler.
Example:
char name[20];
printf(“Enter the name:\n”); scanf(“%s”, name);
printf(“The entered name is:%s”, name);
Output: Enter the name: Rama
The entered name is: Rama
Write a C program to read and display a string using scanf() and printf().
#include<stdio.h>
void main()
{
charstr[20];
printf(“Enter your name:\n”);
scanf(“%s”,str);
printf(“Entered name is:%s\n”,str);
}
Output:
Enter your name: Rajashekhar
Entered name is: Rajashekhar
The “gets()” function allows us to read an “entire line” of input including whitespace
characters.
Syntax : gets(string);
Example:
char name[20];
printf(“Enter the name:”);
gets(name);
Printing a String using puts( )
“puts()” function is used for printing the given strings.
Whenever we want to display a sequence of characters stored in memory locations on the
screen, then puts() function can be used.
Syntax : puts(string);
Example:
char name[20];
printf(“Enter the name:\n”);
gets(name);
printf(“The entered name is:\n”);
puts(name);
Output:
Enter the name:
Abdul Kalam
Entered name is:
Abdul Kalam
Here, buffer is the place to store the resulting string from using the function. The argument
command is an ellipsis so you can put as many types of arguments as you want. Finally, the
format is the string that contains the text to be printed. The string may contain format tags.
#include <stdio.h>
main ( )
{
Charbuf [100];
intnum = 10;
sprintf(buf, “ num = %3d”, num);
}
substring is: there