| | |
sorting the "news"
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
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?
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?
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.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Dec 2007
Posts: 7
Reputation:
Solved Threads: 0
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.
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.
C Syntax (Toggle Plain Text)
<ol style="list-style-type: decimal"><li>#include <stdlib.h></li> <li>#include <stdio.h></li> <li>#define MAXLENGTH 20</li> <li>#define elementtype News</li> <li>#define N 9 //number of news</li> <li>typedef struct {</li> <li> int ID;</li> <li> char name[60];</li> <li> char text[500]; //structure</li> <li> int read;</li> <li> } News; </li> <li>typedef struct {</li> <li>int last;</li> <li>elementtype elements[MAXLENGTH];</li> <li>} LIST;</li> <li>void bubblesort(int *array){</li> <li> int i,j,k;</li> <li> for (i=0; i<N; i++){</li> <li> for (j=N-1; j>i; j--){</li> <li> if (array[j-1]<array[j]){ //Bubble sort</li> <li> k=array[j];</li> <li> array[j]=array[j-1];</li> <li> array[j-1]=k;</li> <li> }</li> <li> }</li> <li> }</li> <li>} </li> <li>int main()</li> <li>{</li> <li> int n; // how many times we will allow analysis before</li> <li>reading</li> <li> int decision;</li> <li> News new1 = {1,"heading 1.", "body 1"};</li> <li> News new2 = {2,"heading 2.", "body 2"};</li> <li> News new3 = {3,"Heading 3.", "body 3"};</li> <li> News new4 = {4,"heading 4.", "body 4"};</li> <li> News new5 = {5,"heading 5.", "body 5"};</li> <li> News new6 = {6,"heading 6.", "body 6"};</li> <li> News new7 = {7,"Heading 7.", "body 7"};</li> <li> News new8 = {8,"Heading 8.", "body 8"};</li> <li> News new9 = {9,"heading 9.", "body 9"};</li> <li> printf("\t\t\tWelcome to the news sorting!\n\n\n\n");</li> <li> for (n=0;n<15;n++){</li> <li> printf("%d--> %s\n", new1.ID, new1.name,0);</li> <li> printf("%d--> %s\n", new2.ID, new2.name,0);</li> <li> printf("%d--> %s\n", new3.ID, new3.name,0);</li> <li> printf("%d--> %s\n", new4.ID, new4.name,0);</li> <li> printf("%d--> %s\n", new5.ID, new5.name,0);</li> <li> printf("%d--> %s\n", new6.ID, new6.name,0);</li> <li> printf("%d--> %s\n", new7.ID, new7.name,0);</li> <li> printf("%d--> %s\n", new8.ID, new8.name,0);</li> <li> printf("%d--> %s\n\n", new9.ID, new9.name,0);</li> <li> printf("Enter number + ENTER for reading: ");</li> <li> scanf("%d",&decision);</li> <li> if (decision==1){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new1.text);</li> <li> new1.read++;}</li> <li> if (decision==2){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new2.text);</li> <li> new2.read++;}</li> <li> if (decision==3){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new3.text);</li> <li> new3.read++;}</li> <li> if (decision==4){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new4.text);</li> <li> new4.read++;}</li> <li> if (decision==5){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new5.text);</li> <li> new5.read++;}</li> <li> if (decision==6){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new6.text);</li> <li> new6.read++;}</li> <li> if (decision==7){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new7.text);</li> <li> new7.read++;}</li> <li> if (decision==8){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new8.text);</li> <li> new8.read++;}</li> <li> if (decision==9){</li> <li> printf("---------------------------------------\n%s\n",</li> <li>new9.text);</li> <li> new9.read++;}</li> <li> system("pause");</li> <li> system("cls"); } </li> <li> int array[N]={new1.read, new2.read, new3.read, new4.read, new5.read,</li> <li>new6.read, new7.read, new8.read, new8.read}; </li> <li> bubblesort(array); // call bubblesort </li> <li> //Most read news-------------------------------</li> <li> if (new1.read==array[0]){</li> <li> printf("Most read news is:\t%s", new1.name);}</li> <li> if (new2.read==array[0]){</li> <li> printf("Most read news is:\t%s", new2.name);}</li> <li> if (new3.read==array[0]){</li> <li> printf("Most read news is:\t%s", new3.name);}</li> <li> if (new4.read==array[0]){</li> <li> printf("Most read news is:\t%s", new4.name);}</li> <li> if (new5.read==array[0]){</li> <li> printf("Most read news is:\t%s", new5.name);}</li> <li> if (new6.read==array[0]){</li> <li> printf("Most read news is:\t%s", new5.name);}</li> <li> if (new7.read==array[0]){</li> <li> printf("Most read news is:\t%s", new7.name);}</li> <li> if (new8.read==array[0]){</li> <li> printf("Most read news is:\t%s", new8.name);}</li> <li> if (new9.read==array[0]){</li> <li> printf("Most read news is:\t%s", new9.name);}</li> <li> //-------------------------------------------------</li> <li> printf("\n");</li> <li> //2. Second most read news-------------------------------</li> <li> if (new1.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new1.name);}</li> <li> if (new2.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new2.name);}</li> <li> if (new3.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new3.name);}</li> <li> if (new4.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new4.name);}</li> <li> if (new5.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new5.name);}</li> <li> if (new6.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new6.name);}</li> <li> if (new7.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new7.name);}</li> <li> if (new8.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new8.name);}</li> <li> if (new9.read==array[1]){</li> <li> printf("2. Most read news is:\t%s", new9.name);}</li> <li> //-------------------------------------------------</li> <li> printf("\n");</li> <li> //3. most read news-------------------------------</li> <li> if (new1.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new1.name);}</li> <li> if (new2.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new2.name);}</li> <li> if (new3.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new3.name);}</li> <li> if (new4.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new4.name);}</li> <li> if (new5.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new5.name);}</li> <li> if (new6.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new6.name);}</li> <li> if (new7.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new7.name);}</li> <li> if (new8.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new8.name);}</li> <li> if (new9.read==array[2]){</li> <li> printf("3. Most read news is:\t%s", new9.name);}</li> <li> //------------------------------------------------- </li> <li> printf("\n\nDiffernce between the TOP news and one in the middle is:</li> <li>%d - %d = %d", array[0], array[4], array[0] - array[4]); </li> <li> printf("\n\n");</li> <li> system("pause");</li> <li> return 0;</li> <li>}</li> </ol>
Last edited by prljavibluzer; Feb 5th, 2008 at 6:37 pm.
•
•
•
•
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.
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?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Other Threads in the C Forum
- Previous Thread: vprintf to printf
- Next Thread: print the number 7011 as 011 (take off a 7)
Views: 706 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware highest homework inches incrementoperators kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation threads turboc unix user variable visualstudio voidmain() wab win32 windows.h






