You are on page 1of 9

The array construct II

23-Topics Covered: Passing arrays to the functions as argument,

Passing arrays to the functions as argument


Single element of an array can be passed in similar manner as passing variable to a
function.

Example: C program to pass a single element of an array to function

#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.

// C program to accept N numbers and arrange them in an ascending order


#include <stdio.h>
 void main()
{
int i, j, a, n, number[30];
 
printf("Enter the value of N :");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; i++)
{
scanf("%d", &number[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; i++)
printf("%d\n", number[i]);
}
Output

Enter the value of N: 6


Enter the numbers
3
78
90
456
780
200

The numbers arranged in ascending order are given below


3
78
90
200
456
780
Searching an element from array
Searching is the process to locate particular data from the given data.
Example

#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

You might also like