Finally Almost Finished 2 Problems!! :(

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

Finally Almost Finished 2 Problems!! :(

 
0
  #1
Dec 8th, 2005
Ok i finished off the program i was making. now i just got a couple problems icant work out....spent hours doing this please help me with my last few probs...

First thing is when i write the records to file, and then when i try to sort it...the display shows all the records twice instead of just the sorted records.
As you can see i managed to make the bubble sort work with the critical count but i got help on that.
I added the code for a quick sort as well,, can some show me how i can implement this into this program.
What i want is to give the user a choice to sort using bubble sort for Artist and Quicksort for Genre.

Please i will really apreciate if anyone can help me solve these two problems...



  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.  
  9. #define MAX 20
  10. //Structure
  11.  
  12. struct CdRecords
  13. {
  14. char Artist[50];
  15. char Album[50];
  16. char Year[10];
  17. char Label[50];
  18. char Genre[50];
  19. }cdDB[MAX];
  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. void write_records(struct CdRecords cdDB[]);
  27. void write_file(struct CdRecords cdDB[]);
  28. void read_file(struct CdRecords cdDB[]);
  29. void sort_critical_count(struct CdRecords cdDB[], int choice);
  30.  
  31. void q_sort (struct CdRecords array [], int count);
  32. void quick_sort (struct CdRecords array [], int left, int right);
  33.  
  34.  
  35. // file pointer
  36. FILE * fp;
  37.  
  38. /******************************************************************************************/
  39. void sort_critical_count(struct CdRecords cdDB[],int choice)
  40. {
  41.  
  42. system("CLS");
  43. int count = 0;
  44. naive_sort (cdDB, MAX, & count);
  45. if(choice == 5)
  46. printf("\nCritical count is : %d\n",count);
  47. else
  48. printf("Array has been sorted");
  49. printf("Press Enter To Continue");
  50. fflush(stdin);
  51. getch();
  52. }
  53. /******************************************************************************************/
  54. void write_records(struct CdRecords cdDB[])
  55. {
  56. system("CLS");
  57. int i,ch;
  58.  
  59.  
  60. //input Data from the keyboard
  61. //go through record
  62. //write to disk using formatted output
  63. for (i=0;i<MAX;i++)
  64.  
  65. {
  66. printf("Enter the Artist: \n");
  67. scanf("%s",cdDB[i].Artist);
  68. printf("Enter the Album: \n");
  69. scanf("%s",cdDB[i].Album);
  70. printf("Enter Label name: \n");
  71. scanf("%s",cdDB[i].Label);
  72. printf("Enter Year: \n");
  73. scanf("%s",&cdDB[i].Year);
  74. printf("Enter the Genre of music: \n");
  75. scanf("%s",cdDB[i].Genre);
  76. printf("Enter 1 to continue or 0 To Finish: \n");
  77. scanf("%d",&ch);
  78. if (ch==0){
  79. break;
  80. }
  81. }
  82. }
  83. /******************************************************************************************/
  84.  
  85. /******************************************************************************************/
  86. void write_file(struct CdRecords cdDB[])
  87. {
  88. system("CLS");
  89. int i;
  90. fp=fopen("CD-Records.txt","w");
  91.  
  92. for (i=0;i<MAX;i++)
  93. {
  94. fprintf(fp, "%s %s %s %s %s\n",cdDB[i].Artist, cdDB[i].Album, cdDB[i].Label,cdDB[i].Year,cdDB[i].Genre);
  95. //Close the file
  96. }
  97. printf("Data has been written to file");
  98. fflush(stdin);
  99. getch();
  100. fclose(fp);
  101. }
  102.  
  103. /***********************************/
  104.  
  105. /******************************************************************************************/
  106. void read_file(struct CdRecords cdDB[])
  107. {
  108. system("CLS");
  109. int j;
  110. fp = fopen("CD-Records.txt","r+");
  111. //Read the data that was written
  112. printf("reading data...\n");
  113.  
  114. for (j=0; j<MAX; j++)
  115. {
  116.  
  117. fscanf(fp,"%s %s %s %s %s",cdDB[j].Artist, cdDB[j].Album, cdDB[j].Label, &cdDB[j].Year, cdDB[j].Genre);
  118. printf("Record %d\n",j+1," is...\n");
  119. printf("%s\n",cdDB[j].Artist);
  120. printf("%s\n",cdDB[j].Album);
  121. printf("%s\n",cdDB[j].Label);
  122. printf("%s\n",cdDB[j].Year);
  123. printf("%s\n",cdDB[j].Genre);
  124. printf("\n");
  125.  
  126. }
  127. fclose(fp);
  128. printf("Press Enter To Continue");
  129. fflush(stdin);
  130. getch();
  131. // close file
  132. }
  133. /******************************************************************************************/
  134. // Sort an array of integers with inefficient version of bubblesort
  135.  
  136. void naive_sort (struct CdRecords array [], int arraySize, int * count)
  137. {
  138. for (int pass = 0; pass <= arraySize - 2; pass++)
  139. { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
  140. {
  141. *count = *count + 1; // count critical operations
  142. if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
  143. swap (&array[counter], &array[counter+1]);
  144. }
  145. }
  146. }
  147. /******************************************************************************************/
  148. // Exchange a given pair of values in an array
  149. void swap (struct CdRecords * v1, struct CdRecords * v2)
  150. {
  151. struct CdRecords temp;
  152. temp = *v1;
  153. *v1 = *v2;
  154. *v2 = temp;
  155. }
  156. /******************************************************************************************/
  157.  
  158. /******************************************************************************************/
  159. void q_sort (struct CdRecords array [], int count)
  160. {
  161. quick_sort(array,0,count-1);
  162. }
  163. void quick_sort (struct CdRecords array [], int left, int right)
  164. {
  165. int i, j;
  166. char *x;
  167. struct CdRecords temp;
  168.  
  169. i = left;
  170. j = right;
  171.  
  172. x = array[(left+right)/2].Genre;
  173.  
  174. do {
  175. while(strcmp(array[i].Genre,x)<0 && i<right) i++;
  176. while(strcmp(array[i].Genre,x)>0 && j>left) j--;
  177. if(i<=j) {
  178. temp = array[i];
  179. array[i] = array[j];
  180. array[j] = temp;
  181. i++; j--;
  182.  
  183. }
  184.  
  185. }while(i<=j);
  186.  
  187. if(left<j) quick_sort(array, left, j);
  188. if(i<right)quick_sort(array, i, right);
  189. }
  190. /******************************************************************************************/
  191. int main()
  192. {
  193. int ch ;
  194.  
  195. // struct CdRecords cdDB[i];
  196.  
  197.  
  198. system("CLS");
  199.  
  200. do {
  201.  
  202. printf("MAIN MENU\n");
  203. printf("Press 1 To Enter Records\n");
  204. printf("Press 2 To Sort Records\n");
  205. printf("Press 3 To Save Records To File\n");
  206. printf("Press 4 To Read Records From File\n");
  207. printf("Press 5 To Show Diagnostic Details\n");
  208. printf("Press 6 To Search For A Record\n");
  209. printf("Press 7 To Quit\n");
  210. printf("Enter Your Choice : ");
  211. scanf("%d",&ch);
  212.  
  213. switch (ch)
  214. {
  215.  
  216. case 1:
  217. write_records(cdDB);
  218. break;
  219. case 2:
  220. sort_critical_count(cdDB,2);
  221. break;
  222. case 3:
  223. write_file(cdDB);
  224. break;
  225. case 4:
  226. read_file(cdDB);
  227. break;
  228. case 5:
  229. sort_critical_count(cdDB,5);
  230. break;
  231. // case 6:
  232. // record_search(cdDB);
  233. // break;
  234. case 7:
  235. break;
  236. default:
  237. break;
  238. }
  239.  
  240. } while(ch !=7);
  241. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Finally Almost Finished 2 Problems!! :(

 
0
  #2
Dec 8th, 2005
  1. fflush(stdin); Do not use that. (Nor gets.) Use this:
    1. int c;
    2. while((c = getchar()) != '\n' && c != EOF);
  2. getch() is nonstandard and you shouldn't use it; but if you must, it's in <conio.h>.
  3. scanf("%s",cdDB[i].Artist); This is a bad way to read in a string (although, admittedly, better than gets()). You should use fgets() instead.
  4. main() should return a value, like 0 (although it doesn't have to).
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
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: still problem

 
0
  #3
Dec 8th, 2005
Ok try to compile and run the program.
When i enter a cdrecord instead of asking me to continue or finish, the program automatically asks for next cd record data.
Can u please show what i need to modify in order to make it run as i want it to.
Also take alook at the quicksort function even though i call it in the program it dont work .

Can someone show what i need to modify,,, only thing is i cant make any big changes cuz im running out of time..



  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAXLINE 80
  5.  
  6.  
  7.  
  8. struct Music
  9. {
  10. char Artist[20];
  11. char Album[20];
  12. char Year[4];
  13. char Label[20];
  14. char Genre[20];
  15. };
  16.  
  17.  
  18.  
  19. void getline(char *line)
  20. {
  21. int c, i;
  22.  
  23. for ( i=0;i<MAXLINE-1 && ( c=getchar()) != EOF && c != '\n'; i++)
  24. line[i] = c;
  25.  
  26. if(c == '\n')
  27. {
  28. line[i] = c;
  29. ++i;
  30. }
  31. line[i] = '\0';
  32.  
  33. }
  34.  
  35.  
  36.  
  37. void bufferKeys()
  38. {
  39. int c;
  40. do{
  41. c= getchar();
  42. }while (c<32);
  43. }
  44.  
  45.  
  46.  
  47. void naive_sort (struct Music array [], int arraySize, int * count);
  48. void swap (struct Music * v1, struct Music * v2);
  49. void enterData(struct Music myDB[]);
  50. void filewriting(struct Music myDB[]);
  51. void readfromFile(struct Music myDB[]);
  52. void sortAndCriticalCount(struct Music myDB[], int choice);
  53. void find(struct Music myDB[]);
  54. void quick_sort (struct Music array[], int left, int right);
  55. void q_sort (struct Music array[], int count);
  56.  
  57. const int datasize = 10;
  58. // file pointer
  59. FILE * mydata;
  60.  
  61.  
  62.  
  63. int main()
  64. {
  65. int choice = 0;
  66.  
  67. struct Music myDB[datasize];
  68.  
  69.  
  70.  
  71. do{
  72. system("CLS"); //clear screen
  73. printf("Application\n");
  74. printf("1 Enter Data\n");
  75. printf("2 Bubble Sort\n");
  76. printf("3 Quick Sort\n");
  77. printf("4 Write Data to file\n");
  78. printf("5 Read Data to file\n");
  79. printf("6 Search\n");
  80. printf("7 Diagnostics\n");
  81. printf("-1 Exit\n");
  82. printf("Enter a number : ");
  83. scanf("%d",&choice);
  84.  
  85. if(choice == 1)
  86. enterData(myDB);
  87. else if(choice == 2)
  88. sortAndCriticalCount(myDB,2);
  89. else if(choice == 3)
  90. q_sort(myDB,1);
  91. else if(choice == 4)
  92. filewriting(myDB);
  93. else if(choice == 5)
  94. readfromFile(myDB);
  95. else if(choice == 6)
  96. find(myDB);
  97. else if (choice == 7)
  98. sortAndCriticalCount(myDB,7);
  99. else if(choice == -1)
  100. exit(0);
  101. else if ((choice > 1) || (choice < 6))
  102. {
  103. printf("\nChoice not available press enter to continue");
  104. fflush(stdin);
  105. getch();
  106. }
  107. }while(choice != -1);
  108. exit(0);
  109. }
  110.  
  111. void sortAndCriticalCount(struct Music myDB[],int choice)
  112. {
  113. system("CLS");
  114. int count = 0;
  115. naive_sort (myDB, datasize, & count);
  116. if(choice == 5)
  117. printf("\nCritical count is : %d\n",count);
  118. else
  119. printf("Array has been sorted");
  120. printf("Press enter to return to menu");
  121. fflush(stdin);
  122. getch();
  123. }
  124.  
  125. void enterData(struct Music myDB[])
  126. {
  127. system("CLS");
  128. int i;
  129.  
  130. //input Data from the keyboard
  131. //go through record
  132. //write to disk using formatted output
  133. i=1;
  134. while(i!=0)
  135. {
  136. printf("Enter the Artist: \n");
  137. getline(myDB[i].Artist);
  138. bufferKeys();
  139. printf("Enter the Album: \n");
  140. getline(myDB[i].Album);
  141. bufferKeys();
  142. printf("Enter Label name: \n");
  143. getline(myDB[i].Label);
  144. bufferKeys();
  145. printf("Enter Year: \n");
  146. getline(myDB[i].Year);
  147. bufferKeys();
  148. printf("Enter the Genre of music: \n");
  149. getline(myDB[i].Genre);
  150. bufferKeys();
  151.  
  152. printf("\n\n press 1 to continue,0 to stop\n\n");
  153. scanf("%d",&i);
  154. break;
  155. }
  156.  
  157.  
  158. }
  159.  
  160. void find(struct Music myDB[])
  161. {
  162. system("CLS");
  163. int i;
  164. char name[20];
  165. printf("Enter Artist name :");
  166. scanf("%s", name);
  167. for(i = 0;i<datasize;i++)
  168. {
  169. if((strcmp(name,myDB[i].Artist))==0)
  170. {
  171. printf("\n");
  172. printf("%s\n",myDB[i].Artist);
  173. printf("%s\n",myDB[i].Album);
  174. printf("%s\n",myDB[i].Label);
  175. printf("%d\n",myDB[i].Year);
  176. printf("%s\n",myDB[i].Genre);
  177. }
  178. }
  179. printf("Press enter to return to menu");
  180. fflush(stdin);
  181. getch();
  182.  
  183. }
  184.  
  185.  
  186. void filewriting(struct Music myDB[])
  187. {
  188. system("CLS");
  189. int i;
  190. mydata=fopen("mymusic.txt","w");
  191.  
  192. for (i=0;i<datasize;i++)
  193. {
  194. fprintf(mydata, "%s %s %s %s %s\n",myDB[i].Artist, myDB[i].Album,
  195. myDB[i].Label,myDB[i].Year,myDB[i].Genre);
  196. //Close the file
  197. }
  198. printf("Data has been written to file");
  199. fflush(stdin);
  200. getch();
  201. fclose(mydata);
  202. }
  203.  
  204. void readfromFile(struct Music myDB[])
  205. {
  206. system("CLS");
  207. int j;
  208. mydata = fopen("mymusic.txt","r");
  209. //Read the data that was written
  210. printf("reading data...\n");
  211.  
  212. for (j=0; j<datasize; j++)
  213. {
  214.  
  215. fscanf(mydata,"%s %s %s %s %s",myDB[j].Artist, myDB[j].Album,
  216. myDB[j].Label,
  217. myDB[j].Year, myDB[j].Genre);
  218. printf("Record %d\n",j+1," is...\n");
  219. printf("%s\n",myDB[j].Artist);
  220. printf("%s\n",myDB[j].Album);
  221. printf("%s\n",myDB[j].Label);
  222. printf("%s\n",myDB[j].Year);
  223. printf("%s\n",myDB[j].Genre);
  224. printf("\n");
  225.  
  226. }
  227. fclose(mydata);
  228. printf("Press enter to return to menu");
  229. fflush(stdin);
  230. getch();
  231. // close the file again
  232. }
  233.  
  234.  
  235.  
  236.  
  237. void naive_sort (struct Music array [], int arraySize, int * count)
  238. {
  239. for (int pass = 0; pass <= arraySize - 2; pass++)
  240. { for (int counter = 0; counter <= arraySize - 2-pass; counter++)
  241. {
  242. *count = *count + 1; // count critical operations
  243. if (strcmp(array[counter].Artist,array[counter+1].Artist)>0)
  244. swap (&array[counter], &array[counter+1]);
  245. }
  246. }
  247. }
  248.  
  249.  
  250.  
  251. void swap (struct Music * v1, struct Music * v2)
  252. {
  253. struct Music temp;
  254. temp = *v1;
  255. *v1 = *v2;
  256. *v2 = temp;
  257. }
  258.  
  259.  
  260. void q_sort (struct Music array[], int count)
  261. {
  262. quick_sort(array,0,count-1);
  263. }
  264. void quick_sort (struct Music array[], int left, int right)
  265. {
  266. int i, j;
  267. char *x;
  268. struct Music temp;
  269.  
  270. i = left;
  271. j = right;
  272.  
  273. x = array[(left+right)/2].Genre;
  274.  
  275. do {
  276. while(strcmp(array[i].Genre,x)<0 && i<right) i++;
  277. while(strcmp(array[i].Genre,x)>0 && j>left) j--;
  278. if(i<=j) {
  279. temp = array[i];
  280. array[i] = array[j];
  281. array[j] = temp;
  282. i++; j--;
  283.  
  284. }
  285.  
  286. }while(i<=j);
  287.  
  288. if(left<j) quick_sort(array, left, j);
  289. if(i<right)quick_sort(array, i, right);
  290. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Finally Almost Finished 2 Problems!! :(

 
0
  #4
Mar 17th, 2006
  1. fflush(stdin);
Did you read my previous post?
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC