944,221 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1597
  • C RSS
Dec 11th, 2005
0

data sorts in reverse order!! help!!

Expand Post »
i got the quicksort coding correct ,, the problem is that the data is still not sorting below is the complete program to the database.
The data is to be sorted by genre like i have stated i have checked over and over again but cant find nething wrong...

The indirect method the Ancient Dragon showed me,, is kind of confusing and i am unable to incorporate that into this program..
apreciate everythin and nething.




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

Re: data sorts in reverse order!! help!!

hello

sorry ppl i been making mistakes in my posts ,, i was thinking why ppl aint answer to this one,,..

i actually needed help in making my quick sort and delete records function work... ne one got ideas on whats wrong ....
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: i can't figure out this code :( help please
Next Thread in C Forum Timeline: Struct in Functions





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


Follow us on Twitter


© 2011 DaniWeb® LLC