Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #154169
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: An experimental max_n for int's... |
| Date | 2020-08-29 05:33 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <86wo1h8we4.fsf@linuxsc.com> (permalink) |
| References | <ricv3s$8kk$1@gioia.aioe.org> |
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> writes:
> What do you think of this wrt max_n specialized for ints:
> ____________________________
> #include <stdio.h>
> #include <stddef.h>
> #include <assert.h>
>
>
> int max2(int a, int b)
> {
> if (a > b) return a;
> return b;
> }
>
>
> int max_n_int(int* p, size_t sz)
> {
> assert(sz > 0);
>
> int result = p[0];
>
> for (size_t i = 1; i < sz; ++i)
> {
> result = max2(result, p[i]);
> }
>
> return result;
> }
> [...]
>
> Is there a better way to write this?
I would be inclined to write something more like this:
#include <limits.h>
#include <stddef.h>
static int
maximum( int *v, size_t n, int r ){
return n == 0 ? r : maximum( v+1, n-1, r < *v ? *v : r );
}
int
max_n_int( int *p, size_t n ){
return maximum( p, n, INT_MIN );
}
The INT_MIN is there to handle the degenerate case of arrays
with no elements.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-29 00:11 -0700
Re: An experimental max_n for int's... Bonita Montero <Bonita.Montero@gmail.com> - 2020-08-29 10:59 +0200
Re: An experimental max_n for int's... gazelle@shell.xmission.com (Kenny McCormack) - 2020-08-29 10:18 +0000
Re: An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-30 00:04 -0700
Re: An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-30 00:06 -0700
Re: An experimental max_n for int's... Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-08-29 03:43 -0700
Re: An experimental max_n for int's... Bart <bc@freeuk.com> - 2020-08-29 12:06 +0100
Re: An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-29 18:08 -0700
Re: An experimental max_n for int's... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-29 05:33 -0700
Re: An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-29 18:01 -0700
Re: An experimental max_n for int's... Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-08-30 03:35 -0700
Re: An experimental max_n for int's... Bart <bc@freeuk.com> - 2020-08-30 12:12 +0100
Re: An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-30 20:33 -0700
Re: An experimental max_n for int's... James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-31 07:50 -0400
Re: An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-31 17:46 -0700
Re: An experimental max_n for int's... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-09-01 02:32 +0100
Re: An experimental max_n for int's... Richard Damon <Richard@Damon-Family.org> - 2020-09-01 07:27 -0400
Re: An experimental max_n for int's... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-09-01 13:36 +0100
Re: An experimental max_n for int's... James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-09-01 10:21 -0400
Re: An experimental max_n for int's... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-09-01 15:57 +0100
Re: An experimental max_n for int's... Richard Damon <Richard@Damon-Family.org> - 2020-09-01 21:45 -0400
Re: An experimental max_n for int's... David Brown <david.brown@hesbynett.no> - 2020-09-01 14:39 +0200
Re: An experimental max_n for int's... James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-09-01 10:28 -0400
Re: An experimental max_n for int's... antispam@math.uni.wroc.pl - 2020-08-30 12:37 +0000
Re: An experimental max_n for int's... DFS <nospam@dfs.com> - 2020-08-29 13:43 -0400
Re: An experimental max_n for int's... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2020-08-29 18:00 -0700
csiph-web