# "sorting news" assignment

• 02-05-2008
prljavibluzer
"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:

```#include <stdlib.h>#include <stdio.h>#define MAXLENGTH 20#define elementtype News#define N 9                //number of newstypedef 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 beforereading    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==8){            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;}```
• 02-05-2008
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.
• 02-05-2008
iMalc
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.
• 02-06-2008
prljavibluzer
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".
• 02-06-2008
Elysia
No. That's up to you.
Even should we rewrite, you didn't write it, and so you fail either way.
• 02-06-2008
prljavibluzer
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.
• 02-06-2008
zacs7
> 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.
• 02-06-2008
Elysia
Quote:

Originally Posted by prljavibluzer
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.