Processing arrays 1:    find the largest value and its index in an array

  • Write a C program that finds the smallest value and its index in the array of all elements stored in the array myList:

       int main(int argc, char *argv[])
       {
           double myList[] = { 1, 4, 9,, 7, 5, 6};     // Input array
           int    N = 6;      // Array size
           double min;        // Smallest value in array 
           int    indexOfMin; // Index where the smallest value is found
    
           // Find min value and its index
           min = myList[0];  // Assume first element is min
           indexOfMin = 0; 
    
           // Try to find a smaller value in elements 1, 2, .., N-1
           for ( int i = 1; i < N; i++ )
               if ( myList[i] < min ) // We found a smaller element
               {  
                   min = myList[i];  // Update min value
                   indexOfMin = i;   // Update its index in array
               }
                
           printf("Min = %lf, index = %d\n\n", min, indexOfMin);
       }