"sorting the news"

R

Randy Howard

Instead of unnecessary phylosopying rather try to help me since I have less
than 24 hours to fix bad coding in this or I'll be in problems.

Although most regulars seem to agree that CBF has become increasingly
caustic (often confusingly so) over the last few years, that's not the
core issue here.

You seem to be implying above that when you are late on assignment, you
raise a SIGEMERGENCY and everyone should come running to your aid.
The problem with that is, everyone has been late on projects. Running
to Usenet or google for the answer, instead of spending time in front
of the compiler/debugger is likely not the right model for solving such
issues.

You'll find that when people detect you're working on homework or on a
tight deadline, they actually intentionally slow down their responses.
Why? Because you are supposed to /learn/ these things, not have a
canned solution handed to you, which you likely do not even understand
at that point in your learning. This is difficult to accept and absorb
at that stage, but you may discover later that people that hand you the
answer are *not* helping you. They actually hinder your learning.
The thread was on comp.programming but I was pointed here because the thread
there is vandalized and I was told that I should get better help here.
Sadly, even there I got more help.

True enough. However, in the time since you posted the original
request for assistance, you could have completed the assignment.

FWIW, almost everyone reading your pleas did similar or identical
homework on their own at some point, without google, without Usenet and
without getting the answer handed to them. It's a very large part of
why they /could/ help you, namely, because they actually learned it.
 
R

Randy Howard

Here's what you do:
1 Fail this assignment
2 Post the next assignment in time to get help

Where's option #3? Namely, "start the assignment on time and complete
it for yourself by learning the material in time."
 
R

Randy Howard

I think you're right. In their defense, CBF is just an embarrassment
with his idiotic signature and his idiotic, badly conceived and badly
broken ggets routine - who'd even want him in their clique?

Could you explain what you think is "badly broken" about ggets()? It
would be refreshing to hear you talk about technical issues for a
change.
 
C

CBFalconer

Randy said:
Ivica wrote

Although most regulars seem to agree that CBF has become
increasingly caustic (often confusingly so) over the last few
years, that's not the core issue here.

Why do you consider advising a newbie that cross-posting is better
than multi-posting is being "caustic and confusing"?
 
D

Default User

My car's tyres need to get a change.

Kenny is a well-know troll in this group. Everything he posts is
designed to harass and annoy the regulars. You're best off ignoring him.




Brian
 
J

Johannes Bauer

Default said:
Kenny is a well-know troll in this group.

I noticed.
Everything he posts is designed to harass and annoy the regulars.

I noticed.
You're best off ignoring him.

I also noticed - his drivel got me aggravated, anyways. Nevertheless you
are right and I will not reply to him anymore. Just couldn't deny that
myself this one time.

Regards,
Johannes
 
K

Kenny McCormack

Kenny is a well-know troll in this group. Everything he posts is
designed to harass and annoy the regulars. You're best off ignoring him.

Unless, of course, you enjoy this sort of thing.
 
R

Richard Heathfield

Default User said:
Kenny is a well-know troll in this group. Everything he posts is
designed to harass and annoy the regulars.

He *is* a regular, in the sense that he often posts here. He contributes
nothing useful, however.
 
A

Antoninus Twink

I noticed.


I noticed.


I also noticed - his drivel got me aggravated, anyways. Nevertheless you
are right and I will not reply to him anymore. Just couldn't deny that
myself this one time.

Yes, you ignore Kenny, you poor sensitive soul! Stop up your ears, cover
your eyes to the obvious reality of what's going on in this group, and
you'll find that you've become a "regular" in no time.
 
R

Richard

Ha. What sympathy. What grace.

To the OP: Keep in mind that saying you are in a hurry is like catnip to
these guys. It just makes it all the more fun for them to jerk you
around.

Especially CBFalconer, who is, from what I can gather, some sort of
unemployed Call Center Operative. I don't believe I have ever seen him
once answer a question politely or correctly. I believe the rest of the
clique might have decked him out in big slippers and a chequered felt
costume with bells attached.
 
R

Richard

