"sorting the news"

I

Ivica

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.

#include <stdlib.h>
#include <stdio.h>

#define MAXLENGTH 20
#define elementtype News
#define N 9 //number of news

typedef struct {
int ID;
char name[60];
char text[500]; //structure
int read;
} News;


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

void bubblesort(int *array){
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;
}
}
}
}


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"};

printf("\t\t\tWelcome to the news sorting!\n\n\n\n");

for (n=0;n<15;n++){
printf("%d--> %s\n", new1.ID, new1.name,0);
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);
printf("Enter number + ENTER for reading: ");
scanf("%d",&decision);
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++;}

system("pause");
system("cls"); }


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


//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);}
//-------------------------------------------------

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;
}
 
B

Ben Bacarisse

Ivica said:
We have program which prints news.

See my post in comp.programming. If I'd known you'd post here I'd
have waited since you will get more answers here, I suspect, than in
the now vandalised thread over there.
 
C

CBFalconer

Ben said:
See my post in comp.programming. If I'd known you'd post here
I'd have waited since you will get more answers here, I suspect,
than in the now vandalised thread over there.

If he (or you) are posting in multiple newsgroups, you should be
cross-posting, and certainly not multi-posting. I have
cross-posted this to comp.programming and comp.lang.c so you can
see how it is done.
 
R

Richard Heathfield

CBFalconer said:
If he (or you) are posting in multiple newsgroups, you should be
cross-posting, and certainly not multi-posting.

Ben certainly knows this already, as you ought to know already.
Furthermore, the OP originally posted *only* in comp.programming. There,
it was suggested to him that he post here. Perhaps you would care to
demonstrate to the OP how to travel back in time to widen the group
distribution of an article already posted?
I have
cross-posted this to comp.programming and comp.lang.c so you can
see how it is done.

Well done - but you have failed to demonstrate the other requirement, the
time travel bit.
 
I

Ivica

CBFalconer said:
If he (or you) are posting in multiple newsgroups, you should be
cross-posting, and certainly not multi-posting. I have
cross-posted this to comp.programming and comp.lang.c so you can
see how it is done.

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.

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

Ben Bacarisse

CBFalconer said:
If he (or you) are posting in multiple newsgroups, you should be
cross-posting, and certainly not multi-posting.

I am not multi-posting. I am alerting people to the fact that a nearly
identical post exists so as to *minimise* the effects of the
multi-posting. The OP was ill-advised to "also post this to
comp.lang.c" by someone else.
I have
cross-posted this to comp.programming and comp.lang.c so you can
see how it is done.

Thank you. Do you want me to show you how to shorten your sig? :)
 
R

Richard Bos

Ivica said:
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.

Poor thing. Perhaps you would not have been in problems if you'd started
doing your homework earlier.

If you want helpful suggestions, I have three:
- don't use bubblesort; even insertion sort would be better, but C has
an in-built qsort() function which you should be able to use here;
- learn to use arrays, because your plethora of new1...new9 repeated
lines makes your code harder to read than it should (in fact, I
strongly suspect that this was the point of giving you this homework
in the first place);
- if you have a homework problem, and you want to ask people's advise,
don't leave it to the very last moment, and be civil to the people
whose mercy you rely on.

Richard
 
M

Mark Bluemel

Ivica said:
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.

This looks like homework to me...
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?

Only the sorting looks nasty?
#include <stdlib.h>
#include <stdio.h>

#define MAXLENGTH 20
#define elementtype News
#define N 9 //number of news

typedef struct {
int ID;
char name[60];
char text[500]; //structure
int read;
} News;


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

What is this for?
void bubblesort(int *array){
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;
}
}
}
}

