Page 1
ARRAYS USING FUNCTIONS
1-d arrays using functions
Passing individual array elements to a function
We can pass individual array elements as arguments to a function like other simple
variables.
Example:
#include<stdio.h>
void check(int);
void main()
{
int a[10],i;
clrscr();
printf(“\n enter the array elements:” );
for(i=0;i<10;i++)
{
scanf(“%d” , &a[i]);
check(a[i]);
}
void check(int num)
{
if(num%2==0)
printf(“%dis even\n” ,num);
else
printf(“%dis odd\n” ,num);
}
Page 2
ARRAYS USING FUNCTIONS
1-d arrays using functions
Passing individual array elements to a function
We can pass individual array elements as arguments to a function like other simple
variables.
Example:
#include<stdio.h>
void check(int);
void main()
{
int a[10],i;
clrscr();
printf(“\n enter the array elements:” );
for(i=0;i<10;i++)
{
scanf(“%d” , &a[i]);
check(a[i]);
}
void check(int num)
{
if(num%2==0)
printf(“%dis even\n” ,num);
else
printf(“%dis odd\n” ,num);
}
Output:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
Example:
C program to pass a single element of an array to function
#include <stdio.h>
void display(int a)
{
printf(” %d” ,a);
}
int main()
{
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;
}
Page 3
ARRAYS USING FUNCTIONS
1-d arrays using functions
Passing individual array elements to a function
We can pass individual array elements as arguments to a function like other simple
variables.
Example:
#include<stdio.h>
void check(int);
void main()
{
int a[10],i;
clrscr();
printf(“\n enter the array elements:” );
for(i=0;i<10;i++)
{
scanf(“%d” , &a[i]);
check(a[i]);
}
void check(int num)
{
if(num%2==0)
printf(“%dis even\n” ,num);
else
printf(“%dis odd\n” ,num);
}
Output:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
Example:
C program to pass a single element of an array to function
#include <stdio.h>
void display(int a)
{
printf(” %d” ,a);
}
int main()
{
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;
}
Output
2 3 4
Passing whole 1-D array to a function
We can pass whole array as an actual argument to a function the corresponding formal arguments
should be declared as an array variable of the same type.
Example:
# include <stdio.h>
main()
{
int i, a[6]={1,2,3,4,5,6};
func(a);
printf(“ contents of array:” );
for(i=0;i<6;i+ +)
printf(“%d” ,a[i]);
printf(”\n” );
}
func(int val[])
{
int sum=0,i;
for(i=0;i<6;i++)
{
val[i]=val[i] *val[i];
sum+=val[i];
}
printf(“ the sum of squares:%d” , sum);
}
Output
contents of array: 1 2 3 4 5 6
the sum of squares: 91
Page 4
ARRAYS USING FUNCTIONS
1-d arrays using functions
Passing individual array elements to a function
We can pass individual array elements as arguments to a function like other simple
variables.
Example:
#include<stdio.h>
void check(int);
void main()
{
int a[10],i;
clrscr();
printf(“\n enter the array elements:” );
for(i=0;i<10;i++)
{
scanf(“%d” , &a[i]);
check(a[i]);
}
void check(int num)
{
if(num%2==0)
printf(“%dis even\n” ,num);
else
printf(“%dis odd\n” ,num);
}
Output:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
Example:
C program to pass a single element of an array to function
#include <stdio.h>
void display(int a)
{
printf(” %d” ,a);
}
int main()
{
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;
}
Output
2 3 4
Passing whole 1-D array to a function
We can pass whole array as an actual argument to a function the corresponding formal arguments
should be declared as an array variable of the same type.
Example:
# include <stdio.h>
main()
{
int i, a[6]={1,2,3,4,5,6};
func(a);
printf(“ contents of array:” );
for(i=0;i<6;i+ +)
printf(“%d” ,a[i]);
printf(”\n” );
}
func(int val[])
{
int sum=0,i;
for(i=0;i<6;i++)
{
val[i]=val[i] *val[i];
sum+=val[i];
}
printf(“ the sum of squares:%d” , sum);
}
Output
contents of array: 1 2 3 4 5 6
the sum of squares: 91
Example.2:
Write a C program to pass an array containing age of person to a function. This function should
find average age and display the average age in main function.
#include <stdio.h>
float averagefloat 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 averagefloat 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
Solved Example:
1. Write a program to find the largest of n numbers and its location in an array.
#include <stdio.h>
Page 5
ARRAYS USING FUNCTIONS
1-d arrays using functions
Passing individual array elements to a function
We can pass individual array elements as arguments to a function like other simple
variables.
Example:
#include<stdio.h>
void check(int);
void main()
{
int a[10],i;
clrscr();
printf(“\n enter the array elements:” );
for(i=0;i<10;i++)
{
scanf(“%d” , &a[i]);
check(a[i]);
}
void check(int num)
{
if(num%2==0)
printf(“%dis even\n” ,num);
else
printf(“%dis odd\n” ,num);
}
Output:
enter the array elements:
1 2 3 4 5 6 7 8 9 10
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
Example:
C program to pass a single element of an array to function
#include <stdio.h>
void display(int a)
{
printf(” %d” ,a);
}
int main()
{
int c[]={2,3,4};
display(c[2]); //Passing array element c[2] only.
return 0;
}
Output
2 3 4
Passing whole 1-D array to a function
We can pass whole array as an actual argument to a function the corresponding formal arguments
should be declared as an array variable of the same type.
Example:
# include <stdio.h>
main()
{
int i, a[6]={1,2,3,4,5,6};
func(a);
printf(“ contents of array:” );
for(i=0;i<6;i+ +)
printf(“%d” ,a[i]);
printf(”\n” );
}
func(int val[])
{
int sum=0,i;
for(i=0;i<6;i++)
{
val[i]=val[i] *val[i];
sum+=val[i];
}
printf(“ the sum of squares:%d” , sum);
}
Output
contents of array: 1 2 3 4 5 6
the sum of squares: 91
Example.2:
Write a C program to pass an array containing age of person to a function. This function should
find average age and display the average age in main function.
#include <stdio.h>
float averagefloat 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 averagefloat 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
Solved Example:
1. Write a program to find the largest of n numbers and its location in an array.
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], maximum, size, c, location = 1;
clrscr();
printf("Enter the number of elements in array\n");
scanf("%d", &size);
printf("Enter %dintegers\n", size);
for (c = 0; c < size; c++)
scanf("%d", &array[c]);
maximum = array[0];
for (c = 1; c < size; c++)
{
if (array[c] > maximum)
{
maximum = array[c];
location = c+1;
}
}
printf("Maximum element is present at location %d and it's value is %d.\n", location,
maximum);
getch();
}
Output:
Enter the number of elements in array
5
Enter 5 integers
2
4
7
9
1
Read More