Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #112295

Re: Algorithm/code to compare values in an array

From Real Troll <Real.Troll@Trolls.com>
Newsgroups comp.lang.c
Subject Re: Algorithm/code to compare values in an array
Date 2017-06-15 16:35 -0400
Organization Aioe.org NNTP Server
Message-ID <ohuqoh$3kp$1@gioia.aioe.org> (permalink)
References <ohu82o$qph$4@dont-email.me>

Show all headers | View raw


On 15/06/2017 16:16, DFS wrote:
>
>
> This code works fine, but maybe there's a better or shorter approach 
> than 2 loops (one forward and one backward)?
>
>

Of course there is a better way that works all the time irrespective 
whether the array is sorted or not.  Use this code:


#include <stdio.h>

int minimum (int values [], int numberOfElements);
int maximum (int values[], int numberOfElements);

int main ()
{
     int array1 [] = {100,200};
     int array2 [] = {50,125,150,175,190,210};

     printf ("Array1 minimum is: %i\n", minimum (array1, 2) );
     printf ("Array1 maximum is: %i\n", maximum (array1, 2) );

     printf ("Array2 minimum is: %i\n", minimum (array2, 6) );
     printf ("Array2 maximum is: %i\n", maximum (array2, 6) );

     return 0;
}
int minimum (int values[], int numberOfElements)
{
     int minValue, i;
     minValue = values[0];

     for ( i = 1; i < numberOfElements; ++i )
         if ( values[i] < minValue )
             minValue = values[i];
     return minValue;
}

int maximum (int values[], int numberOfElements)
{
     int maxValue, i;
     maxValue = values[0];

     for ( i = 1; i < numberOfElements; ++i )
         if ( values[i] > maxValue )
             maxValue = values[i];
     return maxValue;
}

You could combine both Min and Max in one function but you may not use 
both in the same prog so the best thing is to keep separate.

Me thinks so anyway but like everything, somebody here will find holes 
in the code.

Also, the size of the array is hard coded as dynamic arrays is not 
supported in C like it is in C++.



>
>

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Algorithm/code to compare values in an array DFS <nospam@dfs.com> - 2017-06-15 11:16 -0400
  Re: Algorithm/code to compare values in an array Richard Heathfield <rjh@cpax.org.uk> - 2017-06-15 16:41 +0100
    Re: Algorithm/code to compare values in an array DFS <nospam@dfs.com> - 2017-06-15 13:48 -0400
      Re: Algorithm/code to compare values in an array Richard Heathfield <rjh@cpax.org.uk> - 2017-06-15 19:47 +0100
        Re: Algorithm/code to compare values in an array Gareth Owen <gwowen@gmail.com> - 2017-06-15 20:17 +0100
          Re: Algorithm/code to compare values in an array Richard Heathfield <rjh@cpax.org.uk> - 2017-06-15 20:51 +0100
  Re: Algorithm/code to compare values in an array Barry Schwarz <schwarzb@dqel.com> - 2017-06-15 11:43 -0700
    Re: Algorithm/code to compare values in an array DFS <nospam@dfs.com> - 2017-06-15 19:21 -0400
      Re: Algorithm/code to compare values in an array Barry Schwarz <schwarzb@dqel.com> - 2017-06-15 18:49 -0700
  Re: Algorithm/code to compare values in an array Real Troll <Real.Troll@Trolls.com> - 2017-06-15 16:35 -0400
    Re: Algorithm/code to compare values in an array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-15 21:34 +0100
      Re: Algorithm/code to compare values in an array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-15 23:23 +0100
        Re: Algorithm/code to compare values in an array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-16 00:35 +0100
          Re: Algorithm/code to compare values in an array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-16 01:04 +0100
          Re: Algorithm/code to compare values in an array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-06-16 01:10 +0100
        Re: Algorithm/code to compare values in an array DFS <nospam@dfs.com> - 2017-06-15 20:45 -0400
    Re: Algorithm/code to compare values in an array DFS <nospam@dfs.com> - 2017-06-15 20:00 -0400

csiph-web