2-D Array and 2-D strings in C
2D Array and 2D strings
A 1-D array is a collection of several elements that contain only one row of elements. The 2-D array is a collection of rows and columns where each row contains smiler number of columns. The requirement of 2-D array is removing the need of several 1-d array and it may be helpful to create mathematical data structure named as matrix.
Initializing 2-D Array
Taking input in a 2-D Array :-
[code language = “cpp”] int main() { int a[3][2]; int i,j;
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 2 ; j++ )
{
printf("Enter [%d][%d] element of array : ",i,j);
scanf("%d",&a[i][j]);
}
} } [/code] Output:-
Enter [0][0] element of array : 1 Enter [0][1] element of array : 2 Enter [1][0] element of array : 3 Enter [1][1] element of array : 4 Enter [2][0] element of array : 5 Enter [2][1] element of array : 6
Taking input and printing a 2-D Array :-
[code language = “cpp”] int main() { int a[3][2]; int i,j;
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 2 ; j++ )
{
printf("Enter [%d][%d] element of array : ",i,j);
scanf("%d",&a[i][j]);
}
}
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 2 ; j++ )
{
printf("%4d",a[i][j]);
}
printf("n");
} } [/code] Output :-
Enter [0][0] element of array : 1 Enter [0][1] element of array : 2 Enter [1][0] element of array : 3 Enter [1][1] element of array : 4 Enter [2][0] element of array : 5 Enter [2][1] element of array : 6 1 2 3 4 5 6
Adding, Subtraction and multiplication of matrix :-
[code language = “cpp”] int main() { int a[3][3]; int b[3][3]; int c[3][3];
void input( int [][3] );
void output( int [][3] );
void sum( int [][3] , int [][3] , int [][3] );
void subtraction( int [][3] , int [][3] ,int [][3] );
void multiply( int [][3] , int [][3], int [][3] );
printf("Taking input in first Array : n");
input(a);
printf("Value in a : n");
output(a);
printf("Taking input in first Array : n");
input(b);
printf("nValue in b : n");
output(b);
printf("Adding a and b :n");
printf("nvalue of sum of a and b:n");
sum(a,b,c);
output(c);
printf("Subtracting a and b :n");
printf("nvalue of subtraction of a and b:n");
subtraction(a,b,c);
output(c);
printf("multiplying a and b :n");
printf("value of multiplication of a and b :n");
multiply(a,b,c);
output(c); }
void input( int a[][3] ) { int i ,j;
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 3 ; j++ )
{
printf("Enter [%d][%d] element of array : ",i,j);
scanf("%d",&a[i][j]);
}
} }
void output( int a[][3] ) { int i,j; for( i = 0 ; i < 3 ; i++ ) { for( j = 0 ; j < 3 ; j++ ) { printf("%4d",a[i][j]); } printf("n"); } }
void sum( int x[][3] , int y[][3], int z[][3] ) { int i,j;
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 3 ; j++ )
{
z[i][j] = x[i][j] + y[i][j];
}
} }
void subtraction( int x[][3] , int y[][3], int z[][3] ) { int i,j;
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 3 ; j++ )
{
z[i][j] = x[i][j] - y[i][j];
}
} }
void multiply( int x[][3] , int y[][3], int z[][3] ) { int i,j,k;
for( i = 0 ; i < 3 ; i++ )
{
for( j = 0 ; j < 3 ; j++ )
{
z[i][j] = 0 ;
for( k = 0 ; k < 3 ; k++ )
z[i][j] = z[i][j] + ( x[i][k] * y[k][j] );
}
} } [/code] Output:-
Taking input in first Array : Enter [0][0] element of array : 1 Enter [0][1] element of array : 2 Enter [0][2] element of array : 3 Enter [1][0] element of array : 4 Enter [1][1] element of array : 5 Enter [1][2] element of array : 6 Enter [2][0] element of array : 7 Enter [2][1] element of array : 8 Enter [2][2] element of array : 9 Value in a : 1 2 3 4 5 6 7 8 9 Taking input in first Array : Enter [0][0] element of array : 1 Enter [0][1] element of array : 2 Enter [0][2] element of array : 3 Enter [1][0] element of array : 4 Enter [1][1] element of array : 5 Enter [1][2] element of array : 6 Enter [2][0] element of array : 7 Enter [2][1] element of array : 8 Enter [2][2] element of array : 9 Value in b : 1 2 3 4 5 6 7 8 9 Adding a and b : value of sum of a and b: 2 4 6 8 10 12 14 16 18 Subtracting a and b : value of subtraction of a and b: 0 0 0 0 0 0 0 0 0 multiplying a and b : value of multiplication of a and b : 30 36 42 66 81 96 102 126 150
2-D String :
As we know , A 1-D string can hold only one string at a time. To store more then one string, we can create 2-D string.
Initializing of 2-D string :
Input with 2-D String :
In this program we will take input in 2-D String. [code language= “cpp” ] int main() { char a[5][10]; int i;
for( i = 0 ; i < 5 ; i++ )
{
printf("Enter a string : ");
scanf("%s",a[i]);
}
return 0; } [/code] output:
Enter a string : vipin Enter a string : nitin Enter a string : bhaskar Enter a string : yatender Enter a string : harchand
Output with 2-D String :
In this program we will print a 2-D string after taking input. [code language = “cpp”] int main() { char a[5][10]; int i;
for( i = 0 ; i < 5 ; i++ )
{
printf("Enter a string : ");
scanf("%s",a[i]);
}
for( i = 0 ; i < 5 ; i++ )
{
puts(a[i]);
}
return 0; } [/code]
Enter a string : Vipin Enter a string : Nitin Enter a string : Bhaskar Enter a string : Yatender Enter a string : Harchand Vipin Nitin Bhaskar Yatender Harchand
Now we will code a program who print all name start from v or V
,
from a 2-D string :
[code language = “cpp”] int main() { char a[10][10]; int i;
for( i = 0 ; i < 10 ; i++ )
{
printf("Enter a string : ");
scanf("%s",a[i]);
}
for( i = 0 ; i < 10 ; i++ )
{
if ( a[i][0] == 'v' || a[i][0] == 'V')
puts(a[i]);
}
return 0; } [/code] Output:-
Enter a string : vipin Enter a string : nitin Enter a string : bhaskar Enter a string : harchand Enter a string : yatender Enter a string : vijay Enter a string : ajay Enter a string : ram Enter a string : Vinod Enter a string : prateek vipin vijay Vinod
This code will find given String from a bunch of String :
[code language=”cpp”] // Use string.h library int main() { char a[10][10]; char b[10]; int i;
for( i = 0 ; i < 10 ; i++ )
{
printf("Enter a string : ");
scanf("%s",a[i]);
}
printf("Enter a string you want to search : ");
scanf("%s",b);
for( i = 0 ; i < 10 ; i++ )
{
if ( strcmp( a[i] , b ) == 0)
printf("String is at a[%d]",i);
}
return 0; } [/code] Output:-
Enter a string : Vipin Enter a string : nitin Enter a string : yatender Enter a string : bhaskar Enter a string : ajay Enter a string : vijay Enter a string : harchand Enter a string : ned Enter a string : caption Enter a string : rajat Enter a string you want to search : vijay String is at a[5]
We can have more multi dimensional array but we use them according to our needs. In our next blog-post we will read structures :)