13 - ICS 103 Computer Programming in C Handout-13 Topic 1-D Array(How to use array with function Objective Learn how to use one-dimensional arrays with

# 13 - ICS 103 Computer Programming in C Handout-13 Topic 1-D...

This preview shows page 1 - 3 out of 8 pages.

ICS 103: Computer Programming in C Handout-13 Topic: 1-D Array (How to use array with function). Objective: Learn how to use one-dimensional arrays with function. To know how to pass individual 1-D array elements to function. To know how to pass whole 1-D array to the function. Using Arrays with Functions: How to use individual array elements as parameters in function? : This is similar to passing simple arguments The formal parameter should be a simple variable The actual parameter should be the array name followed by the particular index in [ ]. Solved Problem# 1: /*************************************************************** Write a program to find the sum of first three elements in the array, by passing individual array elements to the function. ****************************************************************/ #include<stdio.h> int sum_elements(int a , int b, int c); // Function Prototype void main() { int x[10], total; x[0]=10; x[1]=20; x[2]=30; total = sum_elements( x[0], x[1], x[2] );  //Function call to compute sum printf("For the array  values: %d %d %d\n", x[0], x[1], x[2] );   // Output results ICS 103 : Computer Programming in C , Term-032 ; Rafiq Khan Page 1 of 8
printf("\ nthe sum of first three array elements is : %d", total); } // end of main int sum_ele m e nts(  int a, int b, int c  )   // Function header { int sum; sum=a+b+c; return (sum); } Sample Output : /*************************************************************** Another Solution of Problem#1 ***************************************************************/ #include<stdio.h> int sum_elements(int a , int b, int c); // Function Prototype void main() { int x[10], total=0, i; printf("Please Input 5 element in array : "); for(i=0;i<5;i++) { scanf("%d", &x[i]); } total = sum_elements( x[0], x[1], x[2] ); //Function call to compute sum printf("\nThe  first three   array  elements  are  : %d  %d  %d",  x[0], x[1], x[2] );  // Output results ICS 103 : Computer Programming in C , Term-032 ; Rafiq Khan Page 2 of 8

#### You've reached the end of your free preview.

Want to read all 8 pages?

• Spring '10
• zaman
• Array, Subroutine, #define, Even and odd functions

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern