DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   sorting the "news" (http://www.daniweb.com/forums/thread106293.html)

prljavibluzer Jan 25th, 2008 4:55 pm
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?

Ancient Dragon Jan 25th, 2008 5:03 pm
Re: sorting the "news"
 
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.

WaltP Jan 25th, 2008 11:49 pm
Re: sorting the "news"
 
Quote:

Originally Posted by prljavibluzer (Post 518047)
The main point is to use some of the sorting algorithms. Any suggestions, ideas, pseudocoding since I am newbie?

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

prljavibluzer Feb 5th, 2008 6:37 pm
Re: sorting the "news"
 
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.

  1. #include <stdlib.h>

  2. #include <stdio.h>

  3. #define MAXLENGTH 20

  4. #define elementtype News

  5. #define N 9                //number of news

  6. typedef struct {

  7.       int ID;

  8.       char name[60];

  9.       char text[500];      //structure

  10.       int read;

  11.       } News;

  12. typedef struct {

  13. int last;

  14. elementtype elements[MAXLENGTH];

  15. } LIST;

  16. void bubblesort(int *array){

  17.     int i,j,k;

  18.     for (i=0; i<N; i++){

  19.         for (j=N-1; j>i; j--){

  20.             if (array[j-1]<array[j]){    //Bubble sort

  21.               k=array[j];

  22.               array[j]=array[j-1];

  23.               array[j-1]=k;

  24.               }

  25.         }

  26.     }

  27. }

  28. int main()

  29. {

  30.     int n;                  // how many times we will allow analysis before

  31. reading

  32.     int decision;

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

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

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

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

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

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

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

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

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

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

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

  44.       printf("%d-->  %s\n", new1.ID, new1.name,0);

  45.       printf("%d-->  %s\n", new2.ID, new2.name,0);

  46.       printf("%d-->  %s\n", new3.ID, new3.name,0);

  47.       printf("%d-->  %s\n", new4.ID, new4.name,0);

  48.       printf("%d-->  %s\n", new5.ID, new5.name,0);

  49.       printf("%d-->  %s\n", new6.ID, new6.name,0);

  50.       printf("%d-->  %s\n", new7.ID, new7.name,0);

  51.       printf("%d-->  %s\n", new8.ID, new8.name,0);

  52.       printf("%d-->  %s\n\n", new9.ID, new9.name,0);

  53.       printf("Enter number + ENTER for reading: ");

  54.       scanf("%d",&decision);

  55.         if (decision==1){

  56.             printf("---------------------------------------\n%s\n",

  57. new1.text);

  58.             new1.read++;}

  59.         if (decision==2){

  60.             printf("---------------------------------------\n%s\n",

  61. new2.text);

  62.             new2.read++;}

  63.         if (decision==3){

  64.             printf("---------------------------------------\n%s\n",

  65. new3.text);

  66.             new3.read++;}

  67.         if (decision==4){

  68.             printf("---------------------------------------\n%s\n",

  69. new4.text);

  70.             new4.read++;}

  71.         if (decision==5){

  72.             printf("---------------------------------------\n%s\n",

  73. new5.text);

  74.             new5.read++;}

  75.         if (decision==6){

  76.             printf("---------------------------------------\n%s\n",

  77. new6.text);

  78.             new6.read++;}

  79.         if (decision==7){

  80.             printf("---------------------------------------\n%s\n",

  81. new7.text);

  82.             new7.read++;}

  83.         if (decision==8){

  84.             printf("---------------------------------------\n%s\n",

  85. new8.text);

  86.             new8.read++;}

  87.         if (decision==9){

  88.             printf("---------------------------------------\n%s\n",

  89. new9.text);

  90.             new9.read++;}

  91.             system("pause");

  92.             system("cls");              }

  93.     int array[N]={new1.read, new2.read, new3.read, new4.read, new5.read,

  94. new6.read, new7.read, new8.read, new8.read};

  95.     bubblesort(array); // call bubblesort

  96.     //Most read news-------------------------------

  97.     if (new1.read==array[0]){

  98.         printf("Most read news is:\t%s", new1.name);}

  99.     if (new2.read==array[0]){

  100.         printf("Most read news is:\t%s", new2.name);}

  101.     if (new3.read==array[0]){

  102.         printf("Most read news is:\t%s", new3.name);}

  103.     if (new4.read==array[0]){

  104.         printf("Most read news is:\t%s", new4.name);}

  105.     if (new5.read==array[0]){

  106.         printf("Most read news is:\t%s", new5.name);}

  107.     if (new6.read==array[0]){

  108.         printf("Most read news is:\t%s", new5.name);}

  109.     if (new7.read==array[0]){

  110.         printf("Most read news is:\t%s", new7.name);}

  111.     if (new8.read==array[0]){

  112.         printf("Most read news is:\t%s", new8.name);}

  113.     if (new9.read==array[0]){

  114.         printf("Most read news is:\t%s", new9.name);}

  115.     //-------------------------------------------------

  116.     printf("\n");

  117.     //2. Second most read news-------------------------------

  118.     if (new1.read==array[1]){

  119.         printf("2. Most read news is:\t%s", new1.name);}

  120.     if (new2.read==array[1]){

  121.         printf("2. Most read news is:\t%s", new2.name);}

  122.     if (new3.read==array[1]){

  123.         printf("2. Most read news is:\t%s", new3.name);}

  124.     if (new4.read==array[1]){

  125.         printf("2. Most read news is:\t%s", new4.name);}

  126.     if (new5.read==array[1]){

  127.         printf("2. Most read news is:\t%s", new5.name);}

  128.     if (new6.read==array[1]){

  129.         printf("2. Most read news is:\t%s", new6.name);}

  130.     if (new7.read==array[1]){

  131.         printf("2. Most read news is:\t%s", new7.name);}

  132.     if (new8.read==array[1]){

  133.         printf("2. Most read news is:\t%s", new8.name);}

  134.     if (new9.read==array[1]){

  135.         printf("2. Most read news is:\t%s", new9.name);}

  136.     //-------------------------------------------------

  137.     printf("\n");

  138.     //3. most read news-------------------------------

  139.     if (new1.read==array[2]){

  140.         printf("3. Most read news is:\t%s", new1.name);}

  141.     if (new2.read==array[2]){

  142.         printf("3. Most read news is:\t%s", new2.name);}

  143.     if (new3.read==array[2]){

  144.         printf("3. Most read news is:\t%s", new3.name);}

  145.     if (new4.read==array[2]){

  146.         printf("3. Most read news is:\t%s", new4.name);}

  147.     if (new5.read==array[2]){

  148.         printf("3. Most read news is:\t%s", new5.name);}

  149.     if (new6.read==array[2]){

  150.         printf("3. Most read news is:\t%s", new6.name);}

  151.     if (new7.read==array[2]){

  152.         printf("3. Most read news is:\t%s", new7.name);}

  153.     if (new8.read==array[2]){

  154.         printf("3. Most read news is:\t%s", new8.name);}

  155.     if (new9.read==array[2]){

  156.         printf("3. Most read news is:\t%s", new9.name);}

  157.     //-------------------------------------------------

  158.     printf("\n\nDiffernce between the TOP news and one in the middle is:

  159. %d - %d = %d", array[0], array[4], array[0] - array[4]);

  160.     printf("\n\n");

  161.     system("pause");

  162.     return 0;

  163. }


WaltP Feb 6th, 2008 12:16 pm
Re: sorting the "news"
 
Quote:

Originally Posted by prljavibluzer (Post 526687)
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 for news1 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?


All times are GMT -4. The time now is 9:22 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC