Dynamic array management

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 30
Reputation: RisTar is an unknown quantity at this point 
Solved Threads: 0
RisTar RisTar is offline Offline
Light Poster

Dynamic array management

 
0
  #1
Mar 15th, 2007
Hi, im trying to create a program that will control a dynamic array .
The program have few functions to handle dynamic array.

* Add number to array
* Erase number from array
* Print the array

I have two problems with my code .
The first one is with the EraseNumber function .
I just cant understand why it doesnt work like it should ,
The second is that every time that the menu pops up , that text
Of the menu start to be really messy for some reason .
Maybe one of you would take a look at the code and tell me what he
Thinks that the problem is .

Btw. im using Turbo C , and im runing it on a win2k OS.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <alloc.h>
  4. #include <conio.h>
  5. // Functions proto-type
  6. void mainMenu();
  7. void memoryTester(int *array);
  8. int* addNumber(int *array,int arraysize,int var);
  9. int* eraseNumber(int *array,int arraysize,int var);
  10. void printfArray(int *array,int arraysize);
  11. // Main function
  12. int main()
  13. {
  14. int *array = NULL;
  15. int arraysize = 0;
  16. int var = 0;
  17. int choice;
  18. // Main loop
  19. while (choice != 4)
  20. {
  21. mainMenu();
  22. printf ("Enter your choice: ");
  23. scanf ("%d",&choice);
  24. switch (choice)
  25. {
  26. case 1: printf ("Add number: ");
  27. scanf ("%d",&var);
  28. arraysize++;
  29. array = addNumber(array,arraysize,var);
  30. break;
  31. case 2: printf ("Erase number: ");
  32. scanf ("%d",&var);
  33. arraysize--;
  34. array = eraseNumber(array,arraysize,var);
  35. break;
  36. case 3: putchar('\n');
  37. printf ("**** PRINTING ARRAY ****\n\n");
  38. printfArray (array,arraysize);
  39. getch();
  40. break;
  41. case 4: printf ("Press any key to quit...\n");
  42. getch();
  43. break;
  44. default: printf ("Wrong choice!\n");
  45. printf ("Press any key to continue...\n");
  46. getch();
  47. break;
  48. }
  49. }
  50. free (array);
  51. return 0;
  52. }
  53. // Main menu function
  54. void mainMenu()
  55. {
  56. clrscr();
  57. printf ("**** MAIN MENU ****\n\n");
  58. printf ("1. Add number...\n");
  59. printf ("2. Erase number...\n");
  60. printf ("3. Print array...\n");
  61. printf ("4. Quit...\n");
  62. putchar ('\n');
  63. }
  64. // Memory test function
  65. void memoryTest(int *array)
  66. {
  67. if (array == NULL)
  68. {
  69. printf ("Failed to allocate memory!\n");
  70. printf ("Press any key to quit...\n");
  71. getch();
  72. exit(1);
  73. }
  74. }
  75. // Add number function
  76. int* addNumber(int *array,int arraysize,int var)
  77. {
  78. int *temp,i;
  79. if (array == NULL)
  80. {
  81. array = (int *) malloc (arraysize * sizeof(int));
  82. memoryTest(array);
  83. array[0] = var;
  84. }
  85. else
  86. {
  87. temp = array;
  88. array = (int *) malloc (arraysize * sizeof(int));
  89. memoryTest(array);
  90. for (i=0;i<arraysize-1;i++)
  91. array[i] = temp[i];
  92. array[i] = var;
  93. }
  94. free (temp);
  95. return array;
  96. }
  97. // Erase number function
  98. int* eraseNumber(int *array,int arraysize,int var)
  99. {
  100. int *temp,i,tmp=0;
  101. if (arraysize == 0)
  102. {
  103. array = NULL;
  104. return array;
  105. }
  106. for (i=0;i<arraysize+1;i++)
  107. {
  108. if (array[i] == var)
  109. tmp = 1;
  110. }
  111. if (tmp == 0)
  112. {
  113. printf ("Error: No such number!\n");
  114. printf ("Press any key...\n");
  115. getch();
  116. return array;
  117. }
  118. temp = array;
  119. array = (int *) malloc (arraysize * sizeof(int));
  120. memoryTest(array);
  121. for (i=0;i<arraysize;i++)
  122. {
  123. if (temp[i] == var)
  124. continue;
  125. else
  126. array[i] = temp[i];
  127. }
  128. free (temp);
  129. return array;
  130. }
  131. // Print array function
  132. void printfArray(int *array,int arraysize)
  133. {
  134. int i;
  135. for (i=0;i<arraysize;i++)
  136. printf ("%d,",array[i]);
  137. putchar('\n');
  138. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Dynamic array management

 
0
  #2
Mar 26th, 2007
  1. // Functions proto-type
  2. void mainMenu();
  3. void memoryTester(int *array);
  4. int* addNumber(int *array,int arraysize,int var);
  5. int* eraseNumber(int *array,int arraysize,int var);
  6. void printfArray(int *array,int arraysize);
  7. // Main function
  8. int main()
  9. {
  10. int *array = NULL;
  11. int arraysize = 0;
  12. int var = 0;
  13. int choice;
  14. // Main loop
  15. while (choice != 4)
  16. {
  17. mainMenu();
  18. printf ("Enter your choice: ");
  19. scanf ("%d",&choice);
  20. switch (choice)
  21. {
  22. case 1:
  23. printf ("Add number: ");
  24. scanf ("%d",&var);
  25. arraysize++;
  26. array = addNumber(array,arraysize,var);
  27. break;
  28. case 2:
  29. printf ("Erase number: ");
  30. scanf ("%d",&var);
  31. arraysize--;
  32. array = eraseNumber(array,arraysize,var);
  33. break;
  34. case 3:
  35. putchar('\n');
  36. printf ("**** PRINTING ARRAY ****\n\n");
  37. printfArray (array,arraysize);
  38. getch();
  39. break;
  40. case 4:
  41. printf ("Press any key to quit...\n");
  42. getch();
  43. break;
  44. default:
  45. printf ("Wrong choice!\n");
  46. printf ("Press any key to continue...\n");
  47. getch();
  48. break;
  49. }
  50. }
  51. free (array);
  52. return 0;
  53. }
  54. // Main menu function
  55. void mainMenu()
  56. {
  57. // clrscr();
  58. printf ("**** MAIN MENU ****\n\n");
  59. printf ("1. Add number...\n");
  60. printf ("2. Erase number...\n");
  61. printf ("3. Print array...\n");
  62. printf ("4. Quit...\n");
  63. putchar ('\n');
  64. }
  65. // Memory test function
  66. void memoryTest(int *array)
  67. {
  68. if (array == NULL)
  69. {
  70. printf ("Failed to allocate memory!\n");
  71. printf ("Press any key to quit...\n");
  72. getch();
  73. exit(1);
  74. }
  75. }
  76. // Add number function
  77. int* addNumber(int *array,int arraysize,int var)
  78. {
  79. int *temp,i;
  80. if (array == NULL)
  81. {
  82. array = (int *) malloc (arraysize * sizeof(int));
  83. memoryTest(array);
  84. array[0] = var;
  85. }
  86. else
  87. {
  88. temp = array;
  89. array = (int *) malloc (arraysize * sizeof(int));
  90. memoryTest(array);
  91. for (i=0;i<arraysize-1;i++)
  92. array[i] = temp[i];
  93. array[i] = var;
  94. }
  95. free (temp);
  96. return array;
  97. }
  98. // Erase number function
  99. int* eraseNumber(int *array,int arraysize,int var)
  100. {
  101. int *temp,i,tmp=0;
  102. if (arraysize == 0)
  103. {
  104. array = NULL;
  105. return array;
  106. }
  107. for (i=0;i<arraysize+1;i++)
  108. {
  109. if (array[i] == var)
  110. tmp = 1;
  111. }
  112. if (tmp == 0)
  113. {
  114. printf ("Error: No such number!\n");
  115. printf ("Press any key...\n");
  116. getch();
  117. return array;
  118. }
  119. temp = array;
  120. array = (int *) malloc (arraysize * sizeof(int));
  121. memoryTest(array);
  122. for (i=0;i<arraysize;i++)
  123. {
  124. if (temp[i] == var)
  125. continue;
  126. else
  127. array[i] = temp[i];
  128. }
  129. free (temp);
  130. return array;
  131. }
  132. // Print array function
  133. void printfArray(int *array,int arraysize)
  134. {
  135. int i;
  136. for (i=0;i<arraysize;i++)
  137. printf ("%d,",array[i]);
  138. putchar('\n');
  139. }
Last edited by thekashyap; Mar 26th, 2007 at 6:21 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Dynamic array management

 
0
  #3
Mar 26th, 2007
Problems I see:
1. Line 84: Very first insertion happens in slot 0 whereas the arraysize passed would be 1 at that time.
For all other insertions (where arraysize passed is > 0) the insertion happens at index arraysize. => Inconsistant.
2. Line 112: According to Line 84, it's possible to have a valid number at index 0. => Consider initializing tmp to -1 instead of 0.
3. Line 95: This free() will crash on first insertion as tmp isn't initialized then. Should be moved inside the else part above.
4. Line 110: Seems like a typo, should be tmp = i.
5. Line 124: Although it is assumed by caller (main()) that one call to eraseNumber() will erase ONE entry (because main() does arraysize-- on line 31), because of condition on line 124, if the array contained multiple entries for same number then all of them will be removed.
So say if before a call to eraseNumber() array size of 20 and number X was repeated 5 times in array. After a call to erase
- the variable arraysize will be 19
- array will contain 15 valid numbers (all except those five X)
- last 4 elements in the array would be junk (uninitialized).

A few suggesions:
>> The second is that every time that the menu pops up , that text Of the menu start to be really messy for some reason .
Use fflush(0) before every call to scanf(). See documentation of fflush() to see why.

1. Do not change the value of arraysize inside main(), pass it by pointer to add/eraseNumber functions and let it be changed there.
2. Consider using memcpy() instead of for-loops to copy from temp-array to real array and vise-versa.
3. Take a decision abt how you wanna handle repetaion of same number. Change add/errase appropriately. If you don't want repetation you can give error from addNumber().
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