Re: Assitance - Formal Programming
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Wed, 30 Nov 2005 04:44:01 +0000 (UTC)
Michael T. Gacek said:
> I've been reading "The Science of Programming" by David Gries, and doing
> all excercises. In chapter 16, I have run across a snag and can't find an
> answer. Specifically Chapter 16, section 3, 16.3, question 5 about finding
> the number of plateaus in an array. If someone has a solution program
> please email me at lazy@xxxxxxxxx I would greatly appreciate it.
> Thanks you.
We will define a plateau as a contiguous sequence of equal values in an
array.
#include <stddef.h>
size_t CountPlateaus(int *p, size_t n)
{
size_t i;
size_t num = n;
int prev = p[0];
for(i = 1; i < n; i++)
{
if(prev == p[i])
{
--num;
}
else
{
prev = p[i];
}
}
return num;
}
The reason this is so easy is that a plateau might be only one value wide.
So all we have to do is assume all values are different to their
neighbours, and decrement the count whenever we find that this isn't the
case.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.
- References:
- Assitance - Formal Programming
- From: Michael T. Gacek
- Assitance - Formal Programming
- Prev by Date: Re: Intergrated Development Environments
- Next by Date: Re: Hotel and Rest. Management Major and CS Minor?
- Previous by thread: Assitance - Formal Programming
- Index(es):
Relevant Pages
|