13 - ICS103 Programming in C Lecture 13: Arrays II 1...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 13: Arrays II
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Review on Arrays Using array elements as function arguments Examples Using arrays as function arguments Array as formal parameter to functions Array as actual argument in function call Examples Returning an array result Examples Partially filled Arrays Example
Background image of page 2
3 Review: Recall the following example /* 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]; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 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); }
Background image of page 4
5 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", ); 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[8]; a[0] = 4.5; Then we can make a function call: double root = sqrt(a[0]); This rule applies to all cases including user defined functions.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Example 1 /* 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; }
Background image of page 6
7 Example 2 /* uses a function to count even and odd numbers from an integer array */
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/09/2010 for the course ICS 103 taught by Professor Baleh during the Spring '10 term at Abilene Christian University.

Page1 / 21

13 - ICS103 Programming in C Lecture 13: Arrays II 1...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online