Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

50.Sending an array as an argument

 

Sending an array as an argument:

Like any primary type even an array can be send as an argument.

Here the name of array is specified as an actual argument.

An array is defined as a formal argument.

Elements of actual argument can be directly accessed by the formal argument.

Here it is optional to specify the dimension with the formal argument and proto type in case of single dimensional array and mandatory to two or multi dimensional arrays.

While sending an array as an argument even the size ("n") need to be send as an argument to control the loop in sub function.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


Output:

14        41        32        76        77        89

 

Specification1:

Accept "n" elements into an array and print the sum of all elements using the function sum()

 

Program:

#include<stdio.h>

int sum(int[],int);

void main()

{

int a[50],n,i,sm;

clrscr();

printf("How many elements?");

scanf("%d",&n);

printf("Enter %d elements:\n",n);

for(i=0;i<n;i++)

   scanf("%d",&a[i]);

sm=sum(a,n);

printf("Sum of all the elements %d",sm);

getch();

}

int sum(int x[],int n)

{

    int s,i;

    for(s=0,i=0;i<n;i++)

        s=s+x[i];

   return s;

}

 

Execution:

How many elements? 5

Enter 5 elements:

12   4   5   20   12

Sum of all the elements 53

 

Specification2:

Accept a nxm matrix and print the norm of matrix using the function getnorm().

 

Logic: Norm is the square root of sum of squares of all the elements.

 

Program:

#include<stdio.h>

#include<math.h>

double getnorm(int[50][50],int,int);

void main()

{

    int a[50][50],n,m,i,j;

    clrscr();

    printf("Enter the class of matrix:\n");

    scanf("%d%d",&n,&m);

    printf("Enter %dx%d matrix:\n",n,m);

    for(i=0;i<n;i++) 

      for(j=0;j<m;j++)

        scanf("%d",&a[i][j]);

   printf("The norm of matrix %lf",getnorm(a,n,m));

   getch();

}

double getnorm(int p[50][50],int n,int m)

{

    int i,j;

    long sum;

    for(sum=0,i=0;i<n;i++)

       for(j=0;j<m;j++)

          sum=sum+pow(p[i][j],2);   /* sum of squares of all the elements */

    return sqrt(sum); 

}

 

 

 

 

 

 

Execution:

Enter the class of matrix:

3    4

Enter 3x4 matrix:

1    2    3     4

4    3    2    1

1    2    4    3

The norm of matrix: 9.486833

 

Points to remember:

Point 1 In case of primary type change in formal argument results no change in actual argument because it follows pass by value.

  

Example:

#include<stdio.h>

void process(int,int);

void main()

{

    int x=10,y=20;

    clrscr();

    process(x,y);

    printf("x=%d",x);

    printf("\ny=%d",y);

    getch();

}

void process(int p,int q)

{

     p=p+10;

    q=q+10;

}

 

Output:

x=10

y=20

 

Example explained:

x, y are local variables of main().

p, q are local variables of process().

Only the values of x, y are assigned to p, q.

The change in p, q doesn't reflect the change in x, y.

 

Point2: In case of derived type (array) change in formal argument reflects the change in actual argument because it follows pass by reference (We will discourse it in pointers).

 

 

 

 


                      

 

 

 

                                                                                                                         

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

24    51     42    86    87    99

0 comments:

Post a Comment