Why is qsort not initialised here!!??

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Why is qsort not initialised here!!??

 
0
  #1
Dec 9th, 2005
I got everything working, a couple of probls with file write. But ill work it out. I just need help on getting the quick sort working here...i got it written and even in my switch statement but for some reason it dont work....ne one know y...??


  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. #include <conio.h>
  10. /******************************************************************************************/
  11.  
  12. //Structure
  13.  
  14. struct CdRecords
  15. {
  16. char Artist[20];
  17. char Album[20];
  18. char Year[8];
  19. char Label[20];
  20. char Genre[20];
  21. };
  22. /******************************************************************************************/
  23.  
  24. void naive_sort (struct CdRecords array [], int arraySize, int * count);
  25. void swap (struct CdRecords * v1, struct CdRecords * v2);
  26.  
  27. void write_records(struct CdRecords cdDB[]);
  28. void write_file(struct CdRecords cdDB[]);
  29. void read_file(struct CdRecords cdDB[]);
  30. void delAlbum(struct CdRecords cdDB[]);
  31.  
  32. void sort_critical_count(struct CdRecords cdDB[], int choice);
  33. void record_search(struct CdRecords cdDB[]);
  34.  
  35. void quick_sort (struct CdRecords array [], int left, int right);
  36. void q_sort (struct CdRecords array [], int count);
  37.  
  38. void init_list(struct CdRecords cdDB[]);
  39. int find_free(struct CdRecords cdDB[]);
  40.  
  41.  
  42.  
  43. const int datasize = 20;
  44. // file pointer
  45. FILE * fp;
  46.  
  47.  
  48. /******************************************************************************************/
  49. void init_list(struct CdRecords cdDB [])
  50. {
  51. int i; //TO CONTROL LOOP FOR CLEARING STRUCTURE
  52. for (i=0;i<datasize; i++)
  53. cdDB[i].Artist[0] = '\0'; //SET THE TITLE TO NULL
  54. }
  55. /******************************************************************************************/
  56. //FIND FREE SLOT TO ADD NEW ENTRY
  57.  
  58. int find_free(struct CdRecords cdDB[])
  59. {
  60. int i;
  61. char test; //FIELD TO CHECK FOR A VALUE IN TITLE
  62. for(i=0; i<datasize; i++){
  63. test = cdDB[i].Album[0]; //STORE TITLE IN TEST FIELD
  64.  
  65. if (test=='\0'){ //IF TEST FIELD IS NULL THERE IS AN EMPTY SLOT
  66. return i; //RETURN THE SLOT NUMBER
  67. }
  68.  
  69. }
  70.  
  71. return -1; //IF NO FREE SLOTS FOUND RETURN -1
  72. }
  73. /******************************************************************************************/
  74.  
  75. void sort_critical_count(struct CdRecords cdDB[],int ch)
  76. {
  77. system("CLS");
  78. int count = 0;
  79.  
  80. naive_sort (cdDB, datasize, & count);
  81.  
  82. //if(ch==3){
  83. //q_sort (cdDB, count);
  84. // }
  85. if(ch == 5)
  86. printf("\nCritical count is : %d\n",count);
  87. else
  88. printf("Array has been sorted");
  89. printf("Press Enter To Continue");
  90. fflush(stdin);
  91. getch();
  92. }
  93.  
  94. /******************************************************************************************/
  95.  
  96. void write_records(struct CdRecords cdDB[])
  97. {
  98. system("CLS");
  99. int i;
  100. char ch;
  101. ch=1;
  102. i=0;
  103. int slot; // INTEGER TO LOCATE FREE SPACE IN STRUCTURE
  104. char s[80]; // CALL THE FIND_FREE FUNCTION AND RETURN VALUE IN FIELD - SLOT
  105. slot = find_free(cdDB);
  106.  
  107. if (slot==-1){ //IF SLOT = -1 THEN THE STRUCTURE IS FULL
  108. printf("Database Full");
  109. return;
  110. }
  111. //input Data from the keyboard
  112. //go through record
  113. //write to disk using formatted output
  114. // for (i=0;i<datasize;i++)
  115.  
  116. // {
  117.  
  118.  
  119. printf("Enter the Artist: \n");
  120. //scanf("%s",cdDB[i].Artist);
  121. gets(cdDB[slot].Artist);
  122. gets(cdDB[slot].Artist);
  123. printf("Enter the Album: \n");
  124. //scanf("%s",cdDB[i].Album);
  125. gets(cdDB[slot].Album);
  126. printf("Enter Label name: \n");
  127. //scanf("%s",cdDB[i].Label);
  128. gets(cdDB[slot].Label);
  129. printf("Enter Year: \n");
  130. //scanf("%d",&cdDB[i].Year);
  131. gets(cdDB[slot].Year);
  132. printf("Enter the Genre of music: \n");
  133. //scanf("%s",cdDB[i].Genre);
  134. gets(cdDB[slot].Genre);
  135. // printf("Enter 1 to continue or 0 To Finish: \n");
  136. // scanf("%d",&ch);
  137. //if(ch==0)
  138. //{
  139. //break;
  140. //}
  141. // }
  142. }
  143.  
  144. /******************************************************************************************/
  145. void delAlbum(struct CdRecords cdDB[])
  146. {
  147. int slot; //INTEGER TO HOLD SLOT NUMBER
  148. char s[80]; //FIELD TO HOLD INPUTTED RECORD NUMBER TO DELETE
  149. printf("Enter Album: "); //PROMPT FOR Album TO DELETE
  150. gets(s);
  151. slot = atoi(s); //CONVERT INPUT VALUE TO A SLOT NUMBER
  152.  
  153. if (slot>=0 && slot < datasize)
  154. cdDB[slot].Artist[0] = '\0'; //CLEAR SELECTED STRUCTURE ENTRY
  155. }
  156. /******************************************************************************************/
  157. void record_search(struct CdRecords cdDB[])
  158. {
  159. system("CLS");
  160. int i;
  161. char name[20];
  162.  
  163. printf("Enter Artist name :");
  164. scanf("%s", name);
  165. for(i = 0;i<datasize;i++)
  166. {
  167. if((strcmp(name,cdDB[i].Artist))==0)
  168. {
  169. printf("\n");
  170. printf("%s\n",cdDB[i].Artist);
  171. printf("%s\n",cdDB[i].Album);
  172. printf("%s\n",cdDB[i].Label);
  173. printf("%s\n",cdDB[i].Year);
  174. printf("%s\n",cdDB[i].Genre);
  175.  
  176. }
  177. }
  178. printf("Press Enter To Continue");
  179. fflush(stdin);
  180. getch();
  181.  
  182. }
  183.  
  184. /******************************************************************************************/
  185.  
  186. void write_file(struct CdRecords cdDB[])
  187. {
  188. system("CLS");
  189. int i;
  190. fp=fopen("CD-Records.txt","w");
  191.  
  192. for (i=0;i<datasize;i++)
  193. {
  194. fprintf(fp, "%s %s %s %s %s\n",cdDB[i].Artist,cdDB[i].Album,cdDB[i].Label,cdDB[i].Year,cdDB[i].Genre);
  195. }
  196.  
  197. printf("Data has been written to file");
  198. fflush(stdin);
  199. getch();
  200. fclose(fp);//Close the file
  201.  
  202. }
  203.  
  204. /******************************************************************************************/
  205.  
  206. void read_file(struct CdRecords cdDB[])
  207. {
  208. system("CLS");
  209. int j;
  210. fp = fopen("CD-Records.txt","r");
  211. //Read the data that was written
  212. printf("reading data...\n");
  213.  
  214. for (j=0; j<datasize; j++)
  215. {
  216. if (cdDB[j].Artist[0]){
  217. fscanf(fp,"%s %s %s %s %s",cdDB[j].Artist, cdDB[j].Album, cdDB[j].Label, &cdDB[j].Year, cdDB[j].Genre);
  218. printf("Record %d\n",j+1," is...\n");
  219. printf("%s\n",cdDB[j].Artist);
  220. printf("%s\n",cdDB[j].Album);
  221. printf("%s\n",cdDB[j].Label);
  222. printf("%s\n",cdDB[j].Year);
  223. printf("%s\n",cdDB[j].Genre);
  224. //printf("\n\n");
  225. }
  226. printf("\n\n");
  227. }
  228.  
  229. fclose(fp);
  230. printf("Press Enter To Continue");
  231. fflush(stdin);
  232. getch();
  233. // close file
  234. }
  235.  
  236. /******************************************************************************************/
  237.  
  238. // Sort an array of integers with inefficient version of bubblesort
  239.  
  240. void naive_sort (struct CdRecords array [], int arraySize, int * count)
  241. {
  242. for (int pass = 0; pass <= arraySize - 2; pass++)
  243. { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
  244. {
  245. *count = *count + 1; // count critical operations
  246. if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
  247. swap (&array[counter], &array[counter+1]);
  248. }
  249. }
  250. }
  251.  
  252. // Exchange a given pair of values in an array
  253. void swap (struct CdRecords * v1, struct CdRecords * v2)
  254. {
  255. struct CdRecords temp;
  256. temp = *v1;
  257. *v1 = *v2;
  258. *v2 = temp;
  259. }
  260.  
  261. /******************************************************************************************/
  262. void q_sort (struct CdRecords array [], int count)
  263. {
  264. quick_sort(array,0,count-1);
  265. }
  266. void quick_sort (struct CdRecords array [], int left, int right)
  267. {
  268. int i, j;
  269. char *x;
  270. struct CdRecords temp;
  271.  
  272. i = left;
  273. j = right;
  274.  
  275. x = array[(left+right)/2].Genre;
  276.  
  277. do {
  278. while(strcmp(array[i].Genre,x)<0 && i<right) i++;
  279. while(strcmp(array[i].Genre,x)>0 && j>left) j--;
  280. if(i<=j) {
  281. temp = array[i];
  282. array[i] = array[j];
  283. array[j] = temp;
  284. i++; j--;
  285.  
  286. }
  287.  
  288. }while(i<=j);
  289.  
  290. if(left<j) quick_sort(array, left, j);
  291. if(i<right)quick_sort(array, i, right);
  292. }
  293. /******************************************************************************************/
  294.  
  295. int main()
  296. {
  297. system("CLS");
  298. int ch = 0;
  299.  
  300. struct CdRecords cdDB[datasize];
  301. init_list(cdDB); //CLEAR THE STRUCTURE
  302. do{
  303. system("CLS");
  304. printf("MAIN MENU\n");
  305. printf("Press 1 To Enter Records\n");
  306. printf("Press 2 To Bubble Sort Records\n");
  307. printf("Press 3 To Quick Sort Records\n");
  308. printf("Press 4 To Save Records To File\n");
  309. printf("Press 5 To Read Records From File\n");
  310. printf("Press 6 To Show Diagnostic Details\n");
  311. printf("Press 7 To Search For A Record\n");
  312. printf("Press 8 To Delete A Record\n");
  313. printf("Press 9 To Quit\n");
  314. printf("Enter Your Choice : ");
  315. scanf("%d",&ch);
  316.  
  317. switch (ch)
  318. {
  319.  
  320. case 1:
  321. write_records(cdDB);
  322. break;
  323. case 2:
  324. sort_critical_count(cdDB,2);
  325. break;
  326. case 3:
  327. quick_sort(cdDB,1,2);
  328. break;
  329. case 4:
  330. write_file(cdDB);
  331. break;
  332. case 5:
  333. read_file(cdDB);
  334. break;
  335. case 6:
  336. sort_critical_count(cdDB,5);
  337. break;
  338. case 7:
  339. record_search(cdDB);
  340. break;
  341. case 8:
  342. delAlbum(cdDB);
  343. break;
  344. case 9:
  345. break;
  346. default:
  347. break;
  348. }
  349.  
  350. } while(ch !=8);
  351. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why is qsort not initialised here!!??

 
0
  #2
Dec 9th, 2005
Originally Posted by nabil1983
I got everything working, a couple of probls with file write. But ill work it out. I just need help on getting the quick sort working here...i got it written and even in my switch statement but for some reason it dont work....ne one know y...??
what "doesn't work" -- compiler errors? if yes, what are they. runtime errors? describe them.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Why is qsort not initialised here!!??

 
0
  #3
Dec 9th, 2005
For the switch, print something out in the default (for example, print out the value of ch to see what value was passed...)
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Why is qsort not initialised here!!??

 
0
  #4
Dec 9th, 2005
that the thing im not getting no compiler errors. When in the program i press the option to do a quick sort, nothing happens, its just prompts for another option.
?? dont know y
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: Why is qsort not initialised here!!??

 
0
  #5
Dec 9th, 2005
again, print out ch inside the default: of the switch.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Why is qsort not initialised here!!??

 
0
  #6
Dec 9th, 2005
i've done wat u said and it still did not sort the data but the problem now also is that when i goto
display the data on screen i get all jibrish data entries???
and the search aint working either,, im sure i've done the coding right for it though!!
Last edited by nabil1983; Dec 9th, 2005 at 3:06 pm. Reason: Forgot To Add something
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why is qsort not initialised here!!??

 
0
  #7
Dec 9th, 2005
1. function init_list is not clearing all the fields in the structure. There is a very easy and quick way to do that with only one programming line and no loops
  1. void init_list(struct CdRecords cdDB [])
  2. {
  3. memset(cdDB,0,datasize*sizeof(CdRecords));
  4. }


2. The reason you see gibberish after sorting the array is because the sort alborithms attempt to sort all rows of the CdRecords, even though the array may contain just a few valid rows, however many you type in. function find_free() gets the row number of the next unused row, so all you have to do is pass that to the sort algorithm and have it sort only that many rows.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Why is qsort not initialised here!!??

 
0
  #8
Dec 9th, 2005
when i use the one code line , its giving acompile error "Undefined Identifier CdRecords"

Also might sound dumb and crazy but how do i pass the find_free() to the sort algorithm and use it to only sort the rows....???

you probably think its crazy but im still at a learning stage...

Apreciate all the help u've already given me...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why is qsort not initialised here!!??

 
0
  #9
Dec 9th, 2005
1. check the spelling -- just copy that line that I posted and past it into your program.

2. Just do it like this. you don't need to change the sort function at all.
void sort_critical_count(struct CdRecords cdDB[],int ch)
{
     system("CLS");
	 int count = 0;
 int nRows = find_free(cdDB);
     naive_sort (cdDB,  nRows, & count);

At the end of main() change the do-while loop to exit when ch == 9 instead of 8, so that you can exit the menu.
  		} while(ch !=9);
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 73
Reputation: nabil1983 is an unknown quantity at this point 
Solved Threads: 0
nabil1983 nabil1983 is offline Offline
Junior Poster in Training

Re: Why is qsort not initialised here!!??

 
0
  #10
Dec 9th, 2005
spelling is correct im still getting the same problem::!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC