systempause return 0 void doublerdouble x x x 2 void printarraydouble a int

Systempause return 0 void doublerdouble x x x 2 void

This preview shows page 12 - 17 out of 21 pages.

     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");  }
Image of page 12
13 Example 5 /* 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); printf("After doubling: "); print_array(x, SIZE); system("pause"); return 0; } void double_array(double a[], int size) {      int i;            for (i = 0;  i < size;  ++i)             a[i] *= 2; }   void print_array(double a[], int size)  {        int i;                for (i = 0;  i < size;  ++i)               printf("%.2f\t", a[i]);        printf("\n");  }  
Image of page 13
14 Example 6 /* Finds the average of elements in an array */ #include <stdio.h> #define SIZE 5 /* maximum number of items in list of data */ double get_average(double a[], int size); int main(void) { double x[SIZE], average; int i; printf("Enter %d integer numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i]); average = get_average(x, SIZE); printf("The average of the elements in the array is %.2f\n", average); system("pause"); return 0; } double get_average(double a[], int size) { int i; double sum = 0; for (i = 0; i < size; ++i) sum += a[i]; return sum/size; }
Image of page 14
15 Example 7 /* Finds the maximum and minimum elements from an array */ #include <stdio.h> #define SIZE 8 /* maximum number of items in list of data */ void get_max_min(double a[], int size, double *max, double *min); int main(void) { double x[SIZE], maximum, minimum; int i; printf("Enter %d integer numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i]); 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; } // uses output parameter to return max & min 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]; } }
Image of page 15
16 Returning an array result In C, the return type of a function cannot be an array. Thus, to return an array as result from a function, the only option is to use output parameter. We recall that output parameters for a function are declared as pointer variables. However, as mentioned earlier, an array variable is a pointer variable. Therefore formal parameters of type array are already output parameters.
Image of page 16
Image of page 17

You've reached the end of your free preview.

Want to read all 21 pages?

  • Spring '10
  • baleh
  • Array, size

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes