C examples for Function:Function Definition
Write a function that returns the index of the largest value stored in an array of double
#include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10//from ww w .j a va 2s . c o m int index_of_max(double *arr, int arr_size); int main(void) { double test[SIZE]; srand(time(NULL)); // seed random number generator for (int i = 0; i < SIZE; i++) test[i] = rand() / (double) RAND_MAX; printf("%5s ", "Index"); for (int i = 0; i < SIZE; i++) printf("| %6d ", i); printf("\n"); printf("%5s ", "Value"); for (int i = 0; i < SIZE; i++) printf("| %6.4f ", test[i]); printf("\n\n"); // print results printf("The maximum value occurs at index %d\n", index_of_max(test, SIZE)); return 0; } int index_of_max(double *arr, int arr_size) { // return index of max value in array of doubles int index_of_max = 0; for (int i = 1; i < arr_size; i++) if (*(arr + i) > *(arr + index_of_max)) index_of_max = i; return index_of_max; }