13 - ICS 103: Computer Programming in C Handout-13 Topic:...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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] );
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

13 - ICS 103: Computer Programming in C Handout-13 Topic:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online