printfThe number of even elements is dn evens printfThe number of odd elements

Printfthe number of even elements is dn evens

This preview shows page 7 - 13 out of 21 pages.

printf("The number of even elements is: %d\n", evens); printf("The number of odd elements is: %d\n", odds); system("pause"); return 0; } int is_even(int n) {     return n%2 == 0; }
Image of page 7
8 Example 3 /* Doubles each element of an array */ #include <stdio.h> #define SIZE 8 /* maximum number of items in list of data*/ void doubler(double *x); 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: "); for (i = 0; i < SIZE; ++i) printf("%.2f\t", x[i]); for (i = 0; i < SIZE; ++i) doubler(&x[i]); printf("\After 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; }
Image of page 8
9 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.
Image of page 9
10 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)
Image of page 10
11 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 receives 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.
Image of page 11
12 Example 4 /* 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);
Image of page 12
Image of page 13

You've reached the end of your free preview.

Want to read all 21 pages?

  • Spring '10
  • baleh
  • Array, size

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture