C Programming

Functions and Arrays

C allows the user to build up a library of modules such as the maximum value found in the previous example.

However, in its present form this module or function is limited as it only accepts ten elements. It is thus desirable to modify the function so that it also accepts the number of elements as an argument also. A modified version follows,


	/* example program to demonstrate the passing of an array */
	#include <stdio.h>

	int findmaximum( int [], int );             /* ANSI function prototype */

	int  findmaximum( int numbers[], int elements )
	{
		int  largest_value, i;
		
		largest_value = numbers[0];
		
		for( i = 0; i < elements; ++i )
			if( numbers[i] > largest_value )
				largest_value = numbers[i];
		
		return largest_value;
	}

	main()
	{
		static int numb1[] = { 5, 34, 56, -12, 3, 19 };
		static int numb2[] = { 1, -2, 34, 207, 93, -12 };
		
		printf("maximum of numb1[] is %d\n", findmaximum(numb1, 6));
		printf("maximum is numb2[] is %d\n", findmaximum(numb2, 6));
	}


	Sample Program Output
	maximum of numb1[] is 56
	maximum of numb2[] is 207


©Copyright B Brown. 1984-1999. All rights reserved.