0% found this document useful (0 votes)
202 views

Passing Array To Functions

The document discusses passing arrays to functions in C programming. It provides examples of passing both one-dimensional and two-dimensional arrays as arguments to functions. The examples show how to define functions to accept array arguments, call functions and pass array variables, and manipulate array elements within functions. Various functions are defined to demonstrate sorting arrays, calculating averages, and multiplying matrices using passed array arguments.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
202 views

Passing Array To Functions

The document discusses passing arrays to functions in C programming. It provides examples of passing both one-dimensional and two-dimensional arrays as arguments to functions. The examples show how to define functions to accept array arguments, call functions and pass array variables, and manipulate array elements within functions. Various functions are defined to demonstrate sorting arrays, calculating averages, and multiplying matrices using passed array arguments.

Uploaded by

Arun Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

PASSING ARRAY TO FUNCTIONS

CONTETNTS
HOW TO PASS ONE-D ARRAY HOW TO PASS 2-D ARRAY

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.

One-Dimensional array
int a[100]; //Funtion call largest(a,n); //funtion definition int largest(int x[ ], int size)

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)

#include<stdio.h> #include<conio.h> 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<n;i++) scanf("%d",&a[i]);

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<s;i++) sum=sum+x[i]; avg2=sum/s; return avg2; }

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.

#include<stdio.h> #include<conio.h> 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<n;i++) scanf("%d",&a[i]);

ssort(n,a); printf("The sorted array is \n"); for(i=0;i<n;i++) printf("%d\n",a[i]); getch(); }

void ssort(int s,int x[]) { int temp; for(i=0;i<s-1;i++) { pos=i; for(j=i+1;j<s;j++) { if(x[j]<x[pos]) pos=j; } temp=x[i]; x[i]=x[pos]; x[pos]=temp; } }

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)

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)

#include<stdio.h> #include<conio.h> 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);

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);

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(); }

void read(int s[100],int x) { int i; for(i=0;i<x;i++) scanf("%d",&s[i]); }

void print(int s[100],int x) { int i; for(i=0;i<x;i++) printf("%d\n",s[i]); }

void bubble(int s[100],int x) { int i,j,t; for(i=0;i<x-1;i++) for(j=0;j<x-i-1;j++) if(s[j]>s[j+1]) { t=s[j]; s[j]=s[j+1]; s[j+1]=t; } }

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

#include<stdio.h> #include<conio.h> 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);

printf("Enter %d elements\n",n*n); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); for(i=0;i<n;i++) for(j=0;j<n;j++) b[i][j]=a[i][j];

matmul(a,b,c,n); printf("A square is\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%5d",c[i][j]); printf("\n"); }

matmul(a,c,q,n); printf("A cube is\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%5d",q[i][j]); printf("\n"); }

printf("Z matrix is\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) z[i][j]=a[i][j]+c[i][j]+q[i][j]; for(i=0;i<n;i++) { for(j=0;j<n;j++) printf("%5d",z[i][j]); printf("\n"); } getch(); }

void matmul(int m[10][10],int l[10][10],int p[10][10],int x) { for(i=0;i<x;i++) for(j=0;j<x;j++) { p[i][j]=0; for(k=0;k<x;k++) p[i][j]=p[i][j]+m[i][k]*l[k][j]; } }

You might also like