Professional Documents
Culture Documents
#include <stdio.h>
int main()
{
int c[]={2,3,4};
display(c[2]);
return 0;
}
void display(int a)
{
printf("%d",a);
}
Output
4
Example:
Passing entire one-dimensional array to a function
#include <stdio.h>
float average(float a[]);
int main(){
float avg, c[]={23.4, 55, 22.6, 3, 40.5, 18};
avg=average(c); /* Only name of array is passed as argument. */
printf("Average age=%.2f",avg);
return 0;
}
float average(float a[])
{
int i;
float avg, sum=0.0;
for(i=0;i<6; i++)
{
sum+=a[i];
}
avg =(sum/6);
return avg;
}
Output
Average age=27.08
Passing Multi-dimensional Arrays to Function
Example
#include <stdio.h>
void Function(int c[2][2]);
int main()
{
int c[2][2],i,j;
printf("Enter 4 numbers:\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{
scanf("%d",&c[i][j]);
}
Function(c); /* passing multi-dimensional array to function */
}
void Function(int c[2][2])
{
int i,j;
printf("Displaying:\n");
for(i=0;i<2; i++)
for(j=0;j<2; j++)
printf("%d\n",c[i][j]);
}
Output
Enter 4 numbers:
2
3
4
5
Displaying:
2
3
4
5
The array construct II
23-Topics Covered: Operation on arrays - simple sorting and searching, searching an element from
array
Simple sorting
Sorting is the process of arranging data into meaningful order so that you can analyze it more
effectively. For example, you might want to order students’ result data. In the following example, two
elements are compared. If second element is greater than first element then they are swapped with the
help of a variable. This process uses nested for loop.
#include<stdio.h>
int main()
{
int a[30], ele, num, i;
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter the values :");
for (i = 0; i < num; i++)
{
scanf("%d", &a[i]);
}
//Read the element to be searched
printf("\nEnter the elements to be searched :");
scanf("%d", &ele);
//Search starts from the zeroth
location
i = 0;
while (i < num)
{
if (ele== a[i])
{
printf("Number “ %d “ found at the location = %d",ele, i);
}
else
{
printf("Number not found");
}
i++;
}
return (0);
}
Output
Enter no of elements : 5
11 22 33 44 55
Enter the elements to be searched : 44
Number found at the location = 4