Array Struct Sort Help!! Urgent!

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

Array Struct Sort Help!! Urgent!

 
0
  #1
Dec 5th, 2005
Ok i've tried understanding some previous posts but im just finding it really hard to work with structure passing to other functions and sorting.

Can someone please explain in a simplistic on how i can pass structure to a function that sorts the structure by 'Year' or 'Artist'
Below is the code i've done so far that allows a user to input records and those records are then put to a *.txt file....how can i proceed from there on...

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // Idea here is to enter an array of structures
  6. // write them to disk and read them back
  7.  
  8.  
  9. // simple structure
  10. struct tuple {
  11. char artist[50];
  12. char album[50];
  13. char label[50];
  14. int year[4];
  15. char genre[30];
  16. };
  17.  
  18. int filewrite() {
  19.  
  20. // file pointer
  21. FILE * mydata;
  22.  
  23. // structures
  24. struct tuple myDB[20];
  25. struct tuple otherDB[20];
  26.  
  27. // variable needed
  28. int i;
  29.  
  30.  
  31. // go through each record
  32. // write each to disk using formatted output
  33. mydata = fopen("mymusic.txt","w");
  34. i=1;
  35.  
  36. while(i){
  37. printf("Enter the artist: \n");
  38. //scanf("%s",myDB[i].artist);
  39. scanf("%s",myDB[i].artist);
  40. printf("Enter the album: \n");
  41. scanf("%s",myDB[i].album);
  42. printf("Enter label name: \n");
  43. scanf("%s",myDB[i].label);
  44. printf("Enter year: \n");
  45. scanf("%d",&myDB[i].year);
  46. printf("Enter the genre of music: \n");
  47. scanf("%s",&myDB[i].genre);
  48.  
  49. fprintf(mydata, "%s %s %s %d %s\n",myDB[i].artist, myDB[i].album, myDB[i].label,myDB[i].year,myDB[i].genre);
  50. printf("\n\n press 1 to continue,0 to stop");
  51. scanf("%d",&i);
  52.  
  53. }
  54.  
  55. fclose(mydata);
  56.  
  57.  
  58. // read the data
  59. printf("\n\n\n\n\n\n\n\nreading data...\n");
  60.  
  61. mydata = fopen("mymusic.txt","r");
  62.  
  63.  
  64. while(i){
  65. fscanf(mydata,"%s %s %s %d %s",otherDB[i].artist, otherDB[i].album, otherDB[i].label, &otherDB[i].year, otherDB[i].genre);
  66. printf("\n\n\nRecord is...\n\n\n");
  67. printf("%s\n",otherDB[i].artist);
  68. printf("%s\n",otherDB[i].album);
  69. printf("%s\n",otherDB[i].label);
  70. printf("%d\n",otherDB[i].year);
  71. printf("%s\n",otherDB[i].genre);
  72.  
  73. }
  74. fclose(mydata);
  75.  
  76. }
  77. //***********************************************************//
  78. int bubble(int x[],int n)
  79. {
  80. int hold,j,pass,i,switched = 1;
  81. for(pass = 0; pass < n-1 && switched == 1;pass++)
  82. {
  83. switched=0;
  84. for (j=0;j<n-pass-1;j++)
  85. if (x[j]>x[j+1])
  86. {
  87. switched=1;
  88. hold = x[j];
  89. x[j] = x[j+1];
  90. x[j+1]=hold;
  91. }
  92. }
  93. return(0);
  94. }
  95.  
  96. //****************************//
  97.  
  98. void main()
  99. {
  100. int c;
  101.  
  102. while(c!=5)
  103. {
  104.  
  105. printf("GIVE CHOICE--\n");
  106. printf(" 1 TO ENTER CD INFO.\n");
  107. printf(" 2 TO SEE STUDENT.TXT FILE\n");
  108. printf(" 3 TO SORT CDs BASED ON ARTIST\n");
  109. printf(" 4 TO SEARCH USING ALBUM NAME\n");
  110. printf(" 5 TO EXIT\n\n--");
  111. scanf("%d",&c);
  112.  
  113. switch(c)
  114. {
  115. case 1:
  116. filewrite();
  117. break;
  118. case 5:
  119.  
  120. break;
  121. default:
  122. break;
  123. }
  124. }
  125.  
  126. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,585
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: 1487
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Array Struct Sort Help!! Urgent!

 
0
  #2
Dec 5th, 2005
In order to sort, you first need an array of structures. Linked lists can be sorted, but much more difficult than arrays.
  1. struct tuple array[NumTuples];

Now, after filling in all the array elements, you want to pass to function bubble and sort by Year. In the case of arrays, its a lot faster to sort an array of ints that represent the index values of the array of structures.

  1. int bubble(int x[],struct tuple array[], int n)
  2. // function is going to sort array[] indirectly by use of integer
  3. // array x[].
  4. {
  5. int i,j;
  6. for(i = 0; i < n-1; i++)
  7. {
  8. for(j = i+1; j < n; j++)
  9. {
  10. if( array[x[i]].Year > array[x[j]].Year)
  11. {
  12. int hold = array[x[i]].Year;
  13. array[x[i]].Year = array[x[j]].Year;
  14. array[x[j]].Year = hold;
  15. }
  16. }
  17. }
  18. return 0;
  19. }
  20.  
  21. int main()
  22. {
  23. int i;
  24. struct tuple array[NumTuples];
  25. int indexs[NumTuples];
  26. // initialize indexes
  27. for(i = 0; i < NumTuples; i++)
  28. indexs[i] = i;
  29. // sort the array
  30. bubble(indexs,array, int NumTuples);
  31. // print each elment
  32. for(i = 0; i < NumTuples; i++)
  33. printf("%s\n", array[indexs[i]].artist);
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