Programming in C

(Barry) #1

142 Chapter 8 Working with Functions


In the mainroutine, two arrays called array1and array2are defined to contain five
and seven elements, respectively.
Inside the first printfcall, a call is made to the minimumfunction with the arguments
array1and 5 .This second argument specifies the number of elements contained in
array1.The minimumfunction finds the minimum value in the array and the returned
result of –37 is then displayed at the terminal.The second time the minimum function is
called,array2is passed, together with the number of elements in that array.The result of
1 as returned by the function is then passed to the printffunction to be displayed.

Assignment Operators


Study Program 8.11 and try to guess the output beforelooking at the actual program
results.

Program 8.11 Changing Array Elements in Functions
#include <stdio.h>

void multiplyBy2 (float array[], int n)
{
int i;

for ( i = 0; i < n; ++i )
array[i] *= 2;
}

int main (void)
{

float floatVals[4] = { 1.2f, -3.7f, 6.2f, 8.55f };
int i;
void multiplyBy2 (float array[], int n);

multiplyBy2 (floatVals, 4);

for ( i = 0; i < 4; ++i )
printf ("%.2f ", floatVals[i]);

printf ("\n");

return 0;
}

Program 8.11 Output
2.40 -7.40 12.40 17.10
Free download pdf