This document discusses passing arrays to functions in C programming. It provides examples of passing individual array elements, one-dimensional arrays, and multidimensional arrays to functions. The examples demonstrate declaring functions that accept array parameters, passing arrays from main to other functions, and accessing array elements within those functions. The output from each example program is also shown. The author is identified as Dr. Srinivas Yadlapaty with qualifications and contact information provided.
This document discusses passing arrays to functions in C programming. It provides examples of passing individual array elements, one-dimensional arrays, and multidimensional arrays to functions. The examples demonstrate declaring functions that accept array parameters, passing arrays from main to other functions, and accessing array elements within those functions. The output from each example program is also shown. The author is identified as Dr. Srinivas Yadlapaty with qualifications and contact information provided.
Qualification: Mtech - Computer Science(JNTU-Hyderabad), PHD in Computer Science and Engineering Mobile number: 9989601027 Mail id: [email protected] Experience(16 years): Worked as Assistant Professor, Associate Professor in several reputed engineering colleges(Avanthi, Sri Indu, SVIET, ST.Mary’s, Brilliant, Siddhartha engineering colleges in Hyderabad), Guest faculty for MBES Engineering college – Ambajogai - Maharashtra for java, Guest faculty for RK Engineering college - Vijayawada for Java, Guest faculty for MVR engineering college - Vijayawada for Java , Software Engineer in Grdemettle, Senior Software Engineer in Yaaltech, Working for Harvest online, Mumbai on Java and Python, Working for Infimind solutions, Bangalore on Python and Java. Currently working for St Mary’s Engineering College, Hyderabad. --------------------------------------------------------------------------------------------------------- Passing arrays to functions: Pass Individual Array Elements: Passing array elements to a function is similar to passing variables to a function. Program: #include <stdio.h> void display(int x, int y) { printf("%d\n",x); printf("%d\n",y); } void main() { int ar[] = {90,80,70,60,50}; clrscr(); // pass second and third elements to display() display(ar[1], ar[2]); getch(); } Output: Pass arrays to functions: Program: // Display marks of a student #include<stdio.h> void find(int marks[]); // This is also correct: void find(int marks[5]); void main() { int marks[]={10,20,30,40,50}; clrscr(); find(marks); getch(); } void find(int marks[]) { int i; printf("Marks obtained:\n"); for(i=0;i<5;i++) printf("%d\n",marks[i]); } Output: Program: // Program to calculate the sum of array elements by passing to a function #include <stdio.h> float sum(float num[]); void main() { float result, num[] = {90.24, 75, 82.7, 59, 60.9, 51}; clrscr(); // num array is passed to sum() result = sum(num); printf("Result = %.2f", result); getch(); } float sum(float num[]) { int i; float sum = 0.0; for (i = 0; i < 6; ++i) sum += num[i]; return sum; } Output: Pass Multidimensional Arrays to a Function: To pass multidimensional arrays to a function, only the name of the array is passed to the function (similar to one-dimensional arrays). Program: #include<stdio.h> void display(int num[2][2]); void main() { int num[2][2],i,j; clrscr(); printf("Enter 4 numbers:\n"); for (i = 0; i < 2; ++i) for (j = 0; j < 2; ++j) scanf("%d", &num[i][j]); // pass multi-dimensional array to a function display(num); getch(); } void display(int num[2][2]) { int i,j; printf(“The entered elements in matrix form is:\n”); for (i = 0; i < 2; ++i) { for (j = 0; j < 2; ++j) { printf("%d\t", num[i][j]); } printf("\n"); } } Output: