Thread: function for largest value

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    89

    function for largest value

    If i want to find largest number in any sequence, I write program like this

    Code:
     #include<stdio.h>
    
    int main()
    {
    	int i = 0;
       int array[5] = {1, 2, 3, 4, 5};
       int largest = array[i];
      
       	for (i = 1; i <= 5; i++)
    	{
    		if ( largest < array[i])
    			largest = array[i];
    	}
    	
    	printf(" largest : %d ", largest);
    	return 0;
    
    
    }
    I want to make a function that gives largest number. I don't know how to do it using function

    Code:
    #include<stdio.h>
    
    void foo( int *ptr)
    {
    	int largest = *ptr;
    	for (i = 1; i <= 5; i++)
    	{
    		
    	}
    	printf(" largest : %d ", largest);
    }
    int main()
    {
       int i = 0;
       int array[5] = {1, 2, 3, 4, 5};
       foo(&array[0]);	
       
    	return 0;
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for (i = 1; i <= 5; i++)
    Well the first step is knowing how to loop over an array.
    The test should be <, not <=

    Compare
    Code:
    #include<stdio.h>
     
    int main()
    {
        int i = 0;
       int array[5] = {1, 2, 3, 4, 5};
       int largest = array[i];
       
        for (i = 1; i < 5; i++)
        {
            if ( largest < array[i])
                largest = array[i];
        }
         
        printf(" largest : %d ", largest);
        return 0;
     
     
    }
    with
    Code:
    #include<stdio.h>
    int largestValue(int array[]) {
        int i = 0;
        int largest = array[i];
       
        for (i = 1; i < 5; i++)
        {
            if ( largest < array[i])
                largest = array[i];
        }
        return largest;     
    }
    
    int main()
    {
        int array[5] = {1, 2, 3, 4, 5};
        int largest = largestValue(array);
        printf(" largest : %d ", largest);
        return 0;
    }
    You don't need to alter any of the code, you can just copy/paste the loop body directly into a function.
    Not a single * to be seen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2022
    Posts
    89
    Thank you very much
    Last edited by Kittu20; 01-15-2023 at 03:22 AM.

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    If you need to do this in a function because you want to use arbitrary sized arrays, the number of elements in the array must be passed to the function:
    Code:
    // return the position with the largest value
    // or -1 if elems == 0.
    unsigned int find_largest_int( int *p, unsigned int elems )
    {
      unsigned int i, j;
    
      if ( ! elems ) return -1;
    
      for ( j = 0, i = 1; i < elems; i++ )
        if ( p[i] > p[j] )
          j = i;
    
      return j;
    }
    Example:
    Code:
      int array[] = { 1, 2, 3, 7, 5, 6 };
      unsigned int largest;
    
      largest = find_largest_int( array, sizeof array / sizeof array[0] );
    
      // don't need to test for 'largest == (unsigned int)-1' because the array has elements...
      printf( "Found @%u: %d\n", largest, array[largest] );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-05-2013, 11:03 PM
  2. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  3. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  4. Arrays: mean, largest value, function count,
    By aryanthegreat in forum C++ Programming
    Replies: 4
    Last Post: 04-19-2006, 12:56 PM
  5. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM

Tags for this Thread