Date post: | 04-Jun-2018 |
Category: |
Documents |
Upload: | arun-kumar |
View: | 231 times |
Download: | 0 times |
of 26
8/13/2019 Passing Array to Functions
1/26
PASSING ARRAY TO FUNCTIONS
8/13/2019 Passing Array to Functions
2/26
CONTETNTS
HOW TO PASS ONE-D ARRAY
HOW TO PASS 2-D ARRAY
8/13/2019 Passing Array to Functions
3/26
Rules to pass an array to function
The function must be called by passing only
the name of the array.
In the function definition, the formal
parameters must be an array type, the size of
the array does not need to be specified.
The function prototype must show that the
argument is an array.
8/13/2019 Passing Array to Functions
4/26
One-Dimensional array
int a[100];
//Funtion call
largest(a,n);//funtion definition
int largest(int x[ ], int size)
8/13/2019 Passing Array to Functions
5/26
Write a C function which takes an array of
integers as its argument and returns the
average of all array elements. Also write the
associated main function, which reads the
array and prints the average.
May/June 2010 (8 Marks)
8/13/2019 Passing Array to Functions
6/26
#include
#include
float avg(int,int[]);// function declaration
main()
{
int i,a[100],n;
float avg1;
clrscr();
printf("Enter size of array \n");
scanf("%d",&n);printf("Enter array a %d elements\n",n);
for(i=0;i
8/13/2019 Passing Array to Functions
7/26
avg1=avg(n,a);// function call
printf("average=%f",avg1);
getch();}
float avg(int s,int x[])//function defintion
{
float i,sum=0,avg2;
for(i=0;i
8/13/2019 Passing Array to Functions
8/26
Write a c function which takes array of N
integers and to sort them using selection sort.
Use it in the main function to sort N array
elements using selection sort technique.
8/13/2019 Passing Array to Functions
9/26
#include
#include
void ssort(int,int[]);
int i,j,pos;
main()
{
int a[100],n;clrscr();
printf("Enter Array size\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);for(i=0;i
8/13/2019 Passing Array to Functions
10/26
ssort(n,a);printf("The sorted array is \n");
for(i=0;i
8/13/2019 Passing Array to Functions
11/26
void ssort(int s,int x[])
{ int temp;
for(i=0;i
8/13/2019 Passing Array to Functions
12/26
Write a function that finds the smallest of N
numbers in an array. Use it in a main function
to find the smallest of arrays A,B,C, and D each
with N elements.
Dec-06/Jan-07
(12 marks)
8/13/2019 Passing Array to Functions
13/26
Write a C functions
* to read N array elements* to print N array elements
* to sort N array elements in ascending order
using bubble sort.Using these functions write a C program to sort
A, B and C arrays of N elements.
June-July-2008
(12 marks)
8/13/2019 Passing Array to Functions
14/26
#include
#include
void read(int[],int);
void bubble(int[],int),print(int[],int);
main()
{
int a[100],b[100],c[100],i,j,n;
printf("enter array size\n");
scanf("%d",&n);
8/13/2019 Passing Array to Functions
15/26
printf("enter %d elements\n",n);
read(a,n);
printf("Enter %d elemnts\n",n);
read(b,n);printf("Enter %d elements\n");
read(c,n);
8/13/2019 Passing Array to Functions
16/26
bubble(a,n);
printf("Sorted A array elements\n");
print(a,n);
bubble(b,n);
printf("Sorted B array elements\n");
print(b,n);
bubble(c,n);
printf("Sorted C array elements\n");
print(c,n);
getch();
}
8/13/2019 Passing Array to Functions
17/26
void read(int s[100],int x)
{
int i;
for(i=0;i
8/13/2019 Passing Array to Functions
18/26
void print(int s[100],int x)
{
int i;
for(i=0;i
8/13/2019 Passing Array to Functions
19/26
void bubble(int s[100],int x)
{
int i,j,t;
for(i=0;i
8/13/2019 Passing Array to Functions
20/26
Write a user defined function to find the
product of two matrices of order (n*n) and
use it in a main function to compute
A3+A2+A. Where A is a matrix of order
(n*n)
(20 marks)
July-2007
8/13/2019 Passing Array to Functions
21/26
#include
#include
int a[10][10],n,b[10][10],c[10][10],i,j,k,q[10][10],
z[10][10];
void matmul(int[10][10],int[10][10],int[10][10],int);
main()
{
clrscr();
printf("Enter order of matrix(n*n)\n");
scanf("%d",&n);
8/13/2019 Passing Array to Functions
22/26
printf("Enter %d elements\n",n*n);
for(i=0;i
8/13/2019 Passing Array to Functions
23/26
matmul(a,b,c,n);
printf("A square is\n");
for(i=0;i
8/13/2019 Passing Array to Functions
24/26
matmul(a,c,q,n);
printf("A cube is\n");
for(i=0;i
8/13/2019 Passing Array to Functions
25/26
printf("Z matrix is\n");
for(i=0;i
8/13/2019 Passing Array to Functions
26/26