Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > "sorting the news"

Reply
Thread Tools

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




 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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>
Try the download section.



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

 
Reply With Quote
 
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.


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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
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.


 
Reply With Quote
 
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.
 
Reply With Quote
 
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
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
 
Reply With Quote
 
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
> 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]
 
Reply With Quote
 
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
> 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.


 
Reply With Quote
 
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.


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

--
pete
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments