vimix

lab10 cs1

Apr 2nd, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. //Dynamically allocating and filling array in a function example
  2. //Venix Cador
  3. //03/29/2012
  4.  
  5. #include <iostream>
  6. #include "VarArray.h"
  7.  
  8. using namespace std;
  9.  
  10. void addNumber(int *& arrayptr, int number, int &size);
  11.  
  12. void removeNumber(int *& arrayptr, int number, int &size);
  13.  
  14. void output(int *arrayptr, int size);
  15.  
  16. //Main fucntion
  17.  
  18. int main(){
  19. int number,
  20. size = 0,
  21. *ptr = new int [size];
  22.  
  23. char status = 'n';
  24. char action = 'a';
  25.  
  26. do
  27. {
  28. cout <<"done? [y/n]: ";
  29. cin >> status;
  30.  
  31. if(status == 'n'){
  32. cout << "add or remove? [a/r]: ";
  33. cin >> action;
  34. cout << "Input number: ";
  35. cin >> number;
  36.  
  37. switch (action) {
  38. case 'a':
  39. addNumber(ptr,number, size);
  40. output(ptr, size);
  41. break;
  42. case 'r':
  43. removeNumber(ptr, number, size);
  44. output(ptr, size);
  45. break;
  46. default:
  47. cout << "Please enter a valid action!" << endl;
  48. }
  49. }
  50.  
  51. }while( status == 'n');
  52.  
  53. output(ptr, size);
  54.  
  55. return 0;
  56.  
  57. }
  58. //addNumber where you add the number and increase the size of the array
  59. void addNumber(int *& arrayptr, int number, int &size){
  60. // create a new array;
  61. //increment the size and copy the first array to the second one;
  62.  
  63. int* ptr;
  64. ptr = new int[size + 1];
  65.  
  66. for(int i = 0; i < size; i++) {
  67. ptr[i] = arrayptr[i];
  68. }
  69.  
  70. //put the number inside the last element of ptr;
  71. //delete old array;
  72. ptr[size] = number;
  73.  
  74. delete [] arrayptr;
  75. arrayptr = ptr;
  76. size++;
  77. }
  78. //this where you remove the number and decrease the size by one
  79. //the array shrink everytime you remove a number from the array
  80. void removeNumber(int *& arrayptr, int number, int &size){
  81. int* ptr;
  82. ptr = new int[size-1];
  83. int ptrcounter = 0;
  84. for(int i = 0; i < size; ++i){
  85. if (arrayptr[i] != number) {
  86. ptr[ptrcounter] = arrayptr[i];
  87. ptrcounter++;
  88. }
  89. }
  90.  
  91. delete [] arrayptr;
  92. arrayptr = ptr;
  93. size--;
  94. }
  95. //That is where you the function printed the number in incresing order
  96. //It finds the smallest number to be output first.
  97. void output(int *arrayptr, int size) {
  98. int* ptr;
  99. int tempsize=size;
  100. ptr = new int [size];
  101.  
  102. for(int i = 0; i < size; i++) {
  103. ptr[i] = arrayptr[i];
  104. }
  105. while(tempsize!=0){
  106. int smallest= ptr[0];
  107. for (int i = 0; i < tempsize; i++)
  108. if (ptr[i]<smallest)
  109. smallest=ptr[i];
  110. cout<<smallest<<" ";
  111. removeNumber(*&ptr, smallest, tempsize);
  112. }
  113. cout<<endl;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment