954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

sorting the "news"

I need to write something like this, just plain and simple c.

1. Program starts and prints 5 news titles
2. You select one and it prints news body
3. after viewing you select close and select other one or exit program
4. after that it prints "sort by views" of the news

The main point is to use some of the sorting algorithms. Any suggestions, ideas, pseudocoding since I am newbie?

prljavibluzer
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

go to some news web site such as www.cnn.com and select 5 articles. Copy/past a paragraph from each article into your program.

In main() make a menu with the 5 titles and number them a) b) c) d) and e). Then ask for title input. After that create a simple switch statement and which prints the paragraph that is associated with the selected title.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
The main point is to use some of the sorting algorithms. Any suggestions, ideas, pseudocoding since I am newbie?


Do a search forBubble Sort. It's the easiest to understand and for the small amount of data you need it should be more than adequate.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

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.

<ul><li>#include <stdlib.h></li>
<li>#include <stdio.h></li>
<li>#define MAXLENGTH 20</li>
<li>#define elementtype News</li>
<li>#define N 9                //number of news</li>
<li>typedef struct {</li>
<li>       int ID;</li>
<li>       char name[60];</li>
<li>       char text[500];      //structure</li>
<li>       int read;</li>
<li>       } News;</li>
<li>typedef struct {</li>
<li>int last;</li>
<li>elementtype elements[MAXLENGTH];</li>
<li>} LIST;</li>
<li>void bubblesort(int *array){</li>
<li>    int i,j,k;</li>
<li>    for (i=0; i<N; i++){</li>
<li>        for (j=N-1; j>i; j--){</li>
<li>            if (array[j-1]<array[j]){     //Bubble sort</li>
<li>              k=array[j];</li>
<li>              array[j]=array[j-1];</li>
<li>              array[j-1]=k;</li>
<li>              }</li>
<li>        }</li>
<li>    }</li>
<li>}</li>
<li>int main()</li>
<li>{</li>
<li>    int n;                  // how many times we will allow analysis before</li>
<li>reading</li>
<li>    int decision;</li>
<li>    News new1 = {1,"heading 1.", "body 1"};</li>
<li>    News new2 = {2,"heading 2.", "body 2"};</li>
<li>    News new3 = {3,"Heading 3.", "body 3"};</li>
<li>    News new4 = {4,"heading 4.", "body 4"};</li>
<li>    News new5 = {5,"heading 5.", "body 5"};</li>
<li>    News new6 = {6,"heading 6.", "body 6"};</li>
<li>    News new7 = {7,"Heading 7.", "body 7"};</li>
<li>    News new8 = {8,"Heading 8.", "body 8"};</li>
<li>    News new9 = {9,"heading 9.", "body 9"};</li>
<li>    printf("\t\t\tWelcome to the news sorting!\n\n\n\n");</li>
<li>    for (n=0;n<15;n++){</li>
<li>       printf("%d-->  %s\n", new1.ID, new1.name,0);</li>
<li>       printf("%d-->  %s\n", new2.ID, new2.name,0);</li>
<li>       printf("%d-->  %s\n", new3.ID, new3.name,0);</li>
<li>       printf("%d-->  %s\n", new4.ID, new4.name,0);</li>
<li>       printf("%d-->  %s\n", new5.ID, new5.name,0);</li>
<li>       printf("%d-->  %s\n", new6.ID, new6.name,0);</li>
<li>       printf("%d-->  %s\n", new7.ID, new7.name,0);</li>
<li>       printf("%d-->  %s\n", new8.ID, new8.name,0);</li>
<li>       printf("%d-->  %s\n\n", new9.ID, new9.name,0);</li>
<li>       printf("Enter number + ENTER for reading: ");</li>
<li>       scanf("%d",&decision);</li>
<li>        if (decision==1){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new1.text);</li>
<li>            new1.read++;}</li>
<li>        if (decision==2){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new2.text);</li>
<li>            new2.read++;}</li>
<li>        if (decision==3){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new3.text);</li>
<li>            new3.read++;}</li>
<li>        if (decision==4){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new4.text);</li>
<li>            new4.read++;}</li>
<li>        if (decision==5){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new5.text);</li>
<li>            new5.read++;}</li>
<li>        if (decision==6){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new6.text);</li>
<li>            new6.read++;}</li>
<li>        if (decision==7){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new7.text);</li>
<li>            new7.read++;}</li>
<li>        if (decision==8){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new8.text);</li>
<li>            new8.read++;}</li>
<li>        if (decision==9){</li>
<li>            printf("---------------------------------------\n%s\n",</li>
<li>new9.text);</li>
<li>            new9.read++;}</li>
<li>            system("pause");</li>
<li>            system("cls");              }</li>
<li>    int array[N]={new1.read, new2.read, new3.read, new4.read, new5.read,</li>
<li>new6.read, new7.read, new8.read, new8.read};</li>
<li>    bubblesort(array); // call bubblesort</li>
<li>    //Most read news-------------------------------</li>
<li>    if (new1.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new1.name);}</li>
<li>    if (new2.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new2.name);}</li>
<li>    if (new3.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new3.name);}</li>
<li>    if (new4.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new4.name);}</li>
<li>    if (new5.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new5.name);}</li>
<li>    if (new6.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new5.name);}</li>
<li>    if (new7.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new7.name);}</li>
<li>    if (new8.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new8.name);}</li>
<li>    if (new9.read==array[0]){</li>
<li>         printf("Most read news is:\t%s", new9.name);}</li>
<li>    //-------------------------------------------------</li>
<li>    printf("\n");</li>
<li>    //2. Second most read news-------------------------------</li>
<li>    if (new1.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new1.name);}</li>
<li>    if (new2.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new2.name);}</li>
<li>    if (new3.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new3.name);}</li>
<li>    if (new4.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new4.name);}</li>
<li>    if (new5.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new5.name);}</li>
<li>    if (new6.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new6.name);}</li>
<li>    if (new7.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new7.name);}</li>
<li>    if (new8.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new8.name);}</li>
<li>    if (new9.read==array[1]){</li>
<li>         printf("2. Most read news is:\t%s", new9.name);}</li>
<li>    //-------------------------------------------------</li>
<li>    printf("\n");</li>
<li>    //3. most read news-------------------------------</li>
<li>    if (new1.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new1.name);}</li>
<li>    if (new2.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new2.name);}</li>
<li>    if (new3.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new3.name);}</li>
<li>    if (new4.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new4.name);}</li>
<li>    if (new5.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new5.name);}</li>
<li>    if (new6.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new6.name);}</li>
<li>    if (new7.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new7.name);}</li>
<li>    if (new8.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new8.name);}</li>
<li>    if (new9.read==array[2]){</li>
<li>         printf("3. Most read news is:\t%s", new9.name);}</li>
<li>    //-------------------------------------------------</li>
<li>    printf("\n\nDiffernce between the TOP news and one in the middle is:</li>
<li>%d - %d = %d", array[0], array[4], array[0] - array[4]);</li>
<li>    printf("\n\n");</li>
<li>    system("pause");</li>
<li>    return 0;</li>
<li>}</li>
</ul>
prljavibluzer
Newbie Poster
7 posts since Dec 2007
Reputation Points: 10
Solved Threads: 0
 

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.


First of all, why aren't you using an array fornews1 to news9? Wouldn't that make your program much shorter and easier to follow?

Why do you claim sorting looks pretty nasty and it's written badly? What is it you don't like about the code? Assuming it works (you never said it didn't) all I see is an inconsistent use of spaces throughout the program (usually in FOR and IF statements) making it hard to read. Add them around operators like
for (i = 0; i < N; i++){ if (array[j-1] < array[j]){
all over the program.

Why are you using the LIST tag in your CODE tags?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You