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


Groups > comp.lang.c > #112266

Algorithm/code to compare values in an array

From DFS <nospam@dfs.com>
Newsgroups comp.lang.c
Subject Algorithm/code to compare values in an array
Date 2017-06-15 11:16 -0400
Organization A noiseless patient Spider
Message-ID <ohu82o$qph$4@dont-email.me> (permalink)

Show all headers | View raw


Given two int arrays:

q = {100,200};
s = {50,125,150,175,190,210};

I want to find:

1. the min s > the min q
2. the max s < the max q

Those would be 125 and 190

This code works fine, but maybe there's a better or shorter approach 
than 2 loops (one forward and one backward)?

------------------------------------------------
int main(int argc, char *argv[])
{
	int q[] = {100,200};
	int s[] = {50,125,150,175,190,210};

	int qmin = q[0];
	int qmax = q[1];

	int min_s = qmin;
	int max_s = qmax;

	for(int i = 0; i < 5; i++)
	{
	   if (s[i] > qmin) {min_s = s[i]; break;}
	}

	for(int i = 4; i >= 0; i--)
	{
	   if (s[i] < qmax) {max_s = s[i]; break;}
	}
	
	printf("min %d, max %d\n", min_s, max_s);	
	return 0;
}
------------------------------------------------

Back to comp.lang.c | Previous | Next — 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