Randy Howard said:
Although most regulars seem to agree that CBF has become increasingly
caustic (often confusingly so) over the last few years, that's not the
core issue here.

You seem to be implying above that when you are late on assignment, you
raise a SIGEMERGENCY and everyone should come running to your aid.

No, he is asking those you CAN and WANT TO to come to his aid. Not a
bunch of moralising arses with nothing better to do than revel in other
peoples misfortunes. And like Falconer, you too have added nothing to
the thread in terms of helping the OP. If you can not, or will not, help
then why not just go away?

You're right YOU are under NO obligation to help. If you dont want to
then dont. But dont bore the hell out of everyone and pollute the thread
with your reasonings for not doing so. No one is interested.
 
R

Richard

Randy Howard said:
Where's option #3? Namely, "start the assignment on time and complete
it for yourself by learning the material in time."

You arses sure like yourselves. You have NO idea why the poster was
short of time. Why are you here in a help group?
 
R

Richard

Antoninus Twink said:
I think you're right. In their defense, CBF is just an embarrassment
with his idiotic signature and his idiotic, badly conceived and badly
broken ggets routine - who'd even want him in their clique?

He's not still pimping that awful library is he?
 
J

John Bode

We have program which prints 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.

Here is some nasty code, I've translated it from Croatian. Hope it makes
sense.

However, sorting looks pretty nasty and it's written badly. Any suggestions
in changing?
I am stuck with the deadline, only tomorrow is left for me for having fun
with this code.

Thanks in advance.

This is probably too late to help before your deadline; however, it
will hopefully be of help for your next assignment.
#include <stdlib.h>
#include <stdio.h>

#define MAXLENGTH 20
#define elementtype News

Why are you creating a synonym for the News type? What is the purpose
of using elementtype instead of News?
#define N 9 //number of news

Give constants meaningful names. "N" carries no meaning;
"NEWS_ITEM_COUNT" or "MAX_NEWS_ITEMS" would do a better job of
communicating that this constant stores the number of news items.
When you have to support large and complex programs, you come to
appreciate meaningful names.
typedef struct {
int ID;
char name[60];
char text[500]; //structure
int read;
} News;

typedef struct {
int last;
elementtype elements[MAXLENGTH];

} LIST;

This LIST type doesn't appear to be used anywhere in the code below.
What is its purpose?
void bubblesort(int *array){

You should pass the size of the array to bubblesort as a parameter
instead of relying on the global constant N. What if you had to sort
multiple arrays, each of different lengths? As written, your
bubblesort function is "tightly coupled" to your main program; it
cannot be easily reused. For a toy program like this, it's not that
much of an issue, but it's a bad habit to get into.

int i,j,k;
for (i=0; i<N; i++){
for (j=N-1; j>i; j--){
if (array[j-1]<array[j]){ //Bubble sort
k=array[j];
array[j]=array[j-1];
array[j-1]=k;
}
}
}

}

Note that the standard library already provides a sorting function,
qsort().
int main()
{
int n; // how many times we will allow analysis before
reading
int decision;

News new1 = {1,"heading 1.", "body 1"};

News new2 = {2,"heading 2.", "body 2"};

News new3 = {3,"Heading 3.", "body 3"};

News new4 = {4,"heading 4.", "body 4"};

News new5 = {5,"heading 5.", "body 5"};

News new6 = {6,"heading 6.", "body 6"};

News new7 = {7,"Heading 7.", "body 7"};

News new8 = {8,"Heading 8.", "body 8"};

News new9 = {9,"heading 9.", "body 9"};

Oy. This *screams* for an array:

News newsItems[NEWS_ITEM_COUNT] = {
{1, "heading 1.", "body 1"},
{2, "heading 2.", "body 2"},
...
};
printf("\t\t\tWelcome to the news sorting!\n\n\n\n");

for (n=0;n<15;n++){

Avoid magic numbers (numeric literals with no obvious meaning) in your
code. What is significant about 15? What does it represent?
printf("%d--> %s\n", new1.ID, new1.name,0);

You're passing more parameters to printf than you need to; the "0"
parameter isn't used.
Get rid of it.
printf("%d--> %s\n", new2.ID, new2.name,0);
printf("%d--> %s\n", new3.ID, new3.name,0);
printf("%d--> %s\n", new4.ID, new4.name,0);
printf("%d--> %s\n", new5.ID, new5.name,0);
printf("%d--> %s\n", new6.ID, new6.name,0);
printf("%d--> %s\n", new7.ID, new7.name,0);
printf("%d--> %s\n", new8.ID, new8.name,0);
printf("%d--> %s\n\n", new9.ID, new9.name,0);

Again, an array would make this a lot simpler:

int j;
for (j = 0; j < NEWS_ITEM_COUNT; j++)
printf("%d--> %s\n", newsItems[j].ID, newsItems[j].name);
printf("Enter number + ENTER for reading: ");
scanf("%d",&decision);

scanf() isn't recommended for interactive input, especially for
numeric input. It's better to use fgets() to read input as a string,
and then to convert it using strtol().
if (decision==1){
printf("---------------------------------------\n%s\n",
new1.text);
new1.read++;}
if (decision==2){
printf("---------------------------------------\n%s\n",
new2.text);
new2.read++;}
if (decision==3){
printf("---------------------------------------\n%s\n",
new3.text);
new3.read++;}
if (decision==4){
printf("---------------------------------------\n%s\n",
new4.text);
new4.read++;}
if (decision==5){
printf("---------------------------------------\n%s\n",
new5.text);
new5.read++;}
if (decision==6){
printf("---------------------------------------\n%s\n",
new6.text);
new6.read++;}
if (decision==7){
printf("---------------------------------------\n%s\n",
new7.text);
new7.read++;}
if (decision==8){
printf("---------------------------------------\n%s\n",
new8.text);
new8.read++;}
if (decision==9){
printf("---------------------------------------\n%s\n",
new9.text);
new9.read++;}

Again, this can be simplified greatly by using an array. You can
replace all those if statements with the following:

newsItems[decision].read++;

Remember that arrays in C are 0-origin (i.e., the array elements are
indexed from 0..8, not 1..9).
system("pause");
system("cls"); }

As for finding the 5 most-read articles, you have a number of options
here. One is to sort the newsItems array itself by modifying your
bubblesort function to sort an array of News instead of int. Or you
could use the standard library function qsort(), you just need to
write a comparison function:

int compareTimesRead(const void *item1, const void *item2)
{
News *newsItem1 = item1;
News *newsItem2 = item2;

if (newsItem1->read > newsItem2->read)
return 1;
else if (newsItem1->read < newsItem2->read)
return -1;
else
return 0;
}

and then in your main function, call qsort() as follows:

qsort((void *) newsItems,
NEWS_ITEM_COUNT,
sizeof *newsItems,
compareTimesRead);

You could avoid sorting entirely by simply walking the array once, and
keeping track of the most popular items as you go. The following code
only tracks the single most-popular item, but you should be able to
figure out how to extend it to store the N-most popular items (an
array might be helpful again here):

int mostPopularItemIndex = 0; /* start with the first element */
for (i = 1; i < NEWS_ITEM_COUNT; i++)
{
if (newsItem.read > newsItem[mostPopularIndex].read)
{
mostPopularIndex = 0;
}
}
int array[N]={new1.read, new2.read, new3.read, new4.read, new5.read,
new6.read, new7.read, new8.read, new8.read};

bubblesort(array); // call bubblesort

Again, pass the size of the array as a parameter instead of relying on
the constant N.
//Most read news-------------------------------
if (new1.read==array[0]){
printf("Most read news is:\t%s", new1.name);}
if (new2.read==array[0]){
printf("Most read news is:\t%s", new2.name);}
if (new3.read==array[0]){
printf("Most read news is:\t%s", new3.name);}
if (new4.read==array[0]){
printf("Most read news is:\t%s", new4.name);}
if (new5.read==array[0]){
printf("Most read news is:\t%s", new5.name);}
if (new6.read==array[0]){
printf("Most read news is:\t%s", new5.name);}
if (new7.read==array[0]){
printf("Most read news is:\t%s", new7.name);}
if (new8.read==array[0]){
printf("Most read news is:\t%s", new8.name);}
if (new9.read==array[0]){
printf("Most read news is:\t%s", new9.name);}
//-------------------------------------------------

And again, storing the news items in an array makes all the if
statements above reduce to:

printf("Most read news is:\t%s\n",
newsItems[mostPopularIndex].name);
printf("\n");

//2. Second most read news-------------------------------
if (new1.read==array[1]){
printf("2. Most read news is:\t%s", new1.name);}
if (new2.read==array[1]){
printf("2. Most read news is:\t%s", new2.name);}
if (new3.read==array[1]){
printf("2. Most read news is:\t%s", new3.name);}
if (new4.read==array[1]){
printf("2. Most read news is:\t%s", new4.name);}
if (new5.read==array[1]){
printf("2. Most read news is:\t%s", new5.name);}
if (new6.read==array[1]){
printf("2. Most read news is:\t%s", new6.name);}
if (new7.read==array[1]){
printf("2. Most read news is:\t%s", new7.name);}
if (new8.read==array[1]){
printf("2. Most read news is:\t%s", new8.name);}
if (new9.read==array[1]){
printf("2. Most read news is:\t%s", new9.name);}
//-------------------------------------------------

printf("\n");

//3. most read news-------------------------------
if (new1.read==array[2]){
printf("3. Most read news is:\t%s", new1.name);}
if (new2.read==array[2]){
printf("3. Most read news is:\t%s", new2.name);}
if (new3.read==array[2]){
printf("3. Most read news is:\t%s", new3.name);}
if (new4.read==array[2]){
printf("3. Most read news is:\t%s", new4.name);}
if (new5.read==array[2]){
printf("3. Most read news is:\t%s", new5.name);}
if (new6.read==array[2]){
printf("3. Most read news is:\t%s", new6.name);}
if (new7.read==array[2]){
printf("3. Most read news is:\t%s", new7.name);}
if (new8.read==array[2]){
printf("3. Most read news is:\t%s", new8.name);}
if (new9.read==array[2]){
printf("3. Most read news is:\t%s", new9.name);}
//-------------------------------------------------

printf("\n\nDiffernce between the TOP news and one in the middle is:
%d - %d = %d", array[0], array[4], array[0] - array[4]);

printf("\n\n");
system("pause");
return 0;

}
 
J

John Bode

[snip]
int mostPopularItemIndex = 0; /* start with the first element */
for (i = 1; i < NEWS_ITEM_COUNT; i++)
{
if (newsItem.read > newsItem[mostPopularIndex].read)
{
mostPopularIndex = 0;


Crap. That line *should* read

mostPopularIndex = i;

Sorry about that.
 
J

John Bode

Default User said:



He *is* a regular, in the sense that he often posts here. He contributes
nothing useful, however.

Kenny does occasionally contribute something more useful than
invective, but it is a distressingly small fraction of his total
output. Same for AT and Just Richard.

It could be a *lot* worse. We could have S**** N**** back.
 
M

Malcolm McLean

Ivica said:
Who are you to judge here?
If you don't have some kind of constructive answer, then please skip this
thread.

It's been scheduled for the next week but it's moved to tomorrow for some
reasons.
So you've got a chance.

Look at the qsort() function, and learn how to use it to do what you want.
That's cheating of course.
Your real task is to implement qsort(). But don't call it qsort(). Call it
"bubblesort", or "insertionsort". You can have a whole range of sorts that
operate in slightly different ways but sort the same data.

The function pointer only looks a little bit intimidating. Clearly, if we
can sort anything, we need to be told how to compare two things. All qsort()
needs to know is how many things, how big they are, and how to compare them.
All the function pointer is doing is separating comparison logic from
sorting logic.
 
R

Randy Howard

You arses sure like yourselves. You have NO idea why the poster was
short of time. Why are you here in a help group?

This isn't a help group. It's a Usenet group for discussing C. It's
not rentacoder or DoMyHomework.com.
 
R

Randy Howard

If you can not, or will not, help then why not just go away?

Instead of complaining at those that don't help, why don't you just do
his homework for him instead?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top