943,569 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 821
  • C RSS
Jan 25th, 2008
0

sorting the "news"

Expand Post »
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prljavibluzer is offline Offline
7 posts
since Dec 2007
Jan 25th, 2008
0

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.
Last edited by Ancient Dragon; Jan 25th, 2008 at 5:04 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005
Jan 25th, 2008
0

Re: sorting the "news"

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.
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,716 posts
since May 2006
Feb 5th, 2008
0

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. <ol style="list-style-type: decimal"><li>#include <stdlib.h></li>
  2. <li>#include <stdio.h></li>
  3. <li>#define MAXLENGTH 20</li>
  4. <li>#define elementtype News</li>
  5. <li>#define N 9 //number of news</li>
  6. <li>typedef struct {</li>
  7. <li> int ID;</li>
  8. <li> char name[60];</li>
  9. <li> char text[500]; //structure</li>
  10. <li> int read;</li>
  11. <li> } News;
  12. </li>
  13. <li>typedef struct {</li>
  14. <li>int last;</li>
  15. <li>elementtype elements[MAXLENGTH];</li>
  16. <li>} LIST;</li>
  17. <li>void bubblesort(int *array){</li>
  18. <li> int i,j,k;</li>
  19. <li> for (i=0; i<N; i++){</li>
  20. <li> for (j=N-1; j>i; j--){</li>
  21. <li> if (array[j-1]<array[j]){ //Bubble sort</li>
  22. <li> k=array[j];</li>
  23. <li> array[j]=array[j-1];</li>
  24. <li> array[j-1]=k;</li>
  25. <li> }</li>
  26. <li> }</li>
  27. <li> }</li>
  28. <li>}
  29. </li>
  30. <li>int main()</li>
  31. <li>{</li>
  32. <li> int n; // how many times we will allow analysis before</li>
  33. <li>reading</li>
  34. <li> int decision;</li>
  35. <li> News new1 = {1,"heading 1.", "body 1"};</li>
  36. <li> News new2 = {2,"heading 2.", "body 2"};</li>
  37. <li> News new3 = {3,"Heading 3.", "body 3"};</li>
  38. <li> News new4 = {4,"heading 4.", "body 4"};</li>
  39. <li> News new5 = {5,"heading 5.", "body 5"};</li>
  40. <li> News new6 = {6,"heading 6.", "body 6"};</li>
  41. <li> News new7 = {7,"Heading 7.", "body 7"};</li>
  42. <li> News new8 = {8,"Heading 8.", "body 8"};</li>
  43. <li> News new9 = {9,"heading 9.", "body 9"};</li>
  44. <li> printf("\t\t\tWelcome to the news sorting!\n\n\n\n");</li>
  45. <li> for (n=0;n<15;n++){</li>
  46. <li> printf("%d--> %s\n", new1.ID, new1.name,0);</li>
  47. <li> printf("%d--> %s\n", new2.ID, new2.name,0);</li>
  48. <li> printf("%d--> %s\n", new3.ID, new3.name,0);</li>
  49. <li> printf("%d--> %s\n", new4.ID, new4.name,0);</li>
  50. <li> printf("%d--> %s\n", new5.ID, new5.name,0);</li>
  51. <li> printf("%d--> %s\n", new6.ID, new6.name,0);</li>
  52. <li> printf("%d--> %s\n", new7.ID, new7.name,0);</li>
  53. <li> printf("%d--> %s\n", new8.ID, new8.name,0);</li>
  54. <li> printf("%d--> %s\n\n", new9.ID, new9.name,0);</li>
  55. <li> printf("Enter number + ENTER for reading: ");</li>
  56. <li> scanf("%d",&decision);</li>
  57. <li> if (decision==1){</li>
  58. <li> printf("---------------------------------------\n%s\n",</li>
  59. <li>new1.text);</li>
  60. <li> new1.read++;}</li>
  61. <li> if (decision==2){</li>
  62. <li> printf("---------------------------------------\n%s\n",</li>
  63. <li>new2.text);</li>
  64. <li> new2.read++;}</li>
  65. <li> if (decision==3){</li>
  66. <li> printf("---------------------------------------\n%s\n",</li>
  67. <li>new3.text);</li>
  68. <li> new3.read++;}</li>
  69. <li> if (decision==4){</li>
  70. <li> printf("---------------------------------------\n%s\n",</li>
  71. <li>new4.text);</li>
  72. <li> new4.read++;}</li>
  73. <li> if (decision==5){</li>
  74. <li> printf("---------------------------------------\n%s\n",</li>
  75. <li>new5.text);</li>
  76. <li> new5.read++;}</li>
  77. <li> if (decision==6){</li>
  78. <li> printf("---------------------------------------\n%s\n",</li>
  79. <li>new6.text);</li>
  80. <li> new6.read++;}</li>
  81. <li> if (decision==7){</li>
  82. <li> printf("---------------------------------------\n%s\n",</li>
  83. <li>new7.text);</li>
  84. <li> new7.read++;}</li>
  85. <li> if (decision==8){</li>
  86. <li> printf("---------------------------------------\n%s\n",</li>
  87. <li>new8.text);</li>
  88. <li> new8.read++;}</li>
  89. <li> if (decision==9){</li>
  90. <li> printf("---------------------------------------\n%s\n",</li>
  91. <li>new9.text);</li>
  92. <li> new9.read++;}</li>
  93. <li> system("pause");</li>
  94. <li> system("cls"); }
  95. </li>
  96. <li> int array[N]={new1.read, new2.read, new3.read, new4.read, new5.read,</li>
  97. <li>new6.read, new7.read, new8.read, new8.read};
  98. </li>
  99. <li> bubblesort(array); // call bubblesort
  100. </li>
  101. <li> //Most read news-------------------------------</li>
  102. <li> if (new1.read==array[0]){</li>
  103. <li> printf("Most read news is:\t%s", new1.name);}</li>
  104. <li> if (new2.read==array[0]){</li>
  105. <li> printf("Most read news is:\t%s", new2.name);}</li>
  106. <li> if (new3.read==array[0]){</li>
  107. <li> printf("Most read news is:\t%s", new3.name);}</li>
  108. <li> if (new4.read==array[0]){</li>
  109. <li> printf("Most read news is:\t%s", new4.name);}</li>
  110. <li> if (new5.read==array[0]){</li>
  111. <li> printf("Most read news is:\t%s", new5.name);}</li>
  112. <li> if (new6.read==array[0]){</li>
  113. <li> printf("Most read news is:\t%s", new5.name);}</li>
  114. <li> if (new7.read==array[0]){</li>
  115. <li> printf("Most read news is:\t%s", new7.name);}</li>
  116. <li> if (new8.read==array[0]){</li>
  117. <li> printf("Most read news is:\t%s", new8.name);}</li>
  118. <li> if (new9.read==array[0]){</li>
  119. <li> printf("Most read news is:\t%s", new9.name);}</li>
  120. <li> //-------------------------------------------------</li>
  121. <li> printf("\n");</li>
  122. <li> //2. Second most read news-------------------------------</li>
  123. <li> if (new1.read==array[1]){</li>
  124. <li> printf("2. Most read news is:\t%s", new1.name);}</li>
  125. <li> if (new2.read==array[1]){</li>
  126. <li> printf("2. Most read news is:\t%s", new2.name);}</li>
  127. <li> if (new3.read==array[1]){</li>
  128. <li> printf("2. Most read news is:\t%s", new3.name);}</li>
  129. <li> if (new4.read==array[1]){</li>
  130. <li> printf("2. Most read news is:\t%s", new4.name);}</li>
  131. <li> if (new5.read==array[1]){</li>
  132. <li> printf("2. Most read news is:\t%s", new5.name);}</li>
  133. <li> if (new6.read==array[1]){</li>
  134. <li> printf("2. Most read news is:\t%s", new6.name);}</li>
  135. <li> if (new7.read==array[1]){</li>
  136. <li> printf("2. Most read news is:\t%s", new7.name);}</li>
  137. <li> if (new8.read==array[1]){</li>
  138. <li> printf("2. Most read news is:\t%s", new8.name);}</li>
  139. <li> if (new9.read==array[1]){</li>
  140. <li> printf("2. Most read news is:\t%s", new9.name);}</li>
  141. <li> //-------------------------------------------------</li>
  142. <li> printf("\n");</li>
  143. <li> //3. most read news-------------------------------</li>
  144. <li> if (new1.read==array[2]){</li>
  145. <li> printf("3. Most read news is:\t%s", new1.name);}</li>
  146. <li> if (new2.read==array[2]){</li>
  147. <li> printf("3. Most read news is:\t%s", new2.name);}</li>
  148. <li> if (new3.read==array[2]){</li>
  149. <li> printf("3. Most read news is:\t%s", new3.name);}</li>
  150. <li> if (new4.read==array[2]){</li>
  151. <li> printf("3. Most read news is:\t%s", new4.name);}</li>
  152. <li> if (new5.read==array[2]){</li>
  153. <li> printf("3. Most read news is:\t%s", new5.name);}</li>
  154. <li> if (new6.read==array[2]){</li>
  155. <li> printf("3. Most read news is:\t%s", new6.name);}</li>
  156. <li> if (new7.read==array[2]){</li>
  157. <li> printf("3. Most read news is:\t%s", new7.name);}</li>
  158. <li> if (new8.read==array[2]){</li>
  159. <li> printf("3. Most read news is:\t%s", new8.name);}</li>
  160. <li> if (new9.read==array[2]){</li>
  161. <li> printf("3. Most read news is:\t%s", new9.name);}</li>
  162. <li> //------------------------------------------------- </li>
  163. <li> printf("\n\nDiffernce between the TOP news and one in the middle is:</li>
  164. <li>%d - %d = %d", array[0], array[4], array[0] - array[4]);
  165. </li>
  166. <li> printf("\n\n");</li>
  167. <li> system("pause");</li>
  168. <li> return 0;</li>
  169. <li>}</li>
  170. </ol>
Last edited by prljavibluzer; Feb 5th, 2008 at 6:37 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prljavibluzer is offline Offline
7 posts
since Dec 2007
Feb 6th, 2008
0

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.
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?
Moderator
Reputation Points: 3275
Solved Threads: 890
Posting Sage
WaltP is online now Online
7,716 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: vprintf to printf
Next Thread in C Forum Timeline: print the number 7011 as 011 (take off a 7)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC