1
ICS103 Programming in C
Lecture 13: Arrays II

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

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];
}

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);
}

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",
&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[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.

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;
}

7
Example 2
/*
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++;
}

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

Want to read all 21 pages?

- Spring '10
- baleh
- Array, size