Thread: "sorting news" assignment

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    3

    Exclamation "sorting news" assignment

    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 you good people.

    Code:
    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. }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    When I see almost the same code, repeated over and over like this:
    Code:
    #  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"};
    I have to pause.

    For only nine items, and the coding is already done, well I'd not worry about re-writing it.
    The tire that holds air well, doesn't need a flat repair applied to it.

    But obviously if I had 100 of these items to do, and the code had NOT been written, of course I'd use a better representation of the data/variables, to allow a loop to handle all the nearly repeated, lines of code.

    But for nine items when it's already working - I wouldn't bother.

    The bubblesort looks odd, but it should work fine. IMO it is a "bubblesort" that is a true bubblesort, and not one of the several simple swap sorts that are so frequently called bubblesorts. What makes it that, is the comparisons are never more than 1 array element away from each other. The sorted elements truly "bubble" (not jump), to the top (or bottom).

    For thousands of items, I wouldn't recommend bubblesort, but for nine items it's a great choice.
    Last edited by Adak; 02-05-2008 at 09:27 PM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If I was marking that code you would fail.
    USE AN ARRAY for crying out loud!!!

    Also, the sorting algorithm itself is fine, but you're not supposed to be sorting integers, you're supposed to be sorting structures.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    Could you please rewrite that part or somebody else can show some good will?

    Yes, I understand what are you getting at and what's the main reason for
    this forum but now, I have less than 24 hours to finish this or I'll be
    in problems. That wouldn't be so bad if I don't have to do some other stuff
    too in this "unhappy 24 hours".

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No. That's up to you.
    Even should we rewrite, you didn't write it, and so you fail either way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    3
    I'm not requesting from you and others to rewrite to whole program, I'm just stuck with time and can't do it as intended. If I had only one more day, I could do it like it should be done.
    Conclusion? It's not about lazyness, but the lack of time and other stuff I have to do today.


    So... Who are you to judge here?
    If you don't want to help me, please don't comment then.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > So... Who are you to judge here?
    It's Elysia -- Goddess of the Sky
    The real question is, who are you to argue?

    > I have less than 24 hours to finish this or I'll be in problems.
    It should take 24 minutes. Ohh and you're already "in problems".

    Make it work, and if you happen to get an E instead of an F, well done. The reason you're strapped for time has nothing to do with your programming skill, it's because you didn't take 5 minutes to design your program in the first place. Rather than tapping away at the keyboard, get a pen and some paper.
    Last edited by zacs7; 02-06-2008 at 06:22 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by prljavibluzer View Post
    I'm not requesting from you and others to rewrite to whole program, I'm just stuck with time and can't do it as intended. If I had only one more day, I could do it like it should be done.
    Conclusion? It's not about lazyness, but the lack of time and other stuff I have to do today.

    So... Who are you to judge here?
    If you don't want to help me, please don't comment then.
    I'm simply giving you the facts. If we do it, then you learn nothing. You didn't do it. It's called cheating.
    If we do it for you, you don't learn, so you fail.
    If you don't finish it in time, you fail.
    So get moving. We can help you understand what needs be done, but we won't do it for you.
    We are not the time judge here. If you can't do it in time, then ask for more time. If you can't have more time, then too bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM