943,948 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2521
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2007
0

ootball league table - add data

Expand Post »
ok need a bit of help.

got a program to set up football league table and input all the scores etc etc

just tryin to figure a way to add scores and that from there, also the display code seems a bit messy anyway i can put it into a incremental loop?

well the code is....

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #define MAX 8
  5. #define TRUE 1
  6. #define FALSE 0
  7.  
  8. void prior_insertion(void);
  9.  
  10. void draw_table (void);
  11. void insertion_sort (void);
  12. void draw_sorted (void);
  13. void gfor_against (void);
  14. void sort_alpha (void);
  15.  
  16.  
  17. struct
  18. {
  19. char team[20];
  20. int played;
  21. int points;
  22. int goalsf;
  23. int goalsa;
  24. int goalsd;
  25. }arrdetails[8];
  26.  
  27. main()
  28. {
  29. prior_insertion();
  30. draw_table();
  31. insertion_sort();
  32. gfor_against();
  33. sort_alpha();
  34. draw_sorted ();
  35.  
  36. }
  37.  
  38. void prior_insertion(void)
  39.  
  40. {
  41. /*teams*/
  42. strcpy(arrdetails[0].team,"Stoke City");
  43. strcpy(arrdetails[1].team,"Manchester City");
  44. strcpy(arrdetails[2].team,"Port Vale");
  45. strcpy(arrdetails[3].team,"Derby County");
  46. strcpy(arrdetails[4].team,"Birmingham Town");
  47. strcpy(arrdetails[5].team,"Crystal Palace");
  48. strcpy(arrdetails[6].team,"Luton");
  49. strcpy(arrdetails[7].team,"Hull City");
  50.  
  51. /*played*/
  52. arrdetails[0].played=3;
  53. arrdetails[1].played=3;
  54. arrdetails[2].played=3;
  55. arrdetails[3].played=3;
  56. arrdetails[4].played=3;
  57. arrdetails[5].played=3;
  58. arrdetails[6].played=3;
  59. arrdetails[7].played=3;
  60.  
  61. /*points*/
  62. arrdetails[0].points=5;
  63. arrdetails[1].points=3;
  64. arrdetails[2].points=1;
  65. arrdetails[3].points=9;
  66. arrdetails[4].points=7;
  67. arrdetails[5].points=5;
  68. arrdetails[6].points=1;
  69. arrdetails[7].points=1;
  70.  
  71. /*goalsfor*/
  72. arrdetails[0].goalsf=4;
  73. arrdetails[1].goalsf=5;
  74. arrdetails[2].goalsf=2;
  75. arrdetails[3].goalsf=7;
  76. arrdetails[4].goalsf=4;
  77. arrdetails[5].goalsf=4;
  78. arrdetails[6].goalsf=0;
  79. arrdetails[7].goalsf=0;
  80.  
  81. /*goalsagainst*/
  82. arrdetails[0].goalsa=3;
  83. arrdetails[1].goalsa=6;
  84. arrdetails[2].goalsa=6;
  85. arrdetails[3].goalsa=2;
  86. arrdetails[4].goalsa=2;
  87. arrdetails[5].goalsa=3;
  88. arrdetails[6].goalsa=2;
  89. arrdetails[7].goalsa=2;
  90. }
  91.  
  92.  
  93. void draw_table (void)
  94. {
  95. printf("BELOW IS BEFORE AN INSERTION SORT\n");
  96. printf("\t\t\tPlayed\tPoints\tGoals for\tGoals against\n");
  97. printf("\n");
  98. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[0].team,arrdetails[0].played,arrdetails[0].points,arrdetails[0].goalsf,arrdetails[0].goalsa);
  99. printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[1].team,arrdetails[1].played,arrdetails[1].points,arrdetails[1].goalsf,arrdetails[1].goalsa);
  100. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[2].team,arrdetails[2].played,arrdetails[2].points,arrdetails[2].goalsf,arrdetails[2].goalsa);
  101. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[3].team,arrdetails[3].played,arrdetails[3].points,arrdetails[3].goalsf,arrdetails[3].goalsa);
  102. printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[4].team,arrdetails[4].played,arrdetails[4].points,arrdetails[4].goalsf,arrdetails[4].goalsa);
  103. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[5].team,arrdetails[5].played,arrdetails[5].points,arrdetails[5].goalsf,arrdetails[5].goalsa);
  104. printf("%s \t\t\t %d \t %d \t %d \t \t %d\n",arrdetails[6].team,arrdetails[6].played,arrdetails[6].points,arrdetails[6].goalsf,arrdetails[6].goalsa);
  105. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[7].team,arrdetails[7].played,arrdetails[7].points,arrdetails[7].goalsf,arrdetails[7].goalsa);
  106. }
  107.  
  108. void insertion_sort (void)
  109. {
  110. int pinsert;
  111. int temp;
  112. int z,i,in,gap;
  113. int value;
  114. char tmpteam[20];
  115. int tmpplayed;
  116. int tmppoints;
  117. int tmpgoalsf;
  118. int tmpgoalsa;
  119. z = 0;
  120.  
  121. value=0;
  122.  
  123. for (z = 0; z < MAX; z++)
  124. {
  125. while (arrdetails[z].points > arrdetails[z + 1].points)
  126. {
  127. strcpy(tmpteam,arrdetails[z+1].team);
  128. tmpplayed=arrdetails[z+1].played;
  129. tmppoints=arrdetails[z+1].points;
  130. tmpgoalsf=arrdetails[z+1].goalsf;
  131. tmpgoalsa=arrdetails[z+1].goalsa;
  132.  
  133. strcpy(arrdetails[z+1].team,arrdetails[z].team);
  134. arrdetails[z+1].played=arrdetails[z].played;
  135. arrdetails[z+1].points=arrdetails[z].points;
  136. arrdetails[z+1].goalsf=arrdetails[z].goalsf;
  137. arrdetails[z+1].goalsa=arrdetails[z].goalsa;
  138.  
  139. strcpy(arrdetails[z].team,tmpteam);
  140. arrdetails[z].played=tmpplayed;
  141. arrdetails[z].points=tmppoints;
  142. arrdetails[z].goalsf=tmpgoalsf;
  143. arrdetails[z].goalsa=tmpgoalsa;
  144.  
  145. z--;
  146. }
  147. }
  148. }
  149.  
  150. void gfor_against (void)
  151. {
  152. int value,z,i,gap,in,x;
  153. char tmpteam[20];
  154. int tmpplayed;
  155. int tmppoints;
  156. int tmpgoalsf;
  157. int tmpgoalsa;
  158. int alpha;
  159. x = 0;
  160.  
  161. for (z = 0; z < MAX; z++)/*work out the goals difference*/
  162. {
  163. arrdetails[z].goalsd=arrdetails[z].goalsf-arrdetails[z].goalsa;
  164. }
  165.  
  166. for (z = 0; z < MAX; z++)
  167. {
  168. if(arrdetails[z].points == arrdetails[z+1].points)
  169. {
  170. while (arrdetails[z].goalsd > arrdetails[z + 1].goalsd)
  171. {
  172. strcpy(tmpteam,arrdetails[z+1].team);
  173. tmpplayed=arrdetails[z+1].played;
  174. tmppoints=arrdetails[z+1].points;
  175. tmpgoalsf=arrdetails[z+1].goalsf;
  176. tmpgoalsa=arrdetails[z+1].goalsa;
  177.  
  178. strcpy(arrdetails[z+1].team,arrdetails[z].team);
  179. arrdetails[z+1].played=arrdetails[z].played;
  180. arrdetails[z+1].points=arrdetails[z].points;
  181. arrdetails[z+1].goalsf=arrdetails[z].goalsf;
  182. arrdetails[z+1].goalsa=arrdetails[z].goalsa;
  183.  
  184. strcpy(arrdetails[z].team,tmpteam);
  185. arrdetails[z].played=tmpplayed;
  186. arrdetails[z].points=tmppoints;
  187. arrdetails[z].goalsf=tmpgoalsf;
  188. arrdetails[z].goalsa=tmpgoalsa;
  189.  
  190. z--;
  191. }
  192. }
  193. }
  194. }
  195.  
  196.  
  197. void sort_alpha (void)
  198. {
  199. int z;
  200. int j;
  201. char tmpteam[20];
  202. int tmpplayed;
  203. int tmppoints;
  204. int tmpgoalsf;
  205. int tmpgoalsa;
  206. int i ;
  207.  
  208. if (strcmp(arrdetails[z].team, arrdetails[z+1].team ))
  209. {
  210. i = -1;
  211. strcpy(tmpteam,arrdetails[z+1].team);
  212. tmpplayed=arrdetails[z+1].played;
  213. tmppoints=arrdetails[z+1].points;
  214. tmpgoalsf=arrdetails[z+1].goalsf;
  215. tmpgoalsa=arrdetails[z+1].goalsa;
  216.  
  217. strcpy(arrdetails[z+1].team,arrdetails[z].team);
  218. arrdetails[z+1].played=arrdetails[z].played;
  219. arrdetails[z+1].points=arrdetails[z].points;
  220. arrdetails[z+1].goalsf=arrdetails[z].goalsf;
  221. arrdetails[z+1].goalsa=arrdetails[z].goalsa;
  222.  
  223. strcpy(arrdetails[z].team, arrdetails[j].team);
  224. arrdetails[z].played=arrdetails[j].played;
  225. arrdetails[z].points=arrdetails[j].points;
  226. arrdetails[z].goalsf=arrdetails[j].goalsf;
  227. arrdetails[z].goalsa=arrdetails[j].goalsa;
  228. }
  229.  
  230. else
  231. {
  232. i = 1;
  233. }
  234.  
  235.  
  236.  
  237.  
  238. }
  239.  
  240. void draw_sorted (void)
  241. {
  242.  
  243. printf("\nAFTER INSERTION SORT\n");
  244. printf("\t\t\tPlayed\tPoints\tGoals for\tGoals against\n");
  245. printf("\n");
  246. printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[7].team,arrdetails[7].played,arrdetails[7].points,arrdetails[7].goalsf,arrdetails[7].goalsa);
  247. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[6].team,arrdetails[6].played,arrdetails[6].points,arrdetails[6].goalsf,arrdetails[6].goalsa);
  248. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[5].team,arrdetails[5].played,arrdetails[5].points,arrdetails[5].goalsf,arrdetails[5].goalsa);
  249. printf("%s \t %d \t %d \t %d \t \t %d\n",arrdetails[4].team,arrdetails[4].played,arrdetails[4].points,arrdetails[4].goalsf,arrdetails[4].goalsa);
  250. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[3].team,arrdetails[3].played,arrdetails[3].points,arrdetails[3].goalsf,arrdetails[3].goalsa);
  251. printf("%s \t\t\t %d \t %d \t %d \t \t %d\n",arrdetails[2].team,arrdetails[2].played,arrdetails[2].points,arrdetails[2].goalsf,arrdetails[2].goalsa);
  252. printf("%s \t\t %d \t %d \t %d \t \t %d\n",arrdetails[1].team,arrdetails[1].played,arrdetails[1].points,arrdetails[1].goalsf,arrdetails[1].goalsa);
  253. printf("%s \t\t\t %d \t %d \t %d \t \t %d\n",arrdetails[0].team,arrdetails[0].played,arrdetails[0].points,arrdetails[0].goalsf,arrdetails[0].goalsa);
  254. }

thanks everyone
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fightfox06 is offline Offline
12 posts
since Mar 2007
Mar 4th, 2007
0

Re: ootball league table - add data

Wow!!! First post and you used CODE tags! Thank You. What is it you know that no other first-time poster can figure out? :mrgreen:


Click to Expand / Collapse  Quote originally posted by fightfox06 ...
ok need a bit of help.
Good, that's what we're here for...

Click to Expand / Collapse  Quote originally posted by fightfox06 ...
just tryin to figure a way to add scores and that from there,
Huh? What's that mean?

Click to Expand / Collapse  Quote originally posted by fightfox06 ...
also the display code seems a bit messy anyway i can put it into a incremental loop?
I suppose...

The code is quite long (obviously) and you give no indication where you need changes. Maybe you need to post only the section of the code you need help with and a better explanation of exactly what you are looking for.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Mar 4th, 2007
0

Re: ootball league table - add data

ok thanks for sayin i can use code tags aint hard reli if i can program in c and java

so yea that code inputs a football league table and then outputs it unsorted and then sorted.....
...
...
...what i need help with is how would i go about adding a way off adding more data

for example add a game played always one.
points either 0 for a loss, 1 for a draw or 3 for a win, goals scored and goals scored against

want to end up with a menu system and to be able to do all the options below....so far got B and D sorted out just taking things one step at a time

****************************
* Football League *
****************************
A. Add A New Team
B. Display Current Table
C. Enter Match Details
D. Exit System


thanks again
Last edited by fightfox06; Mar 4th, 2007 at 2:54 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fightfox06 is offline Offline
12 posts
since Mar 2007
Mar 4th, 2007
0

Re: ootball league table - add data

Click to Expand / Collapse  Quote originally posted by fightfox06 ...
...what i need help with is how would i go about adding a way off adding more data

for example add a game played always one.
points either 0 for a loss, 1 for a draw or 3 for a win, goals scored and goals scored against

want to end up with a menu system and to be able to do all the options below....so far got B and D sorted out just taking things one step at a time

****************************
* Football League *
****************************
A. Add A New Team
B. Display Current Table
C. Enter Match Details
D. Exit System
Taking one step at a time is the best way to program...

Start with the menu. Put a while loop in main() that
a) displays the menu
b) accept the menu selection
c) executes the appropriate function
d) exits when the EXIT selection is chosen

To add data, do you have a way to store the data for each game won? That would be the first thing. What do you have to save? What are the permissible values? Once you have that, write a function to input the information and load the data.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Mar 4th, 2007
0

Re: ootball league table - add data

so if i add sommate along the lines of
  1. main()
  2. {
  3. char option, rtn;
  4. option = print_header();
  5. while(option != 'q' && option != 'Q')
  6. {
  7. switch(option)
  8. {
  9. case 'a': case 'A': /*Enter team */
  10. printf("\nPlease Enter a new team:\n");
  11. break;
  12. case 'b': case'B': /*Display table*/
  13. printf("\nLeague Table:\n");
  14. break;
  15. case 'c': case'C': /*Match details*/
  16. printf("\nLeague Table:\n");
  17. break;
  18. case 'd': case 'D': /*Exit*/
  19. printf("\n*** FINISHED ***\n");
  20. exit(0);
  21. default: /*invalid option letter entererd*/
  22. printf("\nInvalid choice, Please try again\n");
  23. }
  24. option = print_header();
  25. }
  26. return 0;
  27. }

sorry for being slow, just not realy good at C java im not soo bad at.

we are doing 3 programming languages this term, C, Java and VB and i dont get C at all. cnt wait till next year when im only doing java

networking is more my cup of tea but we have to do the general route in our 1st year
Last edited by fightfox06; Mar 4th, 2007 at 3:46 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fightfox06 is offline Offline
12 posts
since Mar 2007
Mar 5th, 2007
0

Re: ootball league table - add data

Close....
  1. main() /**** main is an INT ****/
  2. {
  3. char option, rtn;
  4. option = print_header();
  5. while(option != 'q' && option != 'Q')
  6. {
  7. /**** display your menu ****/
  8. /**** accept option ****/
  9. switch(option)
  10. {
  11. case 'a': case 'A': /*Enter team */
  12. printf("\nPlease Enter a new team:\n");
  13. break;
  14. case 'b': case'B': /*Display table*/
  15. printf("\nLeague Table:\n");
  16. break;
  17. case 'c': case'C': /*Match details*/
  18. printf("\nLeague Table:\n");
  19. break;
  20. case 'd': case 'D': /*Exit*/
  21. printf("\n*** FINISHED ***\n");
  22. exit(0);
  23. default: /*invalid option letter entererd*/
  24. printf("\nInvalid choice, Please try again\n");
  25. }
  26. option = print_header();
  27. }
  28. return 0;
  29. }
In each case, call a function to process the selection.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Mar 5th, 2007
0

Re: ootball league table - add data

so how would i go about pointing for example case C to the function

  1.  
  2. void draw_sorted (void)

so that when b is pressed the table it displayed??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fightfox06 is offline Offline
12 posts
since Mar 2007
Mar 5th, 2007
0

Re: ootball league table - add data

I suppose you donno how to use scanf().
As you said you know java let me just say:
System.out.ptintln() = printf()
System.in.read() = scanf()
Here is a sample:

c++ Syntax (Toggle Plain Text)
  1. //Just gave a name to your struct..
  2. struct TeamInfo
  3. {
  4. char team[20];
  5. int played;
  6. int points;
  7. int goalsf;
  8. int goalsa;
  9. int goalsd;
  10. } ;
  11.  
  12. #define MAX_ALLOWED_TEAMS 20
  13. struct TeamInfo arrdetails[MAX_ALLOWED_TEAMS] ;
  14. int current_empty_slot = 0 ;
  15.  
  16. void take_team_input() ;
  17.  
  18. main() /**** main is an INT ****/
  19. {
  20. //...........
  21. while(option != 'q' && option != 'Q')
  22. {
  23. //...........
  24. switch(option)
  25. {
  26. //...........
  27. case 'c': case'C': /*Take match details input*/
  28. //just call the function that does what you want in this case..
  29. take_team_input() ;
  30. break;
  31. //...........
  32. }
  33. option = print_header();
  34. }
  35. return 0;
  36. }
  37.  
  38. void take_team_input()
  39. {
  40. char choice = 'Y' ;
  41.  
  42. if( MAX_ALLOWED_TEAMS <= current_empty_slot )
  43. {
  44. printf( "\nMax limit for team entries reached.", ) ;
  45. return ;
  46. }
  47.  
  48. while( ('y' == choice) || ('Y' == choice) )
  49. {
  50. //take the input:
  51. printf("\nTaking input for team number: %d", current_empty_slot ) ;
  52. printf("\nEnter team name: ") ; fflush(null) ;
  53. scanf( "%s", arrdetails[current_empty_slot].team ) ;
  54. printf("\nEnter number of games played: ") ; fflush(null) ;
  55. scanf( "%d", arrdetails[current_empty_slot].played ) ;
  56. printf("\nEnter goals for: ") ; fflush(null) ;
  57. scanf( "%d", arrdetails[current_empty_slot].goalsf ) ;
  58. //...... and so on...
  59. //now ask if he wants to enter any more...
  60. printf("\n\nWant to enter details for any more teams? [y/n]: ") ; fflush(null) ;
  61. scanf( "%c", choice ) ;
  62.  
  63. current_empty_slot++ :
  64. }
  65. }

"fflush(null) ;" isn't really mandatory, but lotsa ppl come back quoting problems with printf+scanf usage so...
Reputation Points: 254
Solved Threads: 74
Practically a Posting Shark
thekashyap is offline Offline
804 posts
since Feb 2007
Mar 5th, 2007
0

Re: ootball league table - add data

Click to Expand / Collapse  Quote originally posted by thekashyap ...
"fflush(null) ;" isn't really mandatory, but lotsa ppl come back quoting problems with printf+scanf usage so...
Even more ppl come back yelling at anyone that uses it improperly like this, too. fflush(null); will accomplish nothing at best, break your program at worst. You cannot flush the null device AFAIK. And it is only used for output, never input, streams. See this
Last edited by WaltP; Mar 5th, 2007 at 3:52 pm.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,741 posts
since May 2006
Mar 5th, 2007
0

Re: ootball league table - add data

so what do i need to use instead of the fflush(null); line??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fightfox06 is offline Offline
12 posts
since Mar 2007

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: linked list
Next Thread in C Forum Timeline: big o notation?





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


Follow us on Twitter


© 2011 DaniWeb® LLC