Code:
- #include <stdlib.h>
- #include <stdio.h>
- #define MAXLENGTH 20
- #define elementtype News
- #define N 9 //number of news
- typedef 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 before
- reading
- 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;
- }