fori0iSIZE i output the arrays printf5d 5d 5dn firsti secondi diffi systempause

# Fori0isize i output the arrays printf5d 5d 5dn firsti

This preview shows page 12 - 19 out of 46 pages.

for(i=0;i<SIZE; i++) // output the arrays printf("%5d %5d %5d\n", first[i], second[i], diff[i] ) ; system("pause"); return 0; } 12 EXAMPLE 3 /* Computes the mean and standard deviation of an array of data and displays the difference between each value and the mean. */ #include <stdio.h> #include <math.h> #define SIZE 8 /* maximum number of items in list of data */ int main(void) { double x[SIZE], mean, st_dev, sum, sum_sqr; int i; /* Gets the data*/ printf("Enter %d numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i] ); /* Computes the sum and the sum of the squares of all data */ sum = 0; sum_sqr = 0; for (i = 0; i < SIZE; ++i) { sum += x[i]; sum_sqr += x[i] * x[i]; } 13 EXAMPLE 3… /* Computes and prints the mean and standard deviation */ mean = sum / SIZE; st_dev = sqrt(sum_sqr / SIZE - mean * mean); printf("The mean is %.2f.\n", mean); printf("The standard deviation is %.2f.\n", st_dev); /* Displays the difference between each item and the mean */ printf("\nTable of differences between data values and mean\n"); printf("Index Item Difference\n"); for (i = 0; i < SIZE; ++i) printf("%3d%4c%9.2f%5c%9.2f\n", i, ' ', x[i], ' ', x[i] - mean); system("pause"); return (0); } 14 USING ARRAY ELEMENTS AS FUNCTION ARGUMENTS From the last example, we note that x[i] is used as actual arguments to both printf and scanf functions. for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i] ); When i is 3 for example, &x[i] in the above stement passes the address of the 4 th array element , to the scanf function. printf("%3d%4c%9.2f%5c%9.2f\n", i, ' ', x[i], ' ', x[i] - mean); Similarly, when i=3, x[i] in the above statement passes the value of the 4 th element to the printf function. Thus, the important point to note is that array elements are treated as scalar variables and can be used wherever scalar variables can be used. For example, if we have a double array, double a; a = 4.5; Then we can make a function call: double root = sqrt(a); This rule applies to all cases including user defined functions. 15 EXAMPLE 4 /* Finds the square roots of all elemets in an array */ #include <stdio.h> #include <math.h> #define SIZE 8 /* maximum number of items in list of data */ int main(void) { double x[SIZE], result[SIZE]; int i; /* Gets the data */ printf("Enter %d numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%lf", &x[i]); /* Computes the square roots */ for (i = 0; i < SIZE; ++i) result[i] = sqrt(x[i]); /* notice the argument pass to sqrt function */ /* print the result */ printf("\tx\tsqrt(x)\n"); for (i = 0; i < SIZE; ++i) printf("%9.2f%4c%9.2f%\n", x[i], ' ', result[i ]); system("pause"); return 0; } 16 EXAMPLE 5 /* uses a function to count even and odd numbers from an integer array */ #include <stdio.h> #define SIZE 8 /* maximum number of items in list of data */ int is_even(int n); int main(void) { int a[SIZE]; int i, evens = 0, odds = 0; /* Gets the data */ printf("Enter %d integer numbers separated by blanks\n> ", SIZE); for (i = 0; i < SIZE; ++i) scanf("%d", &a[i] ) ; /* counts the even and odd numbers */ for (i = 0; i < SIZE; ++i) { if (is_even( a[i] )) /* notice the argument pass to is_even function */ evens++; else odds++; } printf("The number of even elements is: %d\n", evens); printf("The number of odd elements is: %d\n", odds); system("pause"); return 0; } 17 int is_even(int n) { return n%2 == 0; } EXAMPLE 6 /* 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] );  #### You've reached the end of your free preview.

Want to read all 46 pages?

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