Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #112304
| From | DFS <nospam@dfs.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Algorithm/code to compare values in an array |
| Date | 2017-06-15 19:21 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <ohv4gu$cf9$3@dont-email.me> (permalink) |
| References | <ohu82o$qph$4@dont-email.me> <9fk5kc1lta08klikjmpeenhl9u9ouvrpbh@4ax.com> |
On 6/15/2017 2:43 PM, Barry Schwarz wrote:
> On Thu, 15 Jun 2017 11:16:31 -0400, DFS <nospam@dfs.com> wrote:
>
>> 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
>
> Using qmin when no s[i] satisfies the condition is misleading.
> It forces the user to check the "returned" value against qmin to identify
> the "no match" condition.
Since a range of s values may or may not be contained within q, that's
the only way it could work. As far as I can tell.
> Are the arrays guaranteed to be sorted?
Yes.
>> This code works fine, but maybe there's a better or shorter approach
>> than 2 loops (one forward and one backward)?
>
> Returning the wrong answer is a strange definition of "works fine".
125 and 190 is the right answer for that set of values.
>> ------------------------------------------------
>> 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];
>
> Is q always an array of 2?
No. But it's always evenly divisible by 2, and 2 values always form the
upper and lower boundaries.
FYI:
I'm counting the number of words enclosed in single-quotes, enclosed in
dbl-quotes.
q = position of dbl-quotes in the input
s = position of sgl-quotes in the input
>> int min_s = qmin;
>> int max_s = qmax;
>>
>> for(int i = 0; i < 5; i++)
>
> How do you know the size of s?
The size of s is set at compile time to a relatively large value that I
know won't be exceeded.
As s is populated I keep track of the nbr of entries, which is /not/
always evenly divisible by 2.
>> {
>> if (s[i] > qmin) {min_s = s[i]; break;}
>> }
>
> The code produces the wrong answer if s contains
> {50, 51, 52, 57, 59, 210}.
>> for(int i = 4; i >= 0; i--)
>> {
>> if (s[i] < qmax) {max_s = s[i]; break;}
>> }
>
> Same issue when q contains {100, 400}.
>
>> printf("min %d, max %d\n", min_s, max_s);
>> return 0;
>> }
The code always checks the return values:
if (q[i] < min_s < max_s < q[i+1])
before moving on.
I guess I could do that check in the function and return a success or
fail flag. Either way.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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