• facebook
  • whatsapp
  • telegram

Passing 1-D Arrays to functions

ఒక function లోకి ఒక 1-D array argument అని చెప్పడానికి, ఈ కింది విధంగా empty brackets పెడతాం.
return_type function_name ( Type arg[ ], …..)
Example 1: ఏదైనా function ఒక stringను తీసుకుని దాని length రిటర్న్ చేయాలి. ఒక main program దానిని call చెయ్యడం ఎలాగో చూద్దాం.

#include
     int Length(char x[] ) /* Here, x is a string variable*/
{
     int i = 0;
     while(x[i] != ‘\0’) i++;
     /* We know after this while loop i value becomes number of characters*/
     return(i);
}
int main()
{
     int i, j, k;
     char x[20], y[20] = “Ram”;
     printf ("Enter a string”);
     scanf(“%s”, x);
     i = Length(x);
     /* We are sending string variable x for which data is read */
     j = Length(y);
     /* We are sending string variable for which data is assigned*/
     k = Length(“Ram”);
     /* We are directly sending the string constant*/
     /* All the three are acceptable forms for this function call*/

     printf(“Lengths of strings are=%d %d %d\n”, i, j, k);
     return (0);
}

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


Example 2: ఏదైనా function ఒక string తీసికొని అది palindrome అయితే 1 లేకపోతే 0 రిటర్న్ చేయాలి. ఇందులో పైన రాసిన Length అనే function ను వాడుతున్నాం. ఒక main program దానిని call చెయ్యడం ఎలాగో చూద్దాం.

#include
int length(char x[]) /* Here, x is a string variable*/
{
     int = 0;
     while (x[i]!='\0') i++;

     /* We know after this while loop i value becomes number of characters*/
     return (i);
}
int Palindrome(char x[] )
{
     int i, j;
     for( i = 0, j = Length(x) – 1; i < j; i++, j--)
     {
     if(x[i] != x[j]) return(0);
     }
     return( 1 );
}
int main()
{
     char p[20];
     printf(“Enter a string\n”);
     scanf(“%s”, p);
     (Palindrome(p))?printf(“Yes\n”):printf(“No\n”);
     return (0);
}

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


సాధారణంగా variables (scalar variables) function లోనికి వెళ్తే వాటి formal arguments మీద ఏం జరిగినా ఇవి మారవు. అదే arrays అయితే మార్పులు పంపిన arraysలో కనిపిస్తాయి.
Example 3: ఈ function ఒక string, రెండు character టైప్ arguments rc, cr తీసుకొని ఇచ్చిన stringలో ఎక్కడ rc ఉంటుందో అక్కడ cr ను పెడుతుంది. ఒక main program దానిని call చెయ్యడం ఎలాగో చూపించడానికి రాశాం.

#include
void REPLACE(char x[], char rc, char cr)
{
     int i = 0;
     while(x[i] != ‘\0’){
     if(x[i]==rc) x[i] = cr;
     i++;
     }
}
int main()
{
     char p[20];
     printf(“Enter a string\n”);
     scanf(“%s”, p);

     REPLACE(p, ’a’, ‘*’);
     printf(“%s\n”, p);
     return (0);
}

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

Example 4: ఒక function ఒక one dimensional integer arrayను, ఒక integer ను తీసుకొని ఇచ్చిన array elements టోటల్ ను రిటర్న్ చేస్తుంది. ఒక main program దానిని call చెయ్యడం ఎలాగో తెలుసుకుందాం.

#include
int Sum(int a[], int n )
{
     int i, S = 0;
     for(i =0; i < n; i++) S += a[i];
     return(S);
     }
int main()
{
     int x[20] = { 10, 70, 60, 40, 30, 45 }, S1, S2;
     S1 = Sum( x, 4);
     /* It calculates the sum of first 4 elements*/
     S2 = Sum( x, 6);
     /* It calculates the sum of first 6 elements*/
     printf(“Four Elem. Sum=%d\nSix Elem. Sum=%d\n”, S1, S2);
     return (0);
}

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

Example 5: ఒక function ఒక one dimensional integer arrayను, ఒక integer ను తీసుకొని ఇచ్చిన array elements లో పెద్ద దాని indexను రిటర్న్ చేస్తంది.. మరియూ, ఒక main program దానిని call చెయ్యడం ఎలాగో చూద్దాం.

#include
int MI( int a[], int n )
{
     int i, id = 0;
     for(i =1; i < n; i++) if( a[i] > a[id]) id = i;
     return(id);
}
int main()
{
     int x[20] = {10, 17, 20, 56, 97, 32 }, K;
     K = MI (x, 6); /* Function call*/
     printf(“%d\n”, x[K]);

     return (0);
}

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

Example 6: ఒక function ఒక one dimensional integer array, ఒక integer ను తీసుకొని ఇచ్చిన array elements average, maximum, minimum లను రిటర్న్ చేస్తుంది. ఈ మూడింటినీ ఒక dummy array లో స్టోర్ చేస్తాం. మామూలుగా return statement ద్వారా ఒక్క విలువను మాత్రమే రిటర్న్ చేయగలం. అందుకు ఒక dummy array ను వాడుతున్నాం.ఒక main program దానిని call చెయ్యడం ఎలాగో చూద్దాం.

#include
void STAT1( int a[ ], int n, int b[ ] )
{
     int i, S = a[0], max = a[0], min = a[0];
     for(i =1; i < n; i++){
     S += a[i];
     if(a[i] > max) max = a[i];
     if(a[i] < max) min = a[i];
}
     b[0] = S/n;
     b[1] = max;
     b[2] = min;
}
int main()
{
     int x[ ] = { 70, 71, 56, 97, 66 }, res[3];
     STAT1(x, 5, res);
     /* res array contains average, maximum and minimum*/
     printf(“Average=%d Maximum=%d Minimum=%d\n”, res[0], res[1], res[2]);
     return (0);

}

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

 

 

 

 

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

Posted Date : 03-02-2021 .