Docsity

Experimenting with Array Variables in Functions - Lab 16 - Fall 2007 | CPS 196, Lab Reports of Computer Science

Toggle sidebar
bg1
CPS 196 Lab 16 Fall 2007
Name: _________________________________________
I. Experimenting with array variables in functions:
A. Start a new item in your project and copy this program from here or from the web page:
/* arrayscope.c */
/* demonstrates a function that assigns to an array parameter */
#include <stdio.h>
/* function prototypes */
void clearArray (int values[], int numberOfElements);
void notClearValue (int value);
int main ( )
{
/* declare and define an array of length 12 */
int examples[5] = { 5, 7, -3, 15, 8 };
int mint = 999;
int i;
/* call to the clear array function */
clearArray ( examples, 5);
/* print the elements of the array to observe the change */
for ( i = 0; i < 5; i++)
{
printf("Element %i of the array: %i\n", i, examples[i]);
}
/* call the function that tries to clear the integer variable */
notClearValue (mint);
/* print the value of the variable after the call */
printf("\nThe value of the variable is: %i\n", mint);
return(0);
}
/* Set all the elements of the array to zero */
void clearArray (int values[], int numberOfElements)
{
int i;
for ( i = 0; i < numberOfElements; i++)
{
values[i] = 0;
}
return;
}
/* Set the value of the parameter to zero */
void notClearValue (int value)
{
value = 0;
return;
}
pf3
pf4
pf5

Partial preview of the text

Download Experimenting with Array Variables in Functions - Lab 16 - Fall 2007 | CPS 196 and more Lab Reports Computer Science in PDF only on Docsity!

CPS 196 Lab 16 Fall 2007

Name: _________________________________________

I. Experimenting with array variables in functions:

A. Start a new item in your project and copy this program from here or from the web page:

/* arrayscope.c / / demonstrates a function that assigns to an array parameter */ #include <stdio.h>

/* function prototypes */ void clearArray (int values[], int numberOfElements); void notClearValue (int value);

int main ( ) { /* declare and define an array of length 12 */ int examples[5] = { 5, 7, -3, 15, 8 }; int mint = 999; int i;

/* call to the clear array function */ clearArray ( examples, 5);

/* print the elements of the array to observe the change */ for ( i = 0; i < 5; i++) { printf("Element %i of the array: %i\n", i, examples[i]); }

/* call the function that tries to clear the integer variable */ notClearValue (mint);

/* print the value of the variable after the call */ printf("\nThe value of the variable is: %i\n", mint);

return(0); }

/* Set all the elements of the array to zero / void clearArray (int values[], int numberOfElements) { int i; for ( i = 0; i < numberOfElements; i++) { values[i] = 0; } return; } / Set the value of the parameter to zero */ void notClearValue (int value) { value = 0; return; }

B. Run this program and observe the result. Now use debug to step through the main of the

program and observe the values of the array and of the variable mint. Write down their values

before and after the function calls:

Variables Value after

declarations

Value after

clearArray

Value after

notClearValue

examples[0]

examples[1]

examples[2]

examples[3]

examples[4]

mint

Conclusion:

If an array variable is declared in main and assigned in a function, what happens to the values of the

array in main?

If any other variable is declared in main and assigned in a function, what happens to the value of the

variable in main?

B. Write an additional function for this program and call it from the main.

The function is to compute the standard deviation of an array, which is another common

statistical measure for a set of values, which the statisticians would call a distribution. It

provides an indication of how much the individual values in the distribution differ from the

mean.

1. Write the function prototype and add it to the program at the top. Pick a name (like

stdev). The function should take as parameters a double array of any size and an integer,

which is the size of the array, and return a double. Note that except for the name, this

function has the same prototype as meanDouble.

2. Write the function to compute the standard deviation. Suppose that there is an array

values with n elements, ranging from 0 to n-1. Here is a one-line version of the formula for

the standard deviation:

In the body of the function, you should have these steps to compute the standard

deviation of the array parameter:

a) Calculate the mean of the distribution, by calling meanDouble, and assign it to a

variable.

b) Initialize a variable to zero to add up the sum of the squares of the terms inside

the sum.

c) Loop over the array. For each element compute the term inside the sum; this will

be the difference between the mean and the element values[i] and square the

difference. Add this term to the running sum.

d) After the loop, take the sum of the terms and divide it by the number of elements

of the array (called n in the formula).

e) Now compute the square root of the result of previous step. Use the sqrt function,

and don’t forget to put #include <math.h> at the top of your program.

The standard deviation S of a set of numbers Xi,

where mean(X) is the mean of the numbers:

S = ( ΣΣΣΣ (mean(X) – Xi) 2 ) / n

i = 0

n-

  1. Put a call to the standard deviation function in the main of the program and give the

examples array as an argument. Print out the result of the standard deviation and write the

result here.

Standard deviation =

After you have completed the program, print it out and hand it in.