944,030 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5548
  • C RSS
Dec 5th, 2005
0

Array Struct Sort Help!! Urgent!

Expand Post »
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. }
Similar Threads
Reputation Points: 13
Solved Threads: 0
Junior Poster in Training
nabil1983 is offline Offline
73 posts
since Mar 2005
Dec 5th, 2005
0

Re: Array Struct Sort Help!! Urgent!

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);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 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: command line problem
Next Thread in C Forum Timeline: Resetting arrays





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


Follow us on Twitter


© 2011 DaniWeb® LLC