• facebook
  • whatsapp
  • telegram

Passing 2-D arrays to functions

return_type function_name(Type arg[ ][5],….)

ఒక function లోనికి ఒక 2-D array argument అని చెప్పడానికి, ఇలా రాస్తాం.
అంటే 2-D array column size తప్పక ఇవ్వాలి. దీని అర్ధం, ఆ function ను call చేస్తున్నప్పుడు ఏ 2-D array అయినా పంపవచ్చు, కాని దాని column size మనం ఇచ్చిన దానితో సమానంగా ఉండాలి.

Example 1: ఇక్కడ ఒక 2-D arrayను argument తీసుకొనే function ను రాశాం.ఇది array elements టోటల్ ను రిటర్న్ చేస్తుంది. ఒక main program దానిని call చెయ్యడం ఎలాగో ఈ ప్రోగ్రాం ద్వారా తెలుసుకుందాం.

#include
     int SUM( int x[ ][5], int m, int n)
     {
     int i, j, s = 0;
     for(i = 0; i < m; i++)
     for(j = 0; j < n; j++) s +=x[i][j];
     return(s);
     }
int main()
{
     int a[10][5] = { {1, 2, 3}, {3, 2, 1}, {4, 2, 3} };
     printf(“%d\n”, SUM(a, 3, 3));
     /* here array a is acceptable as column size matching*/
     return (0);
}

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


Example 2: ఒక square matrix ను argument గా తీసుకుని అది “Symmetric Matrix” అయితే 1 లేకపోతే 0 రిటర్న్ చేసే function రాస్తున్నాం. ఒక main program దానిని call చెయ్యడం ఎలాగో ఈ ప్రోగ్రాం ద్వారా తెలుసుకుందాం.

#include
     int SYM( int x[ ][5], int n)
     {
     int i, j;
     for(i = 1; i < n; i++)
     for(j = 0; j < i; j++) if(x[i][j]!=x[j][i]) return(0);
     return(1);
     }
int main()
     {
     int a[10][5] = { {1, 2, 3}, {3, 2, 1}, {4, 2, 3} };
     (SYM(a, 3))?printf(“Yes\n”):printf(“No\n”);
     return (0);
}

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


14.1.5. Recursive Functions

ఇప్పటి వరకూ ఒక function ను వేరే function లో నుంచి call చేసే functions రాశాం. అలా కాకుండా, ఒక function దానికదే call చేసుకుంటే దానిని recursive function అని అంటాం. దాని గురించి ఇప్పుడు నేర్చుకుందాం.

Example 1: ఈ recursive function రెండు పూర్ణాంకాలు (integers) ను తీసుకుని వాటి productను రిటర్న్ చేస్తుంది. ఒక main program దానిని call చెయ్యడం ఎలాగో చూద్దాం.
ఇక్కడ, x*y కావాలంటే, y ను x సార్లు add చేయడం అని అనుకున్నాం.

#include
     int prod(int x, int y)
     {
     if(x==1) return y;
     else
     return( y+prod(x-1,y));
     }
int main()
{
     int M, N, result;
     printf(“Enter two numbers\n”);
     scanf(“%d%d”, &M,&N);
     result=prod(M,N);
     printf(“Result=%d”, result);
     return 0
}

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

Example 2: ఈ recursive function ఒక integerను argument లాగా తీసుకుని దాని digits మొత్తాన్ని రిటర్న్ చేస్తంది. ఒక main program దానిని call చెయ్యడం ఎలాగో చూద్దాం.

#include
int SUM(int x)
     {
     if(x/10) return(x%10+ SUM(x/10));
     else return(x);
     }
int main()
     {
     int Num;
     printf(“Enter an integer\n”);
     scanf(“%d”, &Num);
     printf(“Digits sum=%d\n”, SUM(Num));
     return 0;
}

ఈ కింది Num విలువ 1976 అయినప్పుడు function call ఎలా పని చేస్తుందో చూపించాం.

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

Example 3:ఈ recursive function ఒక integerను తీసుకుని దాని factorial valueను రిటర్న్ చేస్తుంది. ఒక main program దానిని call చెయ్యడం ఎలాగో చూద్దాం.

అంటే, n factorial కావాలంటే (n-1) factorial కనుక్కొని దానిని n తో గుణిస్తే సరిపోతుంది. అలాగే, n-1 factorial కావాలంటే (n-2) factorialకనుక్కొని దానిని (n-1) తో గుణిస్తే సరిపోతుంది.

#include
long fact(int n)
     {
     if(n==0) return 1;
     else
     return (n*fact(n-1));
     }
int main()
     {
     int Num;
     printf(“Enter an integer\n”);
     scanf(“%d”, &Num);
     printf(“Factorial Value of “%ld=%ld\n”, fact(Num));
     return 0;
}

ఈ కింది టేబుల్ n=4 అయినప్పుడు, recursive function ఎలా పని చేస్తుందో చూపిస్తుంది.

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

14.1.6. Storage Classes

Variables ను వాటి లైఫ్, స్కోప్ ను బట్టి ఈ కింది విధంగా విభజిస్తారు. లైఫ్ అంటే వాటికి allocate చేసిన memory ఎంత వరకు ఉంటుంది. స్కోప్ అంటే వాటిని ఎక్కడ వాడుకోవచ్చు.

Example 1: ఈ కింది ప్రోగ్రాంలో static తీసివేసి ఫలితాన్ని గమనించండి.

#include
void F()
     {
     static int a=10;
     printf(“%d\n”,a++);
     }
int main()
{
     F();
     F();
     F();
}

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

14.1.7. Macros

ఒక functionను call చేసినప్పుడు, కొంత సమయం వేచి ఉండాలి . ఒక్కోసారి, function లోపల చేసే కాలిక్యులేషన్లకు పట్టే సమయం ఎక్కువైనట్లయితే function బదులు macro రాస్తాం (ఉదాహరణకు, ఒక రూపాయి money order ద్వారా పంపాలంటే 18 రూపాయలు ఖర్చవుతుంది. అప్పుడు, మనం ఏమి చేస్తాం? ఒక రూపాయి కవరు లో పెట్టీ పోస్ట్ చేస్తాం). ఇంతకు ముందు మనం వాడిన, isupper, islower, toupper అనేవి macros.

Macros వాడిన చోట వాటి బాడీ (definition) substitute అవుతుంది. ఒక్కోసారి, ఇది మన టైపింగ్ భారాన్ని తగ్గిస్తుంది. అలాగే, macros లో టైప్ చెకింగ్ వీలు కాదు.

Example 1: ఈ ప్రోగ్రాంలో LOOP అనే macroను define చేసి దానిని mainలో వాడాం. ఎక్కడ LOOP వాడామో అక్కడ for(i=0;i

#include
#define LOOP for(i=0;i int main()
{
     int i,n, a[10];
     printf("Enter number of integers \n");
     scanf("%d",&n);
     printf(Enter %d integers in to the array \n,n");
     LOOP
     scanf("%d",&a[i]);
     printf("numbers given are\n");
     LOOP
     printf("%d\n", a[i]);
     return(0);
}

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

Example 2: ఈ ప్రోగ్రాంలో కూడా LOOP అనే macroను define చేసి దానిని mainలో వాడాం. కానీ, ఇందులో దానికి కొన్నింటిని argumentsగా ఇచ్చాం. LOOP ఎక్కడ వాడామో అక్కడ for(i=0;i

#include
#define LOOP(i,n) for(i=0;i int main()
{
     int i,k,n, a[10];
     printf("Enter an integer\n");
     scanf("%d", &n);
     LOOP(i,n) /* for(i=0; i      scanf("%d", &a[i]);
     LOOP(k,n) /* for(k=0; k      printf("%d\n", a[k]);
     return(0);
}

Example 3: ఈ ప్రోగ్రాం పైన ఇచ్చిన macroను వేరొక ప్రోగ్రాంలో ఎలా వాడాలో చూపిస్తు0ది.

#include
#define LOOP(i,n) for(i=0;i int main()
{
     int i,j,n, a[10][10];
     printf("Enter size of square matrix\n");
     scanf("%d", &n);
     printf("Enter the data\n");
     LOOP(i,n) /* for(i=0; i      

     LOOP(j,n) /* for(j=0; j      scanf("%d", &a[i][j]);

     “printf("Printing the data\n");”

     LOOP(i,n)
     { /* for(i=0; i      LOOP(j,n) /* for(j=0; j      printf("%d\t", a[i][j]);
     printf("\n");
     }
return(0);
}

Example 4: ఈ ప్రోగ్రాంలో ఒక macro definition ఒక్క line కన్నా ఎక్కువ అయితే ఎలాగో చూపిస్తుంది. చివరి లైనుకు తప్ప మిగతా అన్నింటికి చివరలో back slash (\) ఇవ్వాలి.

#include “printf("Printing the data\n");”
#define LOOP(i,j,n) for(i=0;i      for(j=0;j      int main(){
     int i,j,n, a[10][10];
     printf("Enter size of square matrix\n");
     scanf("%d", &n);
     printf("Enter the data\n");
     LOOP(i,j,n) /* Two lines of the macro are substituted*/
     scanf("%d", &a[i][j]);
     LOOP(i,j,n) /* Two lines of the macro are substituted*/
     printf("%d\t", a[i][j]);
     return(0);
}

Example 5: ఈ ప్రోగ్రాం 5, 4 లను ఫలితంగా ఇస్తుంది. అనలైజ్ చేయండి. మీకే తెలుస్తుంది.

#include
#include
#define norm(a,b) sqrt(a*a+b*b)
int main()
{
     float p=3,q=4,r,s;
     r=norm(p,q); /* r=sqrt(p*p+q*q);*/
     s=norm(p+1,q+1); /* s=sqrt(p+1*p+1+q+1*q+1);*/
     printf(“%f %f\n”,r,s);
     return 0;
}

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

Conclusions
ఈ పాఠంలో functions రాయడమెలాగో నేర్చుకొన్నాం. అలాగే recursive functions, macros, storage classes, గరించి కూడా తెలుసుకున్నాం.

 

 

 

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

Posted Date : 04-02-2021 .