There are other, better sorts, but I'd look at the rest of your program
before worrying about which sort to use.
int main()
{
int n; // how many times we will allow analysis before
reading
int decision;

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

Why not an array of them?
printf("\t\t\tWelcome to the news sorting!\n\n\n\n");

for (n=0;n<15;n++){
printf("%d--> %s\n", new1.ID, new1.name,0); ....
printf("%d--> %s\n\n", new9.ID, new9.name,0);

Again, an array would be neater, surely.
printf("Enter number + ENTER for reading: ");
scanf("%d",&decision);

scanf is a poor choice for interactive input. Read the FAQ at
http://www.c-faq.com, particularly section 12.
if (decision==1){
printf("---------------------------------------\n%s\n",
new1.text);
new1.read++;} ....
if (decision==9){
printf("---------------------------------------\n%s\n",
new9.text);
new9.read++;}

Can I say "use an array" again? If you won't, then a switch structure
might be neater than all these "if"s.
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

If you sorted an array of structures, each containing an offset for a
news item and a count, it could be neater. (I'd probably end up coding
something involving pointers, but I'm avoiding them for this
discussion).

[Rest of code snipped]
 
I

Ivica

Richard Bos said:
Poor thing. Perhaps you would not have been in problems if you'd started
doing your homework earlier.

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

pete

.... or even if *I* had started doing your homework earlier.

But yesterday, you said that it was due today.
So, realising that I didn't have enough time, I left it alone.
If I had started yesterday,
I would have had enough time to make something nice by tomorrow.

And so, the reason that I didn't do it for you

IS ENTIRELY YOUR FAULT!!!
Who are you to judge here?

He's one of the public.
You invited public criticism when you posted in public.
If you don't have some kind of constructive answer,
then please skip this thread.

Here's what you do:
1 Fail this assignment
2 Post the next assignment in time to get help
It's been scheduled for the next week
but it's moved to tomorrow for some reasons.

If your class is graded on a curve,
and you eventually get one assignment done right,
then you won't have any problems.
 
M

Mark Bluemel

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

It might just be because English isn't your first language, but I don't
think you've understood the last of Richard's points :-
>> - if you [snip] want to ask people's advise,
>> [snip] be civil to the people whose mercy you rely on.

I've given you some pointers elsewhere in the thread, but based on your
attitude, I'm beginning to regret it and I'm unlikely to do so in
future. It may not be important to you to lose what help I could
offer, but it's not wise to alienate a large pool of potentially
helpful expertise.
 
K

Kenny McCormack

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.

Welcome to CLC, where the first rule is: Never, ever, give any actual help.
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.

Welcome to CLC, where the first rule is: Never, ever, give any actual help.
 
K

Kenny McCormack

Poor thing. Perhaps you would not have been in problems if you'd started
doing your homework earlier.

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

Kenny McCormack

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.

You are being jerked around. Learn to lay back and enjoy it.
 
J

Johannes Bauer

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

Who are you to ask for answers in such an arrogant way - *you* are the
one who needs help, I guess pretty much everyone else in the thread
knows how to solve your problems. They *choose* to not do this, as it
would miss the point.
It's been scheduled for the next week but it's moved to tomorrow for some
reasons.

You even admit that it's homework! Yeah right, rescheduled. They're all
so mean to you. Go try to convince some people who are *way* stupider
with your lies - nobody else will believe you.

Have fun with your homework,
Regards,
Johannes
 
K

Kenny McCormack

CBFalconer said:


Ben certainly knows this already, as you ought to know already.
Furthermore, the OP originally posted *only* in comp.programming. There,
it was suggested to him that he post here. Perhaps you would care to
demonstrate to the OP how to travel back in time to widen the group
distribution of an article already posted?


Well done - but you have failed to demonstrate the other requirement, the
time travel bit.

I was going to post my usual commentary to the effect of "CLC regulars
eating their own", but then it occurred to me that, even more to the
point, CBF is becoming the Larry Craig of the clique. I.e., the one
that everyone else is picking on, because he has become even too far out
for them.
 
J

Johannes Bauer

Kenny said:
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.

My car's tyres need to get a change. I could read a book on how to do
it, but you probably already know - it would save me a lot of time if
you would just drop by and do it yourself for me. Free of charge, of course.

Oh, and it's urgent. Please stop by in the next 24 hours.

Oh, and bring new tyres yourself. I was too lazy to buy some.

Thanks!
You're the best, dude!
Johannes
 
A

Antoninus Twink

I was going to post my usual commentary to the effect of "CLC regulars
eating their own", but then it occurred to me that, even more to the
point, CBF is becoming the Larry Craig of the clique. I.e., the one
that everyone else is picking on, because he has become even too far
out for them.

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?
 
C

CBFalconer

Antoninus said:
Kenny McCormack wrote: .... snip ...

I think you're right. In their defense, CBF is just an

I apologize. My earlier cross-post seems to have let those
notorious trolls Twink and McCormack into comp.programming. Now I
will need to PLONK them again.
 
R

Richard Bos

Ivica said:
Who are you to judge here?

I am one of the people to whom you came for help. With your attitude, I
don't think I want to give you any more.

Richard
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top