Cursor Base implementation of list problem

Thread Solved

Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training

Cursor Base implementation of list problem

 
0
  #1
28 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: SVR is an unknown quantity at this point 
Solved Threads: 4
SVR SVR is offline Offline
Light Poster
 
0
  #2
27 Days Ago
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; 27 Days Ago at 1:00 am. Reason: spelling
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training
 
0
  #3
27 Days Ago
Originally Posted by SVR View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 60
Reputation: Whilliam is an unknown quantity at this point 
Solved Threads: 0
Whilliam's Avatar
Whilliam Whilliam is offline Offline
Junior Poster in Training
 
0
  #4
27 Days Ago
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC