944,144 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2562
  • C RSS
Dec 9th, 2005
1

Bubble sort & File output jibrish errors???

Expand Post »
This program is almost the same as the one last i posted but here the bubble sort algorithm is not working, i get a compiler error saying::
size of cdRecords is not known
and
need explicit cast to convert

I cant work out what the problem is because another program with exact same details but different structure works fine,, and yes i have tried converting that one to a CD database,, same problem though......

also the file output here is all jibrish,, this it shouldnt do..


  1. //(Database Management System(DBMS), user interface and report generator for results.
  2. //Also a diagnostic tool that allows access to the database metrics.
  3. //This is to show the efficiency of the sort algorithms used.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <io.h>
  9.  
  10. #define MAX 10
  11.  
  12. //******************************************************************************************//
  13.  
  14. //Structure
  15.  
  16. struct CdRecords
  17. {
  18. char Artist[20];
  19. char Album[20];
  20. char Year[8];
  21. char Label[20];
  22. char Genre[20];
  23. };
  24. //******************************************************************************************//
  25. // FUNCTIONS
  26. void addRecord(struct CdRecords cdDB[]);
  27. void listRecords(struct CdRecords cdDB[]);
  28. void delRecord(struct CdRecords cdDB[]);
  29. void init_list(struct CdRecords cdDB[]);
  30. int menu_select(void);
  31. void loadFile (struct CdRecords cdDB[]);
  32. void saveFile(struct CdRecords cdDB[]);
  33. void SearchTitle(struct CdRecords cdDB[]);
  34. void findRecord(struct CdRecords cdDB[]);
  35.  
  36. void sortRecords(struct cdRecords cdDB[]);
  37. void naive_sort (struct cdRecords cdDB[], int arraySize, int * count);
  38. void swap (struct CdRecords * v1, struct cdRecords * v2);
  39.  
  40. const int datasize = 20;
  41.  
  42. //******************************************************************************************//
  43.  
  44. //MAIN FUNCTION
  45.  
  46. void main(void)
  47. {
  48. struct CdRecords cdDB[MAX];
  49.  
  50. char choice; //TO CAPTURE THE MENU SELECTION
  51.  
  52. init_list(cdDB); //CLEAR THE STRUCTURE
  53.  
  54. for (;;) { //LOOP AROUND MENU SELECTION UNTIL USER EXITS
  55.  
  56. choice =menu_select(); // CALL MENU_SELECT FUNCTION AND RETURN VALUE IN FIELD - CHOICE
  57. switch(choice){
  58. case 1:addRecord(cdDB);
  59. break;
  60. case 2:delRecord(cdDB);
  61. break;
  62. case 3:listRecords(cdDB);
  63. break;
  64. case 4:saveFile(cdDB);
  65. break;
  66. case 5:loadFile(cdDB);
  67. break;
  68. case 6:findRecord(cdDB);
  69. break;
  70. case 7:sortRecords(cdDB);
  71. break;
  72. case 8:exit(0);
  73. break;
  74. }//END SWITCH
  75. }//END FOR
  76.  
  77. }
  78. //******************************************************************************************//
  79.  
  80. //INITIALISE THE STRUCTURE
  81.  
  82. void init_list(struct CdRecords cdDB[])
  83. {
  84. int t; //TO CONTROL LOOP FOR CLEARING STRUCTURE
  85. for (t=0;t<MAX; t++)
  86. cdDB[t].Artist[0] = '\0'; //SET THE TITLE TO NULL
  87. }
  88. //******************************************************************************************//
  89.  
  90. //DISPLAY MENU OPTIONS
  91.  
  92. menu_select(void)
  93.  
  94. {char s [80];
  95. int c;
  96.  
  97. printf("*** MAIN MENU ***\n");
  98.  
  99. printf("1.Enter\n"); //DISPLAY MENU OPTIONS TO USER
  100. printf("2.Delete\n");
  101. printf("3.List\n");
  102. printf("4.Save File\n");
  103. printf("5.Load File\n");
  104. printf("6.Search\n");
  105. printf("7.Sort\n");
  106. printf("8.Exit\n");
  107.  
  108. do{
  109. printf("Enter choice ");
  110. gets(s);
  111. c = atoi(s);
  112.  
  113. }while(c<0 ||c> 8); // EXIT APPLICATION WHEN USER SELECTS 7
  114. return c;
  115. }
  116.  
  117. //******************************************************************************************//
  118.  
  119. // ADD NEW ENTRIES TO THE FILE
  120.  
  121. void addRecord(struct CdRecords cdDB[])
  122. {
  123. int slot; // INTEGER TO LOCATE FREE SPACE IN STRUCTURE
  124. char s[80]; // CALL THE FIND_FREE FUNCTION AND RETURN VALUE IN FIELD - SLOT
  125. slot = find_free(cdDB);
  126.  
  127. if (slot==-1){ //IF SLOT = -1 THEN THE STRUCTURE IS FULL
  128. printf("LIst Full");
  129. return;
  130. }
  131.  
  132. printf("Enter Artist: "); //PROMPT TO ADD TITLE
  133. gets(cdDB[slot].Artist);
  134.  
  135. printf("Enter Album: "); //PROMPT TO ADD AUTHOR
  136. gets(cdDB[slot].Album);
  137.  
  138. printf("Enter Label: "); //PROMPT TO ADD PUBLISHER
  139. gets(cdDB[slot].Label);
  140.  
  141. printf("Enter Year: "); //PROMPT TO ADD YEAR
  142. gets(cdDB[slot].Year);
  143.  
  144. printf("Enter Genre: "); //PROMPT TO ADD ISBN
  145. gets(cdDB[slot].Genre);
  146.  
  147.  
  148.  
  149. }
  150.  
  151. //******************************************************************************************//
  152.  
  153. //SEARCH FOR A TITLE
  154.  
  155. void findRecord(struct CdRecords cdDB[])
  156. {
  157. system("CLS");
  158. int i; //FIELD TO HOLD ARRAY COUNT FOR STRUCTURE
  159. char SearchTitle[20]; //FIELD TO HOLD ENTERED TITLE TO BE SEARCHED FOR
  160. int FoundRec; //FIELD TO INDICATE IF RECORD FOUND
  161. printf("Enter Title :"); //PROMPT USER TO ENTER TITLE
  162. scanf("%s", SearchTitle); //CAPTURE ENTERED TITLE
  163. FoundRec = 0;
  164. for(i = 0;i<datasize;i++){ //LOOP ROUND THE LIBRARY STRUCTURE CHECKING EACH TITLE
  165.  
  166. if((strcmp(SearchTitle,cdDB[i].Artist))==0){
  167.  
  168. printf("\n");
  169. printf("%s\n",cdDB[i].Artist);
  170. printf("%s\n",cdDB[i].Album);
  171. printf("%s\n",cdDB[i].Label);
  172. printf("%d\n",cdDB[i].Year);
  173. printf("%s\n",cdDB[i].Genre);
  174.  
  175. FoundRec = 1;
  176. }// END IF
  177. }//END FOR
  178.  
  179. if (FoundRec == 0)
  180. printf("Title Not Found \n"); //INFORM USER IF NO RECORD FOUND
  181.  
  182. printf("Hit Enter to Return to the Main Menu \n");
  183. fflush(stdin);
  184. getch();
  185.  
  186. }
  187.  
  188. //******************************************************************************************//
  189.  
  190. //FIND FREE SLOT TO ADD NEW ENTRY
  191.  
  192. int find_free(struct CdRecords cdDB[])
  193. {
  194. int t;
  195. char test; //FIELD TO CHECK FOR A VALUE IN TITLE
  196. for(t=0; t<MAX; t++){
  197. test = cdDB[t].Artist[0]; //STORE TITLE IN TEST FIELD
  198.  
  199. if (test=='\0'){ //IF TEST FIELD IS NULL THERE IS AN EMPTY SLOT
  200. return t; //RETURN THE SLOT NUMBER
  201. }
  202.  
  203. }
  204.  
  205. return -1; //IF NO FREE SLOTS FOUND RETURN -1
  206. }
  207.  
  208. //******************************************************************************************//
  209.  
  210. //DELETE THE SELECTED ITEM
  211.  
  212. void delRecord(struct CdRecords cdDB[])
  213. {
  214. int slot; //INTEGER TO HOLD SLOT NUMBER
  215. char s[80]; //FIELD TO HOLD INPUTTED RECORD NUMBER TO DELETE
  216. printf("Enter Record #: "); //PROMPT FOR RECORD NUMBER TO DELETE
  217. gets(s);
  218. slot = atoi(s); //CONVERT INPUT VALUE TO A SLOT NUMBER
  219.  
  220. if (slot>=0 && slot < MAX) //IF SLOT NUMBER IS SMALLER THAN MAX
  221. cdDB[slot].Artist[0] = '\0'; //CLEAR SELECTED STRUCTURE ENTRY
  222. }
  223.  
  224. //******************************************************************************************//
  225.  
  226. //DISPLAY THE LIST OF ENTRIES TO THE SCREEN
  227.  
  228. void listRecords(struct CdRecords cdDB[])
  229. {
  230. printf("\n\n LISTING LIBRARY FILE....\n");
  231. int t; //INTEGER TO CONTROL FOR LOOP
  232. for (t=0; t<MAX; ++t) {
  233. if (cdDB[t].Artist[0]) { //IF THERE IS DATA IN THE SELECTED TITLE
  234.  
  235. printf("%s\n",cdDB[t].Artist); //PRINT ALL DETAILS TO SCREEN
  236. printf("%s\n",cdDB[t].Album);
  237. printf("%s\n",cdDB[t].Label);
  238. printf("%d\n",cdDB[t].Year);
  239. printf("%s\n",cdDB[t].Genre);
  240. }
  241. }
  242. printf("\n\n");
  243. }
  244.  
  245. //******************************************************************************************//
  246.  
  247. //SAVE THE cdDB STRUCTURE DETAILS TO THE music FILE
  248.  
  249. void saveFile(struct CdRecords cdDB[])
  250. {
  251.  
  252. printf("\n\n SAVING FILE.......\n");
  253. FILE *fp; //DEFINE THE FILE POINTER
  254. int i;
  255. if ((fp=fopen("music.txt","wb"))==NULL){ //OPEN THE FILE - CAPTURE RESULT
  256. printf("Cannot Open File \n"); //AND DISPLAY MESSAGE TO USER IF
  257. return; //UNABLE TO LOCATE FILE
  258.  
  259. }
  260. for (i=0; i<MAX; i++) //LOOP ROUND CHECKING FOR A VALUE IN TITLE
  261. if (*cdDB[i].Artist) //AND SAVE ALL STRUCTURE DETAILS TO FILE
  262. //if (fwrite(&cdDB[i], //CHECK THAT WE ARE NOT EXCEEDING SIZE OF
  263. //sizeof(struct cdRecords),1,fp)!=1) //STRUCTURE
  264. //printf("File Write Error\n"); //IF YES DISPLAY MESSAGE TO USER
  265.  
  266. fclose(fp);
  267. }
  268.  
  269. //******************************************************************************************//
  270.  
  271.  
  272. //LOAD THE DETAILS OF THE LIBRARY FILE TO THE LIB STRUCTURE
  273.  
  274. void loadFile (struct CdRecords cdDB[])
  275.  
  276. {
  277. printf("\n\n LOADING FILE.......\n");
  278. FILE *fp; //DEFINE THE FILE POINTER
  279.  
  280. int i;
  281. if ((fp=fopen("music.txt","rb"))==NULL){ //OPEN THE FILE - IF NOT FOUND
  282. printf("Cannot Open File \n"); //DISPLAY MESSAGE TO USER
  283. return;
  284. }
  285. init_list(cdDB); //CLEAR STRUCTURE
  286. for (i=0; i<MAX; i++) //LOOP ROUND AND
  287. if (fread(&cdDB[i], //COPY FILE RECORDS TO THE STRUCTURE
  288. sizeof(struct CdRecords),1,fp)!=1){
  289. if (feof(fp)) break; //CHECK FOR ERROR IN READING FILE
  290. printf("File Read Error\n");
  291.  
  292. //******************************************************************************************//
  293.  
  294.  
  295. // SORT FILE BY TITLE USING BUBBLESORT METHOD
  296.  
  297. void sortRecords(struct cdRecords cdDB[])
  298. {
  299. system("CLS");
  300. int count = 0;
  301.  
  302. naive_sort (cdDB, datasize, & count);
  303.  
  304. //if(ch==3){
  305. //q_sort (cdDB, count);
  306. // }
  307.  
  308. printf("\nCritical count is : %d\n",count);
  309.  
  310. printf("Array has been sorted");
  311. printf("Press Enter To Continue");
  312. fflush(stdin);
  313. getch();
  314. }
  315.  
  316. // BUBBLESORT METHOD
  317.  
  318. void naive_sort (struct cdRecords array [], int arraySize, int * count)
  319. {
  320. for (int pass = 0; pass <= arraySize - 2; pass++)
  321. { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
  322. {
  323. *count = *count + 1; // count critical operations
  324. if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
  325. swap (&array[counter], &array[counter+1]);
  326. }
  327. }
  328. }
  329.  
  330. // SWAP A PAIR OF VALUES
  331. void swap (struct CdRecords * v1, struct cdRecords * v2)
  332. {
  333. struct CdRecords temp;
  334. temp = *v1;
  335. *v1 = *v2;
  336. *v2 = temp;
  337. }
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Dec 10th, 2005
1

Re: Bubble sort & File output jibrish errors???

if u are using getch(), it not #include <io.h>. Is #include<conio.h>. You forgot to define 1 of your function int find_free(struct CdRecords cdDB[]);.And i think your main problem with your program is struct cdRecords.
You define your struct as struct CdRecords but you wrote struct cdRecords as an arguement/parameter in your function call. This is the reason why "size of cdRecords is not known". I have change those errors and it can run. Try out and see whether it solve your problem.

  1. //(Database Management System(DBMS), user interface and report generator for results.
  2. //Also a diagnostic tool that allows access to the database metrics.
  3. //This is to show the efficiency of the sort algorithms used.
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <conio.h>
  9.  
  10. #define MAX 10
  11.  
  12. //******************************************************************************************//
  13.  
  14. //Structure
  15.  
  16. struct CdRecords
  17. {
  18. char Artist[20];
  19. char Album[20];
  20. char Year[8];
  21. char Label[20];
  22. char Genre[20];
  23. };
  24.  
  25. //******************************************************************************************//
  26. // FUNCTIONS
  27. void addRecord(struct CdRecords cdDB[]);
  28. void listRecords(struct CdRecords cdDB[]);
  29. void delRecord(struct CdRecords cdDB[]);
  30. void init_list(struct CdRecords cdDB[]);
  31. int menu_select(void);
  32. void loadFile (struct CdRecords cdDB[]);
  33. void saveFile(struct CdRecords cdDB[]);
  34. void SearchTitle(struct CdRecords cdDB[]);
  35. void findRecord(struct CdRecords cdDB[]);
  36.  
  37. void sortRecords(struct CdRecords cdDB[]);
  38. void naive_sort (struct CdRecords cdDB[], int arraySize, int * count);
  39. void swap (struct CdRecords * v1, struct CdRecords * v2);
  40. int find_free(struct CdRecords cdDB[]);
  41.  
  42. const int datasize = 20;
  43.  
  44. //******************************************************************************************//
  45.  
  46. //MAIN FUNCTION
  47.  
  48. void main(void)
  49. {
  50. struct CdRecords cdDB[MAX];
  51.  
  52. char choice; //TO CAPTURE THE MENU SELECTION
  53.  
  54. init_list(cdDB); //CLEAR THE STRUCTURE
  55.  
  56. for (;;) { //LOOP AROUND MENU SELECTION UNTIL USER EXITS
  57.  
  58. choice =menu_select(); // CALL MENU_SELECT FUNCTION AND RETURN VALUE IN FIELD - CHOICE
  59. switch(choice){
  60. case 1:addRecord(cdDB);
  61. break;
  62. case 2:delRecord(cdDB);
  63. break;
  64. case 3:listRecords(cdDB);
  65. break;
  66. case 4:saveFile(cdDB);
  67. break;
  68. case 5:loadFile(cdDB);
  69. break;
  70. case 6:findRecord(cdDB);
  71. break;
  72. case 7:sortRecords(cdDB);
  73. break;
  74. case 8:exit(0);
  75. break;
  76. }//END SWITCH
  77. }//END FOR
  78.  
  79. }
  80. //******************************************************************************************//
  81.  
  82. //INITIALISE THE STRUCTURE
  83.  
  84. void init_list(struct CdRecords cdDB[])
  85. {
  86. int t; //TO CONTROL LOOP FOR CLEARING STRUCTURE
  87. for (t=0;t<MAX; t++)
  88. cdDB[t].Artist[0] = '\0'; //SET THE TITLE TO NULL
  89. }
  90. //******************************************************************************************//
  91.  
  92. //DISPLAY MENU OPTIONS
  93.  
  94. menu_select(void)
  95.  
  96. {char s [80];
  97. int c;
  98.  
  99. printf("*** MAIN MENU ***\n");
  100.  
  101. printf("1.Enter\n"); //DISPLAY MENU OPTIONS TO USER
  102. printf("2.Delete\n");
  103. printf("3.List\n");
  104. printf("4.Save File\n");
  105. printf("5.Load File\n");
  106. printf("6.Search\n");
  107. printf("7.Sort\n");
  108. printf("8.Exit\n");
  109.  
  110. do{
  111. printf("Enter choice ");
  112. gets(s);
  113. c = atoi(s);
  114.  
  115. }while(c<0 ||c> 8); // EXIT APPLICATION WHEN USER SELECTS 7
  116. return c;
  117. }
  118.  
  119. //******************************************************************************************//
  120.  
  121. // ADD NEW ENTRIES TO THE FILE
  122.  
  123. void addRecord(struct CdRecords cdDB[])
  124. {
  125. int slot; // INTEGER TO LOCATE FREE SPACE IN STRUCTURE
  126. //char s[80]; // CALL THE FIND_FREE FUNCTION AND RETURN VALUE IN FIELD - SLOT
  127. slot = find_free(cdDB);
  128.  
  129. if (slot==-1){ //IF SLOT = -1 THEN THE STRUCTURE IS FULL
  130. printf("LIst Full");
  131. return;
  132. }
  133.  
  134. printf("Enter Artist: "); //PROMPT TO ADD TITLE
  135. gets(cdDB[slot].Artist);
  136.  
  137. printf("Enter Album: "); //PROMPT TO ADD AUTHOR
  138. gets(cdDB[slot].Album);
  139.  
  140. printf("Enter Label: "); //PROMPT TO ADD PUBLISHER
  141. gets(cdDB[slot].Label);
  142.  
  143. printf("Enter Year: "); //PROMPT TO ADD YEAR
  144. gets(cdDB[slot].Year);
  145.  
  146. printf("Enter Genre: "); //PROMPT TO ADD ISBN
  147. gets(cdDB[slot].Genre);
  148.  
  149.  
  150.  
  151. }
  152.  
  153. //******************************************************************************************//
  154.  
  155. //SEARCH FOR A TITLE
  156.  
  157. void findRecord(struct CdRecords cdDB[])
  158. {
  159. system("CLS");
  160. int i; //FIELD TO HOLD ARRAY COUNT FOR STRUCTURE
  161. char SearchTitle[20]; //FIELD TO HOLD ENTERED TITLE TO BE SEARCHED FOR
  162. int FoundRec; //FIELD TO INDICATE IF RECORD FOUND
  163. printf("Enter Title :"); //PROMPT USER TO ENTER TITLE
  164. scanf("%s", SearchTitle); //CAPTURE ENTERED TITLE
  165. FoundRec = 0;
  166. for(i = 0;i<datasize;i++){ //LOOP ROUND THE LIBRARY STRUCTURE CHECKING EACH TITLE
  167.  
  168. if((strcmp(SearchTitle,cdDB[i].Artist))==0){
  169.  
  170. printf("\n");
  171. printf("%s\n",cdDB[i].Artist);
  172. printf("%s\n",cdDB[i].Album);
  173. printf("%s\n",cdDB[i].Label);
  174. printf("%d\n",cdDB[i].Year);
  175. printf("%s\n",cdDB[i].Genre);
  176.  
  177. FoundRec = 1;
  178. }// END IF
  179. }//END FOR
  180.  
  181. if (FoundRec == 0)
  182. printf("Title Not Found \n"); //INFORM USER IF NO RECORD FOUND
  183.  
  184. printf("Hit Enter to Return to the Main Menu \n");
  185. fflush(stdin);
  186. getch();
  187.  
  188. }
  189.  
  190. //******************************************************************************************//
  191.  
  192. //FIND FREE SLOT TO ADD NEW ENTRY
  193.  
  194. int find_free(struct CdRecords cdDB[])
  195. {
  196. int t;
  197. char test; //FIELD TO CHECK FOR A VALUE IN TITLE
  198. for(t=0; t<MAX; t++){
  199. test = cdDB[t].Artist[0]; //STORE TITLE IN TEST FIELD
  200.  
  201. if (test=='\0'){ //IF TEST FIELD IS NULL THERE IS AN EMPTY SLOT
  202. return t; //RETURN THE SLOT NUMBER
  203. }
  204.  
  205. }
  206.  
  207. return -1; //IF NO FREE SLOTS FOUND RETURN -1
  208. }
  209.  
  210. //******************************************************************************************//
  211.  
  212. //DELETE THE SELECTED ITEM
  213.  
  214. void delRecord(struct CdRecords cdDB[])
  215. {
  216. int slot; //INTEGER TO HOLD SLOT NUMBER
  217. char s[80]; //FIELD TO HOLD INPUTTED RECORD NUMBER TO DELETE
  218. printf("Enter Record #: "); //PROMPT FOR RECORD NUMBER TO DELETE
  219. gets(s);
  220. slot = atoi(s); //CONVERT INPUT VALUE TO A SLOT NUMBER
  221.  
  222. if (slot>=0 && slot < MAX) //IF SLOT NUMBER IS SMALLER THAN MAX
  223. cdDB[slot].Artist[0] = '\0'; //CLEAR SELECTED STRUCTURE ENTRY
  224. }
  225.  
  226. //******************************************************************************************//
  227.  
  228. //DISPLAY THE LIST OF ENTRIES TO THE SCREEN
  229.  
  230. void listRecords(struct CdRecords cdDB[])
  231. {
  232. printf("\n\n LISTING LIBRARY FILE....\n");
  233. int t; //INTEGER TO CONTROL FOR LOOP
  234. for (t=0; t<MAX; ++t) {
  235. if (cdDB[t].Artist[0]) { //IF THERE IS DATA IN THE SELECTED TITLE
  236.  
  237. printf("%s\n",cdDB[t].Artist); //PRINT ALL DETAILS TO SCREEN
  238. printf("%s\n",cdDB[t].Album);
  239. printf("%s\n",cdDB[t].Label);
  240. printf("%d\n",cdDB[t].Year);
  241. printf("%s\n",cdDB[t].Genre);
  242. }
  243. }
  244. printf("\n\n");
  245. }
  246.  
  247. //******************************************************************************************//
  248.  
  249. //SAVE THE cdDB STRUCTURE DETAILS TO THE music FILE
  250.  
  251. void saveFile(struct CdRecords cdDB[])
  252. {
  253.  
  254. printf("\n\n SAVING FILE.......\n");
  255. FILE *fp; //DEFINE THE FILE POINTER
  256. int i;
  257. if ((fp=fopen("music.txt","wb"))==NULL){ //OPEN THE FILE - CAPTURE RESULT
  258. printf("Cannot Open File \n"); //AND DISPLAY MESSAGE TO USER IF
  259. return; //UNABLE TO LOCATE FILE
  260.  
  261. }
  262. for (i=0; i<MAX; i++) //LOOP ROUND CHECKING FOR A VALUE IN TITLE
  263. if (*cdDB[i].Artist) //AND SAVE ALL STRUCTURE DETAILS TO FILE
  264. //if (fwrite(&cdDB[i], //CHECK THAT WE ARE NOT EXCEEDING SIZE OF
  265. //sizeof(struct CdRecords),1,fp)!=1) //STRUCTURE
  266. //printf("File Write Error\n"); //IF YES DISPLAY MESSAGE TO USER
  267.  
  268. fclose(fp);
  269. }
  270.  
  271. //******************************************************************************************//
  272.  
  273.  
  274. //LOAD THE DETAILS OF THE LIBRARY FILE TO THE LIB STRUCTURE
  275.  
  276. void loadFile (struct CdRecords cdDB[])
  277.  
  278. {
  279. printf("\n\n LOADING FILE.......\n");
  280. FILE *fp; //DEFINE THE FILE POINTER
  281.  
  282. int i;
  283. if ((fp=fopen("music.txt","rb"))==NULL){ //OPEN THE FILE - IF NOT FOUND
  284. printf("Cannot Open File \n"); //DISPLAY MESSAGE TO USER
  285. return;
  286. }
  287. init_list(cdDB); //CLEAR STRUCTURE
  288. for (i=0; i<MAX; i++) //LOOP ROUND AND
  289. if (fread(&cdDB[i], //COPY FILE RECORDS TO THE STRUCTURE
  290. sizeof(struct CdRecords),1,fp)!=1){
  291. if (feof(fp)) break; //CHECK FOR ERROR IN READING FILE
  292. printf("File Read Error\n");
  293. }
  294.  
  295. }
  296. //******************************************************************************************//
  297.  
  298.  
  299. // SORT FILE BY TITLE USING BUBBLESORT METHOD
  300.  
  301. void sortRecords(struct CdRecords cdDB[])
  302. {
  303. system("CLS");
  304. int count = 0;
  305.  
  306. naive_sort (cdDB, datasize, & count);
  307.  
  308. //if(ch==3){
  309. //q_sort (cdDB, count);
  310. // }
  311.  
  312. printf("\nCritical count is : %d\n",count);
  313.  
  314. printf("Array has been sorted");
  315. printf("Press Enter To Continue");
  316. fflush(stdin);
  317. getch();
  318. }
  319.  
  320. // BUBBLESORT METHOD
  321.  
  322. void naive_sort (struct CdRecords array [], int arraySize, int * count)
  323. {
  324. for (int pass = 0; pass <= arraySize - 2; pass++)
  325. { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
  326. {
  327. *count = *count + 1; // count critical operations
  328. if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
  329. swap (&array[counter], &array[counter+1]);
  330. }
  331. }
  332. }
  333.  
  334. // SWAP A PAIR OF VALUES
  335. void swap (struct CdRecords * v1, struct CdRecords * v2)
  336. {
  337. struct CdRecords temp;
  338. temp = *v1;
  339. *v1 = *v2;
  340. *v2 = temp;
  341. }
Reputation Points: 13
Solved Threads: 0
Light Poster
k_en is offline Offline
35 posts
since Jul 2005
Dec 10th, 2005
0

Re: Bubble sort & File output jibrish errors???

dude thanx for the help.... but yesterday i did notice those problems and i changed the layout of my program and im not getting ne problems..thanks to Ancient Dragon and the rest of u guys here...
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005

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: pseodo code challenge <event driven programming>
Next Thread in C Forum Timeline: dynamic allocation of 2d array





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


Follow us on Twitter


© 2011 DaniWeb® LLC