Re: "Sorting" assignment




"Ivica" <prljavi_bluzer@xxxxxxxxxxx> wrote in message
news:fo514c$li8$1@xxxxxxxxxxxxxxxxx

We have program which prints news. There are unlimited number of news(?).
Every news contains subject and body text. Also, we count how much was
every news read. Everytime a news get read, we add 1 to to the value
"read".
Create functions which will print the top 5 of the news, how much in
average something gets read, and what's the difference between the top
news and the news in the middle.

I am looking at switch case usage and bubblesort?

You've given little practical detail so I'm assuming you just have a list of
the news like this, when there are N news items:

# SUBJECT BODY READ
1 "Subject1" "Body1" 12
2 "Subject2" "Body2" 7
3 "Subject3" "Body3" 23
....
N "SubjectN" "BodyN" 21

It sounds like everything has already been read and you want to sort by the
Read count, with highest count at the top. The first column is just an
index, you don't actually need to store this, but it might be useful to do
so to simplify sorting.

You haven't specified language, but let's assume the worst case, C (you
mentioned C in your other thread).

So you have 4 arrays of N items: Index, Subject (of char*), Body (of char*),
and Read. Index initially contains 1 to N.

Work out the average first, that's easy: the total of Read values divided by
N.

Now to the sort, which I suggest you do by manipulating the numbers in the
Index array rather than swapping everything around. After sorting, the Index
values in my example, looking only at the first 3, will start as (1,2,3) but
end up as (3,1,2) (because article #3 has been read the most).

Then print the top 5 using for example:

for (i=1; i<=5; ++i) printf("# %d: Subject: %s \n", i, Subject[Index[i]]);

I haven't specified a sort method; bubble sort is fine if there are only a
few dozen or few hundred articles. but you might get extra marks for a
cleverer sort.

With bubble sort, when comparing items A and B (A,B are indices into the
arrays), compare using Read[A] and Read[B], and if necessary, swap Index[A]
and Index[B].

Notes: you need to check there are in fact at least 5 articles for printing
the results; also there is at least 1 article for the average; also in C
array bounds start at zero.

--
Bart




.



Relevant Pages

  • Re: description and scene setting
    ... breathless quasi-gossip and "news". ... possess a sort of gravitas the modern version utterly lacks. ... Next book the earl himself thinks in passing that gossip will rise up ...
    (rec.arts.sf.composition)
  • ATTN JMS: Thank You, or The TV News Biz
    ... who was twice president of the network and a news producer from ... It's sort of the same as the "power of one being to ... universe, ... Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org ...
    (rec.arts.sf.tv.babylon5.moderated)
  • Re: Against the tide.
    ... venture capitalists who funded us in the past have pulled the plug, ... next week or two, we'll be going belly up, and for the first time ... I don't do this sort of stress very well, ... The good news is that the offer from the larger company that wants to ...
    (rec.music.makers.guitar.acoustic)
  • Re: its a little embarrassing...
    ... It isn't the US doing that, it's trash journalists. ... It's a useful litmus test to sort the journalists not worth paying ... distraction from news). ... the Beckhams do, particularly her - without lenses and bright lights I ...
    (uk.people.support.depression)
  • Still not crazy after all these ears.
    ... Since I had a stroke in 2004 I've had all sort of problems with my ... Now the not so good news. ... left ear and the population norm to start then has a very steep rise to ... I've never been able to get my head round db's and loudness, ...
    (rec.audio.opinion)