• facebook
  • whatsapp
  • telegram

2-D Character Arrays

13.3. 2-D Character Arrays


C language లో మనం 2-D multi-dimensional arrays ను కూడా వాడవచ్చు. ఇప్పుడు మనం 2-D character arrays గురించి నేర్చుకుందాం..

2D character Arraysను ఈ కింది విధంగా declare చేయవచ్చు.
I. char X[10][20];
ఇది 10x20 ఉండే 2D character arrayను క్రియేట్ చేస్తుంది.
II. char X[5][20] = { “RAM”, “RAVI”, “ABHI”};
ఇది 10x20 ఉండే 2D character arrayను క్రియేట్ చేస్తూ మనం assign చేసిన “RAM”, “RAVI”, “ABHI” అనే string constantsనుఈకిందివిధంగాస్టోర్చేస్తుంది.


III. char X[ ][20] = { “RAM”, “RAVI”, “ABHI”};
ఇది 20 columns ఉండే 2D character arrayను క్రియేట్ చేస్తూ మనం assign చేసిన “RAM”, “RAVI”, “ABHI” అనే string constantsను ఈ కింది విధంగా స్టోర్ చేస్తుంది. ఇక్కడ rowsఎన్నో మనం ఇవ్వలేదు, మూడు string constants లను assign చేస్తున్నాం కనుక, rowsను మూడుగా తీసుకుంటుంది.


IV. #define M = 10
#define N = 20
char X[M][N]= { “RAM”, “RAVI”, “ABHI”};
ఇది 10x20 ఉండే 2D character arrayను క్రియేట్ చేస్తూ మనం assign చేసిన “RAM”, “RAVI”, “ABHI” అనే string constants ను ఈ కింది విధంగా స్టోర్ చేస్తుంది. ఇక్కడ rows, columns, సైజ్ లను మనం ఇచ్చిన symbolic constants M, N విలువలగా తీసుకుంటుంది.

X కనుక ఒక two dimensional character array అయితే X[0], X[1], X[2], అనేవి దానిలో ఉన్న rows. వాటినే మనం one dimensional character arrays అని, లేదా string variables అనొచ్చు. అలాగే, X[i][j], ith row jth column element, అది ఒక character. పైన ఇచ్చిన ఉదాహరణలో X[0] అనేది RAM అవుతుంది, X[1] అనేది RAVI అవుతుంది. అలాగే, X[2][2] అనేది ‘H’ అవుతుంది.

Example 1: ఈ కింది ప్రోగ్రాం కొన్ని strings ను two dimensional character array rows లోనికి రీడ్ చేసి వాటిలో longest string ను ప్రింట్ చేస్తుంది.
Solution: ముందుగా ఒక లూప్ రాసి, కొన్ని strings ను two dimensional character array rows లోకి రీడ్ చేస్తాం. తర్వాత, ఇంకో లూప్ సహాయంతో ప్రతి string length ను strlen() function వాడి తెలుసుకుంటూ వాటిలో longest string index ను కనుక్కొంటాం. ఆఖరున, longest string ను ప్రింట్ చేస్తాం.

#include
#include
int main()
{
     int i, n, m, id, max = 0;
     char x[10][10];
     printf(“Enter number of strings\n”);
     scanf(“%d”, &n);
     /* reading a set of strings into rows of a 2-D character array*/
     for(i=0; i < n; i++){
     printf(“Enter a string\n”);
     scanf(“%s”, x[i]);
     }
     /*finding largest string*/
     for(i=0; i < n; i++){
     m = strlen(x[i]);
     /* length of the string x[i] is calculated */

     if( m > max ) {
     max = m;
     id=i;
     }
     }
     printf(“Largest string=%s\n”, x[id]);
     return (0);
}


ఈ ప్రోగ్రాం రన్ అవుతున్నప్పుడు, ఈ పైన ఇచ్చిన strings 2-D array లో ఉన్నాయి అని అనుకొని ప్రోగ్రాంను trace చేసాం.

ఇక్కడ, ABHINAV ప్రింట్ అవుతుంది.

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.


Example 2: ఈ కింది ప్రోగ్రాం కొన్ని stringsను two dimensional character array rows లోకి రీడ్ చేసి వాటిలో alphabetical order లో ముందు వచ్చే string ను ప్రింట్ చేస్తుంది.
Solution: ఈ ప్రోగ్రాంలో కూడా ముందుగా ఒక లూప్ రాసి, కొన్ని strings ను two dimensional character array rows లోకి రీడ్ చేస్తాం. తర్వాత, ఇంకొక లూప్ సహాయముతో ప్రతి stringను వేరొక stringతో strcmp function సహాయంతో కంపేర్ చేసి వాటిలో alphabetical order లో ముందు వచ్చే string indexను కనుక్కొంటాం. ఆఖరున, alphabetical order లో ముందు వచ్చే stringను ప్రింట్ చేస్తాం.

#include
#include
int main()
{
     int i, n, id;
     char x[10][20];
     printf(“Enter number of strings\n ”);
     scanf(“%d”, &n);
     printf(“Enter Strings: ”);
     /* reading a set of strings into rows of a 2-D character array*/
     for(i=0; i < n; i++) {
     printf(“Enter a string\n”);
     scanf(“%s”, x[i]);
     }

     id = 0; /* first string is assumed as the first one in order*/

     for(i=1; i < n; i++)
     if( strcmp( x[id], x[i]) > 0 ) id = i;
     printf(“String which comes first in alphabetical order=%s\n”, x[id]);

     return (0);
}

ఈ కింద RAM, RAVI, ABHI, ABHINAV, ANUఅనే stringsను ప్రోగ్రాంకు ఇచ్చామనుకొని, trace చేశాం.

అఖరున, x[id], అనగా ABHI ప్రింట్ అవుతుంది.
పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.

Example 3: ఈ కింది ప్రోగ్రాం కొన్ని stringsను two dimensional character array rows లోనికి రీడ్ చేసి వాటిలో “VOWELS” ఎక్కువగా ఉండే string ను ప్రింట్ చేస్తుంది.
Solution: ఈ ప్రోగ్రాంలో కూడా ముందుగా ఒకలూప్ రాసి, కొన్ని stringsను two dimensional character array యొక్క rows లోనికి రీడ్ చేస్తాం. తర్వాత, రెండు nested లూప్ లు వాడి ప్రతీ stringలో ఎన్ని “VOWELS” ఉన్నాయో కనుక్కొని వాటిలో “VOWELS” ఎక్కువగా ఉండే stringను కనుక్కొని ప్రింట్ చేస్తుంది.

#include
#include
int main()
{
     int i, j, n, max = 0, id, k, l;
     char x[10][20];
     printf(”Enter number of strings\n”);
     scanf(“%d”, &n);
     /* reading a set of strings into rows of a 2-D character array*/
     for(i=0; i < n; i++){
     printf(”Enter a string\n”);
     scanf(“%s”, x[i]);
     }
     for(i=0; i < n; i++){l=0;
     for(j=0; x[i][j] != ‘\0’; j++){
     switch( toupper( x[i][j] )){
     case ‘A’ :
     case ‘E’ :
     case ‘I’ :
     case ‘O’ :
     case ‘U’ :
     l++;
     break;
     }
     }
     if( l > max ){
     max = l;
     id = i;
     }
     }
     printf(“String having more number of vowels=%s\n”, x[id]);
     return (0);
}


ఈ ప్రోగ్రాం రన్ అవుతున్నప్పుడు, ఈ పైన ఇచ్చిన strings 2-D array లో ఉన్నాయి అని అనుకొని ప్రోగ్రాంను trace చేశాం.


అఖరున, x[id], అనగా “VOWELS” ఎక్కువగా ఉండే ప్రింట్ అవుతుంది..

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.


12. 2-D and multi-dimensional integer, float, long, double arrays
ఈ కింద 2-D integer arrays ఎలా declare చేయాలో చూపించాం.
I. int a[4][5];
ఇది 4 rows, 5 columns ఉండే 2-D integer array ను declare చేస్తుంది.

II. int a[4][5] = {{ 1, 7, 1}, { 7, 2, 1}, { 3, 1, 3}};
ఇది 4 rows, 5 columns ఉండే 2-D integer array ను declare చేస్తూ మనం assign చేసిన విలువలను కింది విధంగా స్టోర్ చేస్తుంది.

III. int a[][5] = {{ 1, 7, 1}, { 7, 2, 1}, { 3, 1, 3}};
ఇది 5 columns ఉండే 2-D integer array ను declare చేస్తూ మనం assign చేసిన విలువలను ఈ కింది విధంగా స్టోర్ చేస్తుంది. ఇక్కడా rows ఎన్నో మనం ఇవ్వలేదు. కాని assignment statement నుంచి మూడు rows గా కంపైలరు అనుకుంటుంది.

IV. int a[4][5]={1,2,2,2,2,2,1,1,1};
ఇది 4 rows, 5 columns ఉండే 2-D integer array ను declare చేస్తూ మనం assign చేసిన విలువలను కింది విధంగా స్టోర్ చేస్తుంది.

V. int a[][5]={1,2,2,2,2,2,1,1,1};
ఇది 4 rows, 5 columns ఉండే 2-D integer array ను declare చేస్తూ మనం assign చేసిన విలువలను కింది విధంగా స్టోర్ చేస్తుంది.

VI. int a[2][4][5];
ఇది ఒక3-D integer arrayను దానిలో 2 plane లు 4x5 size ఉండేటట్లు declare చేస్తుంది.

VII. int a[2][4][5]={ {{1,2}, {1,4}}, {{3,4},{4,5}}};
ఇది 4 rows, 5 columns ఉండే 2-D integer array ను declare చేస్తూ మనం assign చేసిన విలువలను కింది విధంగా స్టోర్ చేస్తుంది.

VIII. int a[][4][5]={ {{1,2}, {1,4}}, {{3,4},{4,5}}};
ఇది 4 rows, 5 columns ఉండే 2-Dinteger array ను declare చేస్తూ మనం assign చేసిన విలువలను కింది విధంగా స్టోర్ చేస్తుంది.

IX. int a[2][4][5]={1,2,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,9,4,5};
ఇది 4 rows, 5 columns ఉండే 2-D integer array ను declare చేస్తూ మనం assign చేసిన విలువలను కింది విధంగా స్టోర్ చేస్తుంది.

X. int a[][4][5]={1,2,3,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,9,4,5};
ఇది 4 rows, 5 columns ఉండే 2-D integer array ను declare చేస్తూ మనం assign చేసిన విలువలను కింది విధంగా స్టోర్ చేస్తుంది. ఇక్కడ data నుంచి two planes ఉంటాయని క0పైలరు అనుకొంటుంది.

Example 4: ఈ ప్రోగ్రాం 2-D integer array లోపలికి డాటా ఎలా element తర్వాత element ఎలా రీడ్ చేయాలి, ఎలా ప్రింట్ చేయాలో చూపిస్తుంది.    

#include
int main()
{
     int i, j, m, n, a[4][5];
     printf(“Enter number of rows and columns\n”);
     scanf(“%d %d”, &m, &n);

     /* Reading a 2-D Array */
     for(i=0; i < m; i++){
     for(j=0; j < n; j++) {
     printf(”Enter next element value\n”);
     scanf(“%d”, &a[i][j]);
     }
     }
     /* Printing a 2-D Array */
     printf("Given matrix\n");
     for(i=0; i < m; i++){
     for(j=0; j < n; j++) printf(“%d\t”, a[i][j]);
     printf(“\n”);
     }
     return (0);
}

ఈ కింది టేబుల్ 2x3 size 2-D array లోపలికి డాటా ఎలా రీడ్ చేస్తుందో చూపిస్తుంది.

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.

Example 5: ఈ కింది ప్రోగ్రాం ఒక square matrix లోపలికి డాటా రీడ్ చేసి దాని “Principal Diagonal” elements యొక్క టోటల్ ప్రింట్ చేస్తుంది.
Solution: పైన చెప్పిన విధంగా square matrix లోపలికి డాటా రీడ్ చేస్తాం. Principal Diagonal elements అనగా a[0]0], a[1][1], a[2][2], మొదలైనవి. కనుక ఒక లూప్ రాసి వాటి టోటల్ కనుక్కుంటాం.

#include
int main()
{
     int i, j, n, s1 = 0, a[20][10];
     printf(”Enter square matrix size\n”);
     scanf(“%d”, &n);
     
     /* Reading the Data*/
     printf(”Enter Matrix Data\n”);
     for(i=0; i < n; i++)
     for(j=0; j < n; j++) {
     printf(”Enter element value\n”);
     scanf(“%d”, &a[i][j]);
     }

     /* Calculating Diagonal Sums */

     for(i=0; i < n; i++){
     s1 += a[i][i];
     }

     printf(“Principal Diag. Sum=%d \n”, s1);
     return (0);
}
ఈ కింద డాటా అనుకొని, ప్రోగ్రాంను trace చేస్తాం.


పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.

Example 6: ఈ ప్రోగ్రాం ఒక square matrix లోపలికి డాటా రీడ్ చేసి అది symmetric matrix అవునా? కాదా? అని ప్రింట్ చేస్తుంది.
Solution: ఒక matrix ను symmetric అనాలంటే దాని transpose దానిలాగే ఉండాలి. దీనినే మరొక విధంగా కూడా చెప్పవచ్చు. దాని lower triangular portion లో ఉన్నelements అన్నీ upper triangular portion లోఉన్నelements తో సమానంగా ఉంటే అది symmetric matrix అవుతుంది. అంటే, elements
a[1][0],
a[2][0], a[2][1]
a[3][0],a[3][1],a[3][2]
ఈ కింది elements లాగా ఉండాలి.
a[0][1], a[0][2], a[0][3]
a[1][2], a[1][3],
a[2][3].

#include
int main()
{
     int i, j, n, a[20][10];
     printf(”Enter square matrix size\n”);
     scanf(“%d”, &n);
     /* Reading the Data*/
     for(i=0; i < n; i++)
     for(j=0; j < n; j++) {
     printf(”Enter element value\n”);
     scanf(“%d”, &a[i][j]);
     }

     for(i=1; i      for(j=0; j      if( a[i][j] !=a[i][n – 1 – i ]){
     printf(“No\n”);
     exit(-1);
     }
     }

     printf(“Yes\n”);
     return (0);
}

ఈ కింది డాటా అనుకొని, పై ప్రోగ్రాంను trace చేశాం.

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.


Example 7: ఈ ప్రోగ్రాం ఒక square matrix లోపలికి డాటా రీడ్ చేసి దాని transpose ను అదే memory లో స్టోర్ చేస్తుంది. ఈ విధంగా స్టోర్ చేయడాన్ని In-situ, In-Core, In-Memory, In-Place operations అని అంటారు.
Solution: ఇది పై ప్రోగ్రాంకు చాలా దగ్గరగా ఉంటుంది. ఇక్కడ elements ను exchange చేస్తాం. అంటే,
a[1][0],
a[2][0], a[2][1]
a[3][0],a[3][1],a[3][2]
లను ఈ కింది వాటితో exchange చేస్తాం.
a[0][1], a[0][2], a[0][3]
a[1][2], a[1][3],
a[2][3].

#include
int main()
{
     int i, j, n, T, a[20][10];
     printf(”Enter square matrix size\n”);
     scanf(“%d”, &n);
     /* Reading the Data*/
     printf("Enter matrix data\n");
     for(i=0; i < n; i++)
     for(j=0; j < n; j++) {
     scanf(“%d”, &a[i][j]);
     }
     /* Exchanging*/
     for(i=1; i      for(j=0; j      T=a[i][j];
     a[i][j]=a[i][n-1-i];
     a[i][n-1-i]=T;
     }
     /* Printing the transpose matrix*/
     printf("Transpose\n");
     for(i=0; i < n; i++){
     for(j=0; j < n; j++) {
     printf(“%d\t”, a[i][j]);
     }
     printf(”\n”);
     }

     return (0);
}

పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.

Example 8: ఈ కింద ఇచ్చిన ప్రోగ్రాం రెండు matrix లను multiply చేసి resultant matrix ను కాలిక్యులేట్ చేస్తుంది.
Solution: రెండు matrix లను multiply చేయాలంటే మొదటి matrix cols రెండో matrix rows తో సమానంగా ఉండాలి. m మొదటి matrix ప్రతీ rowను రెండో matrix ప్రతీ column తో multiply చేస్తాం.

#include
int main()
{
     int i, j, k, m1, n1, m2, n2, A[20][20], B[20][20], C[20][20];

     printf(“Enter the rows and columns of first matrix\n”);
     scanf(“%d%d”, &m1, &n1);
     printf(“Enter the rows and columns of second matrix\n”);
     scanf(“%d%d”, &m2, &n2);
     if(n1!=m2){
     printf(“Matrix multiplication is not possible\n”);
     exit(-1);
     }

     /* Reading first matrix*/
     printf(“Enter Data for first matrix\n”);
     for(i=0; i < m1; i++)
     for(j=0; j < n1; j++)
     scanf(“%d”, &A[i][j]);

     /* Reading first matrix*/
     printf(“Enter Data for second matrix\n”);
     for(i=0; i < m2; i++)
     for(j=0; j < n2; j++)
     scanf(“%d”, &B[i][j]);

     /* Calculating the product matrix*/
     for(i=0; i < m1; i++)
     for(j=0; j < n2; j++){
     C[i][j] = 0;
     for(k=0; k < n1; k++)
     C[i][j] += A[i][k] * B[k][j];
     }

     printf(“Resultant Matrix\n”);
     for(i=0; i < m1; i++){
     for(j=0; j < n2; j++)
     printf(“%d\t”, C[i][j]);
     printf(“\n”);
     }
     return (0);
}
ఈ కింద ఇచ్చిన matrix లు డాటా అనుకొని, పై ప్రోగ్రాంను trace చేస్తాం.


పైన ఇచ్చిన ప్రోగ్రాంను రన్ చేసినప్పుడు కంప్యూటరు మీద ఇలా ఉంటుంది.


13.3 Conclusions
ఈ పాఠంలో strings, 1-D arrays, 2-D character arrays, 2-D arrays గురించి నేర్చుకున్నాం.


 

 

 

NB Venkateswarlu
M.Tech(IIT-Kanpur)
Ph.D (BITS),PDF(UK),
Sr.Prof., CSE, AITAM, Tekkali

Posted Date : 03-02-2021 .