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);
       }

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;   // Index of smallest elem = 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);
       }

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;   // Index of smallest elem = 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);
       }

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;   // Index of smallest elem = 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);
       }

DEMO: demo/C/set1/findMin.c

Processing arrays 2:    shift the elements in an array one place to the left

  • Write a C program that shifts the array elements as follows:

        int main(int argc, char *argv[])
        {
            double myList[] = {1, 4, 9, 7, 5, 6};
    	int    N = 6;
    
            // Shift array one position to left
        
            double temp = myList[0];       // Save the first element
            
            for ( int i = 1; i < N; i++ )
                myList[i-1] = myList[i];   // Shift all element
    
            myList[N-1] = temp;            // Add first element to end
        }

Processing arrays 2:    shift the elements in an array one place to the left

  • Write a C program that shifts the array elements as follows:

        int main(int argc, char *argv[])
        {
            double myList[] = {1, 4, 9, 7, 5, 6};
    	int    N = 6;
    
            // Shift array one position to left
      
            double temp = myList[0];       // Save the first element
           
            for ( int i = 1; i < N; i++ )
                myList[i-1] = myList[i];   // Shift all element
    
            myList[N-1] = temp;            // Add first element to end
        }

Processing arrays 2:    shift the elements in an array one place to the left

  • Write a C program that shifts the array elements as follows:

        int main(int argc, char *argv[])
        {
            double myList[] = {1, 4, 9, 7, 5, 6};
    	int    N = 6;
    
            // Shift array one position to left
      
            double temp = myList[0];       // Save the first element
     
            for ( int i = 1; i < N; i++ )
                myList[i-1] = myList[i];   // Shift elems 1, 2, .., N-1 left
          
            myList[N-1] = temp;            // Add first element to end
        }

Processing arrays 2:    shift the elements in an array one place to the left

  • Write a C program that shifts the array elements as follows:

        int main(int argc, char *argv[])
        {
            double myList[] = {1, 4, 9, 7, 5, 6};
    	int    N = 6;
    
            // Shift array one position to left
      
            double temp = myList[0];       // Save the first element
     
            for ( int i = 1; i < N; i++ )
                myList[i-1] = myList[i];   // Shift elems 1, 2, .., N-1 left
          
            myList[N-1] = temp;            // Add first element to end
        }

DEMO: demo/C/set1/shiftArray.c