Velocity Reviews > "sorting the news"

# "sorting the news"

Ivica
Guest
Posts: n/a

 02-05-2008
We have program which prints news.
Every news contains subject and body text. Also, we count how much was every
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.

#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
} 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
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);
if (decision==2){
printf("---------------------------------------\n%s\n",
new2.text);
if (decision==3){
printf("---------------------------------------\n%s\n",
new3.text);
if (decision==4){
printf("---------------------------------------\n%s\n",
new4.text);
if (decision==5){
printf("---------------------------------------\n%s\n",
new5.text);
if (decision==6){
printf("---------------------------------------\n%s\n",
new6.text);
if (decision==7){
printf("---------------------------------------\n%s\n",
new7.text);
if (decision=={
printf("---------------------------------------\n%s\n",
new8.text);
if (decision==9){
printf("---------------------------------------\n%s\n",
new9.text);

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

bubblesort(array); // call bubblesort

//-------------------------------------------------

printf("\n");

printf("2. Most read news is:\t%s", new1.name);}
printf("2. Most read news is:\t%s", new2.name);}
printf("2. Most read news is:\t%s", new3.name);}
printf("2. Most read news is:\t%s", new4.name);}
printf("2. Most read news is:\t%s", new5.name);}
printf("2. Most read news is:\t%s", new6.name);}
printf("2. Most read news is:\t%s", new7.name);}
printf("2. Most read news is:\t%s", new8.name);}
printf("2. Most read news is:\t%s", new9.name);}
//-------------------------------------------------

printf("\n");

printf("3. Most read news is:\t%s", new1.name);}
printf("3. Most read news is:\t%s", new2.name);}
printf("3. Most read news is:\t%s", new3.name);}
printf("3. Most read news is:\t%s", new4.name);}
printf("3. Most read news is:\t%s", new5.name);}
printf("3. Most read news is:\t%s", new6.name);}
printf("3. Most read news is:\t%s", new7.name);}
printf("3. Most read news is:\t%s", new8.name);}
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;
}

Ben Bacarisse
Guest
Posts: n/a

 02-06-2008
"Ivica" <(E-Mail Removed)-com.hr> writes:

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

--
Ben.

CBFalconer
Guest
Posts: n/a

 02-06-2008
Ben Bacarisse wrote:
> "Ivica" <(E-Mail Removed)-com.hr> writes:
>
>> 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.

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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Richard Heathfield
Guest
Posts: n/a

 02-06-2008
CBFalconer said:

> Ben Bacarisse wrote:
>> "Ivica" <(E-Mail Removed)-com.hr> writes:
>>
>>> 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.

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

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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Ivica
Guest
Posts: n/a

 02-06-2008
"CBFalconer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

Ben Bacarisse
Guest
Posts: n/a

 02-06-2008
CBFalconer <(E-Mail Removed)> writes:

> Ben Bacarisse wrote:
>> "Ivica" <(E-Mail Removed)-com.hr> writes:
>>
>>> 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.

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

--
Ben.

Richard Bos
Guest
Posts: n/a

 02-06-2008
"Ivica" <(E-Mail Removed)-com.hr> wrote:

> "CBFalconer" <(E-Mail Removed)> wrote in message
> > 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.

Poor thing. Perhaps you would not have been in problems if you'd started

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

Mark Bluemel
Guest
Posts: n/a

 02-06-2008
Ivica wrote:
> We have program which prints news.
> Every news contains subject and body text. Also, we count how much was every

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

....
> if (decision==9){
> printf("---------------------------------------\n%s\n",
> new9.text);

Can I say "use an array" again? If you won't, then a switch structure
might be neater than all these "if"s.

>
>
>
> 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]

Ivica
Guest
Posts: n/a

 02-06-2008
"Richard Bos" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)4all.nl...
> Poor thing. Perhaps you would not have been in problems if you'd started

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

It's been scheduled for the next week but it's moved to tomorrow for some
reasons.

pete
Guest
Posts: n/a

 02-06-2008
Ivica wrote:
>
> "Richard Bos" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)4all.nl...
> > Poor thing.
> > Perhaps you would not have been in problems if you'd 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.
I would have had enough time to make something nice by tomorrow.

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

> 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,

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.

and you eventually get one assignment done right,
then you won't have any problems.

--
pete