Re: "Sorting" assignment
- From: "Bartc" <bc@xxxxxxxxxx>
- Date: Mon, 04 Feb 2008 11:44:27 GMT
"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
.
- Follow-Ups:
- Re: "Sorting" assignment
- From: Bartc
- Re: "Sorting" assignment
- References:
- "Sorting" assignment
- From: Ivica
- "Sorting" assignment
- Prev by Date: Re: The annotated annotated annotated C standard
- Next by Date: Re: "Sorting" assignment
- Previous by thread: Re: "Sorting" assignment
- Next by thread: Re: "Sorting" assignment
- Index(es):
Relevant Pages
|