Exercise 3 page 329

Solution index                   Textual C program


#include <stdio.h>

int is_even(int);

void sum (double a[],
          int n,
          double *even_index_sum_ptr,
          double *odd_index_sum_ptr){
  int i;

  for(i = 0; i < n; i++){
    if (is_even(i))  
      *even_index_sum_ptr += a[i];
    else *odd_index_sum_ptr += a[i];
  }
}

int main(void) {
  double even_sum = 0.0, odd_sum = 0.0;

  double a[5] = {0.5, 7.7, 8.3, 3.2, 4.0}; 

  sum(a, 5, &even_sum, &odd_sum);

  printf("Even sum: %f. Odd sum: %f.\n\n", even_sum, odd_sum);
  
  return 0;
}

int is_even(int i){
  return i%2 == 0;
}

int is_even(int);
We provide a prototype of the function is_even, which tests if an integer is even.
 

void sum (double a[],
          int n,
          double *even_index_sum_ptr,
          double *odd_index_sum_ptr){
  int i;

  for(i = 0; i < n; i++){
    if (is_even(i))  
      *even_index_sum_ptr += a[i];
    else *odd_index_sum_ptr += a[i];
  }
}
The function sum has four parameters. The first is the array a, and it is in reality a constant pointer to the first element in the array. The next is the number of elements to sum in the array. The two last parameters are pointers two doubles, and they play the role of call by reference parameters. Via these parameters we may change the values of the actual parameters passed to the function sum.
 

  for(i = 0; i < n; i++){
    if (is_even(i))  
      *even_index_sum_ptr += a[i];
    else *odd_index_sum_ptr += a[i];
  }
We now focus on the for loop of the sum function. We notice that i starts with 0, which always is the lowest index in a C array. It runs to n - 1. If i is even we accumulate the a[i] value in the variable pointed to by even_index_sum_ptr. Notice the dereferencing *even_index_sum_ptr of the pointer variable even_index_sum_ptr, and similary for odd_index_sum_ptr. Also notice that *even_index_sum_ptr += a[i] actually means *even_index_sum_ptr = *even_index_sum_ptr + a[i]. The * dereferencing operator is applied before both the addition and the assignment. You find the dereferencing operator at level 15 in the C operator table.
 

int main(void) {
  double even_sum = 0.0, odd_sum = 0.0;

  double a[5] = {0.5, 7.7, 8.3, 3.2, 4.0}; 

  sum(a, 5, &even_sum, &odd_sum);

  printf("Even sum: %f. Odd sum: %f.\n\n", even_sum, odd_sum);
  
  return 0;
}
In the main function we have the two variables even_sum and odd_sum, the addreses of which are passed as actual parameters to sum. Next we see the array a with doubles, which holds 5 elements. In the call sum(a, 5, &even_sum, &odd_sum) the addresses of the variables even_sum and odd_sum are given to the sum function, which is able to assign to these variables. Again, this is call by reference parameter passing. Finally, the values of even_sum and odd_sum are printed out.
 

int is_even(int i){
  return i%2 == 0;
}
As the last thing in the program, we define the function is_even. We have earlier given a prototype of this function, to make it known to the compiler. The function returns 'true' if i modulo 2 is 0, i.e., if i is even. In general we find it valuable and worthwhile to define very simple functions like is_even, because it makes other parts of the program easier to understand.
 


Generated: Wednesday, March 29, 2006, 12:33:31
This program dissection page is generated from an XML-in-LAML source file