0% found this document useful (0 votes)
2 views5 pages

C Program for 2D Array Operations

The document provides a C program that reads and prints a 2D array, along with explanations on passing arrays to functions. It includes examples of different ways to declare functions that accept arrays as arguments and demonstrates how to calculate the minimum value and average of an array. The output of the programs is also included, showing the results of the computations.

Uploaded by

djjdhsha5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

C Program for 2D Array Operations

The document provides a C program that reads and prints a 2D array, along with explanations on passing arrays to functions. It includes examples of different ways to declare functions that accept arrays as arguments and demonstrates how to calculate the minimum value and average of an array. The output of the programs is also included, showing the results of the computations.

Uploaded by

djjdhsha5
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q .Write a c program to read & print 2d array .

#include<stdio.h>
int main()
{
int m,n,i,j,a[3][3];
printf(“enter number of rows and columns\n”);
scanf(“%d %d”,&m,&n);
printf(“eneter array elements\n”);
for(i=0;i ;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“array elements are\n”);
for(i=0;i ;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%d”,&a[i][j]);
}
}
Passing Array to Function as Arguments
In C, there are various general problems which requires passing more than one variable of the same type to a
function.
For example, consider a function which sorts the 10 elements in ascending order. Such a function requires 10
numbers to be passed as the actual parameters from the main function. Here, instead of declaring 10 different
numbers and then passing into the function, we can declare and initialize an array and pass that into the
function. This will resolve all the complexity since the function will now work for any number of values.
The array defined as the formal parameter will automatically refer to the array specified by the array name
defined as an actual parameter.
Consider the following syntax to pass an array to the function.

functionname(arrayname); //passing array

// At function call

Methods to declare a function that receives an array as an argument


There are 3 ways to declare the function which is intended to receive an array as an argument.

First way:
return_type function(type arrayname[])
Declaring blank subscript notation [] is the widely used technique.

Second way:
return_type function(type arrayname[SIZE])

Optionally, we can define size in subscript notation [].

Third way

return_type function(type *arrayname)

You can also use the concept of a pointer. In pointer chapter, we will learn about it.

C language passing an array to function example


#include <stdio.h>
int minarray(int arr[],int size){
int min=arr[0];
int i=0;
for(i=1;i<size;i++){
if(min>arr[i]){
min=arr[i];
}
}//end of for
return min;
}//end of function

int main(){
int i=0,min=0;
int numbers[]={4,5,7,3,8,9};//declaration of array

min=minarray(numbers,6);//passing array with size


printf("minimum number is %d \n",min);
return 0;
}
Output
minimum number is 3

#include <stdio.h>

float average(int arr[5]);

int main()
{ int arr[] = {10, 34, 21, 78, 5};

float avg = average(arr);

printf("average: %f", avg);

float average(int arr[5])

{ int sum=0; int i;

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

{ printf("arr[%d]: %d\n", i, arr[i]);

sum+=arr[i];

return (float)sum/5;

Output

arr[0]: 10

arr[1]: 34

arr[2]: 21

arr[3]: 78

arr[4]: 5

average: 29.600000

# include <stdio.h>
float average(int arr[], int length);
int main()
{ int arr[] = {10, 34, 21, 78, 5};
int length = sizeof(arr)/sizeof(int);
float avg = average(arr, length); printf("average: %f", avg);
}
float average(int arr[], int length)
{ int sum=0;
int i;
for (i=0; i<length; i++)
{ printf("arr[%d]: %d\n
i, arr[i]);
sum+=arr[i];
}
return (float)sum/length;
}
arr[0]: 10
arr[1]: 34
arr[2]: 21
arr[3]: 78
arr[4]: 5
average: 29.600000

You might also like