Re: Assitance - Formal Programming



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)
.



Relevant Pages

  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily ... design, PIC code, and real time programming. ...
    (rec.pets.dogs.behavior)
  • Re: LISPPA
    ... > The abstract mathematical definition doesn't cope too well ... programming languages which do not allow to write bad programs, ... definition of array? ...
    (comp.lang.lisp)
  • Re: How do i change numerics into binary numbers?
    ... my first time trying out on programming. ... how do I extract the output from the user to make it an array? ... Those are needed details. ... what you expect for input, (even if you know that it may be garbage, you have ...
    (microsoft.public.vb.general.discussion)
  • Re: Bug/Gross InEfficiency in HeathFields fgetline program
    ... Maybe this is true in the sort of programming you do, ... you are storing a list of amounts of money as integers. ... int average ... the array, rounded towards zero. ...
    (comp.lang.c)
  • Re: Puppy Mastiff wants to Nip at Faces
    ... in my first college textbook on structured programming. ... they did was loop through an array to show how you could easily access ...   and improve your crack pot licensing fee calculator. ...
    (rec.pets.dogs.behavior)