944,198 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 782
  • C RSS
Oct 30th, 2009
0

Cursor Base implementation of list problem

Expand Post »
Hello. I've been trying to solve this problem but I can't and my head hurts already. Please help me.
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5.  
  6. #define max 12
  7. #define num 30
  8. #define nam 24
  9.  
  10. typedef struct
  11. {
  12. char name[nam];
  13. int score;
  14. int rank;
  15. }CONTESTANT;
  16.  
  17. typedef struct
  18. {
  19. CONTESTANT CA;
  20. int next;
  21. }HeapSpace;
  22.  
  23. typedef struct
  24. {
  25. HeapSpace *H;
  26. int first;
  27. }VirtualHeap, *VHeap;
  28.  
  29. typedef struct
  30. {
  31. int storage[max];
  32. int ladder[max];
  33. int nextrung;
  34. }DualDataStruc, *DDS;
  35.  
  36. void initialize(DDS *D, VHeap *VH);
  37. int myMalloc(VHeap *VH);
  38. void myFree(VHeap *VH, int x);
  39. int hash(char n[]);
  40. void insert(DDS *D, VHeap *VH, int NOP);
  41. void display(DDS D, VHeap VH);
  42.  
  43. void initialize(DDS *D, VHeap *VH)
  44. {
  45. int ctr;
  46.  
  47. *D = (DDS) malloc(sizeof(DualDataStruc));
  48. *VH = (VHeap) malloc(sizeof(VirtualHeap));
  49. (*VH)->H = (HeapSpace *) malloc(sizeof(HeapSpace) * num);
  50. (*VH)->first = 0;
  51.  
  52. for(ctr = 0; ctr < num - 1; ctr++)
  53. {
  54. (*VH)->H[ctr].next = ctr + 1;
  55. }
  56. (*VH)->H[ctr].next = -1;
  57.  
  58. for(ctr = 0; ctr < max; ctr++)
  59. (*D)->storage[ctr] = -1;
  60.  
  61. (*D)->nextrung = 0;
  62. }
  63.  
  64. int myMalloc(VHeap *VH)
  65. {
  66. int temp;
  67.  
  68. temp = (*VH)->first;
  69. printf("%d\n", temp);
  70.  
  71. if(temp != -1)
  72. {
  73. (*VH)->first = (*VH)->H[temp].next;
  74.  
  75. printf("%d ", (*VH)->first);
  76. }
  77.  
  78. return temp;
  79. }
  80.  
  81. void myFree(VHeap *VH, int x)
  82. {
  83. int temp;
  84.  
  85. if(x != -1)
  86. {
  87. (*VH)->H[x].next = (*VH)->first;
  88. (*VH)->first = x;
  89. }
  90. }
  91.  
  92. int hash(char n[])
  93. {
  94. int ctr, sum = 0;
  95.  
  96. for(ctr = 0; ctr < strlen(n); ctr++)
  97. sum += (int)n;
  98.  
  99. return sum % max;
  100. }
  101.  
  102. void insert(DDS *D, VHeap *VH, int NOP)
  103. {
  104. int ctr;
  105. int x, h;
  106. char n[nam];
  107.  
  108. printf("Enter the name of the contestant:\n");
  109. for(ctr = 0; ctr < NOP; ctr++)
  110. {
  111. flushall();
  112. gets(n);
  113.  
  114. x = myMalloc(&(*VH));
  115. h = hash(n);
  116.  
  117. strcpy((*VH)->H[x].CA.name, n);
  118.  
  119. (*VH)->H[x].next = (*D)->storage[h];
  120. (*D)->storage[h] = x;
  121. (*D)->ladder[++((*D)->nextrung)] = x;
  122. }
  123. }
  124.  
  125. void display(DDS D, VHeap VH)
  126. {
  127. int ctr;
  128.  
  129. for(ctr = 1; ctr <= D->nextrung; ctr++)
  130. puts(VH->H[D->ladder[ctr]].CA.name);
  131.  
  132. getch();
  133. clrscr();
  134. }
  135.  
  136. void main(void)
  137. {
  138. DDS D;
  139. VHeap VH;
  140. int NOP;
  141.  
  142. initialize(&D, &VH);
  143.  
  144. do
  145. {
  146. printf("How many players are there? ");
  147. scanf("%d", &NOP);
  148.  
  149. if(NOP < 1 || NOP > 12)
  150. {
  151. printf("You can only input 1 to 12 players");
  152. getch();
  153. }
  154. clrscr();
  155. }while(NOP < 1 || NOP > 12);
  156.  
  157. insert(&D, &VH, NOP);
  158.  
  159. display(D, VH);
  160. }

I think the problem is in the myMalloc function. Notice that there are two printf in there. One is for the temp value and one is for the VH-> first value. When I ran the program and input different names, sometimes it goes the way I wanted it to. Sometimes, the value of first suddenly becomes -16384 or any number that is not what I expected. This messes up the arrays causing some undesirable effect. The unexpected numbers comes out randomly depending on the names I inputted or the order of names (e.g.: I inputted the names "Kris" and "Allison" in this order and it goes smoothly. I input "Allison and "Kris" in this order, the value of first after "Kris" becomes -1. It shouldn't be -1 because the program only got 2 virtualheaps).
I don't get this. Please help.
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Oct 31st, 2009
0
Re: Cursor Base implementation of list problem
It works for both cases here.
I didn't see any glaring problems that relate to your issue.

At line 97 I think you intended something else ...

  1. for(ctr = 0; ctr < strlen(n); ctr++)
  2. sum += (int)n[ctr]; // [ctr]<--- you want to sum the chars ?

I'll look some more tomorrow.
One suggestion; name variables/types to give more info.
DDS should be PDDS and D should be pPDds etc. Something to let you know it's a ptr to a ptr NOT a ptr to a struct. As it is it's really hard to follow.
Last edited by SVR; Oct 31st, 2009 at 1:00 am. Reason: spelling
SVR
Reputation Points: 10
Solved Threads: 4
Light Poster
SVR is offline Offline
44 posts
since May 2008
Oct 31st, 2009
0
Re: Cursor Base implementation of list problem
Click to Expand / Collapse  Quote originally posted by SVR ...
It works for both cases here.
I didn't see any glaring problems that relate to your issue.

At line 97 I think you intended something else ...

  1. for(ctr = 0; ctr < strlen(n); ctr++)
  2. sum += (int)n[ctr]; // [ctr]<--- you want to sum the chars ?

I'll look some more tomorrow.
One suggestion; name variables/types to give more info.
DDS should be PDDS and D should be pPDds etc. Something to let you know it's a ptr to a ptr NOT a ptr to a struct. As it is it's really hard to follow.
Yeah. Just to make a hash value in a hard way.
Thanks for the advice, I'll take it.
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008
Oct 31st, 2009
0
Re: Cursor Base implementation of list problem
Solved it already. The problem was just in the hash function. I think it was because, if the name inputted is too big, the integer the letters will keep incrementing until it exceeds the maximum for integer giving some undesirable number. I just change the hashing function.

Thank you SVR. You made me look at the statement you quoted.
Reputation Points: 19
Solved Threads: 0
Junior Poster
Whilliam is offline Offline
110 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: File modification time as an int
Next Thread in C Forum Timeline: Need help





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


Follow us on Twitter


© 2011 DaniWeb® LLC