printfBefore doubling for i 0 i SIZE i printf2ft xi for i 0 i SIZE i doubler xi

# Printfbefore doubling for i 0 i size i printf2ft xi

This preview shows page 18 - 24 out of 46 pages.

printf("Before doubling: "); for (i = 0; i < SIZE; ++i) printf("%.2f\t", x[i] ); for (i = 0; i < SIZE; ++i) doubler( &x[i] ); printf("\nAfter doubling: "); for (i = 0; i < SIZE; ++i) printf("%.2f\t", x[i] ); printf("\n"); system("pause"); return 0; } void doubler(double *x) { *x = *x * 2; } 18
USING ARRAYS AS FUNCTION ARGUMENTS In addition to passing individual elements of an array to functions, we can also write functions that take an entire array as a single argument . Such functions can manipulate some or all of the array elements. However, unlike scalar variables where we have the option of either passing value or reference (address) of a variables to a function, C allows only passing by reference for arrays . In this section, we learn how to write functions that take array as argument and how to call such functions. 19
ARRAY AS FORMAL PARAMETER TO FUNCTIONS To specify array as a formal parameter to a function, we put it as if we are declaring the array, but without specifying the size . void print_array (int a[], …); Not specifying the size will allow the function to be called with any size of array . However, the function needs to know the size of the actual array in order to process it correctly. The solution is to add an extra parameter indicating the size or actual number of elements in the array. void print_array(double a[], int size) 20
ARRAY AS ACTUAL ARGUMENT IN FUNCTION CALL To pass an array as actual argument to functions, we just give the array name without the brackets . print_array (a, …); Since functions that take array usually also need to know the size of the array or at least the number of elements in the array, the complete call to the print_array function might be: print_array(a, SIZE); Note that passing array as argument to functions is pass by reference not pass by value. Thus, no copy of the array is made in the memory area of the function. Instead, the function receives the address of the array and manipulates it indirectly. How does the function receive the address when we did not use & operator and the function did not declare a pointer variable? The truth is, array variables are in fact pointer variables , but which are declared differently. 21
EXAMPLE 7 /* Doubles each element of an array */ #include <stdio.h> #define SIZE 8 /* maximum number of items in list of data */ void doubler(double *x); void print_array(double a[], int size); int main(void) { double x[SIZE]; int i; /* Gets the data */ printf("Enter %d integer numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i] ); printf("Before doubling: "); print_array( x , SIZE); for (i = 0; i < SIZE; ++i) doubler( &x[i] ); printf("After doubling: "); print_array( x , SIZE); 22 system("pause"); return 0; } void doubler(double *x) { *x = *x * 2; } void print_array(double a[], int size) { int i; for (i = 0; i < size; ++i) printf("%.2f\t", a[i]); printf("\n"); }
EXAMPLE 8 /* Doubles each element of an array */ #include <stdio.h> #define SIZE 8 /* maximum number of items in list of data */ void double_array(double a[], int size); void print_array(double a[], int size); int main(void) { double x[SIZE]; int i; printf("Enter %d integer numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i] ); printf("Before doubling: "); print_array( x , SIZE); double_array( x , SIZE);

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

Want to read all 46 pages?

• Summer '14
• Darwish
• Array, scanf, Array Subscripts