The next set of examples show various functions that return array as result

# The next set of examples show various functions that

This preview shows page 16 - 21 out of 21 pages.

The next set of examples show various functions that return array as result: Function, read_array , reads data from the user into an array and return it. Function, add_arrays , uses two arrays as input, add the corresponding elements and return the result in another array. Function, reverse_array , uses a single array for both input and output. It reverses the elements inside the array and return the reversed array.
17 Example 8 /* Finds the maximum and minimum from an array . It uses a function that reads and returs an array */ #include <stdio.h> #define SIZE 8 /* maximum number of items in list of data */ void read_array(double a[], int size); void get_max_min(double a[], int size, double *max, double *min); int main(void) { double x[SIZE], maximum, minimum; read_array(x, SIZE); get_max_min(x, SIZE, &maximum, &minimum); printf("The maximum element in the array is %.2f\n", maximum); printf("The minimum element in the array is %.2f\n", minimum); system("pause"); return 0; } void get_max_min(double a[], int size, double *max, double *min) { int i; *max = a[0]; *min = a[0]; for (i = 1; i < size; ++i) { if (a[i] > *max) *max = a[i]; else if (a[i] < *min) *min = a[i]; } } void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); }
18 Example 9 /* Adds two arrays and return the result in another array */ #include <stdio.h> #define SIZE 5 void read_array(double a[], int size); void print_array(double a[], int size); void add_arrays(double a[], double b[], double c[], int size); int main(void) { double first[SIZE], second[SIZE], sum[SIZE]; read_array(first, SIZE); read_array(second, SIZE); add_arrays(first, second, sum, SIZE); printf("First Array: "); print_array(first, SIZE); printf("Second Array: "); print_array(second, SIZE); printf("Sum of Arrays: "); print_array(sum, SIZE); system("pause"); return 0; } void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); } void add_arrays(double a[], double b[], double c[], int size) { int i; for (i=0; i<size; i++) c[i] = a[i] + b[i]; } void print_array(double a[], int size) { int i; for (i = 0; i < size; ++i) printf("%.2f\t", a[i]); printf("\n"); }
19 Example 10 /* Reverses the elements of an array using a function */ #include <stdio.h> #define SIZE 8 void read_array(double a[], int size); void print_array(double a[], int size); void reverse_array(double a[], int size); int main(void) { double x[SIZE]; read_array(x, SIZE); printf("Before reversing: "); print_array(x, SIZE); reverse_array(x, SIZE); printf("After reversing: "); print_array(x, SIZE); system("pause"); return 0; } void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); } void print_array(double a[], int size) { int i; for (i = 0; i < size; ++i) printf("%.2f\t", a[i]); printf("\n"); } void reverse_array(double a[], int size) { int i; double temp[size]; for (i=0; i<size; i++) temp[size-1-i] = a[i]; for (i=0; i<size; i++) a[i] = temp[i]; }
20 Partially filled Arrays The format of array declaration requires that we specify a size at the point of declaration.

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

Want to read all 21 pages?

• Spring '10
• baleh
• Array, size