Debugging Dynamically Allocated Array

I am in Programming Fundamentals 3 at a college and I need help debugging my program. I have been trying to for 12 hours now and I would love any help. Thank you so much!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  #include <iostream>
#include <iomanip>
using namespace std;

void sorter(int [], int);
void shower(int [], int);

int main()
{
	int userNum = 0,
		userCount = 0,
		count = 0;
	int *memptr;

	// Directional Guide
	cout << "This program will allow you to enter as many whole numbers "
		<< "as you wish.\nTo start the rearrangemnet, type the value "
		<< "9999 for your next whole number." << endl;	
	cout << "What is your number? ";
	cin >> userNum;
	while (userNum != 9999)	
		{
			memptr = new int[userNum];// Setting up array for memory space
			memptr[count] = userNum;
			count++;
			cout << "What is your number? ";
			cin >> userNum;
		}

	sorter(memptr, count);
	shower(memptr, count);
	
	for (int i = 0; i < count; i++)
	{
		cout << memptr[i] << " " << endl;
	}
	
	
	delete [] memptr;
	return 0;
}

void sorter(int array[], int size) 
{
    bool swap;
    int temp;
    
    do 
	{
       swap = false;  
       for (int i = 0; i < size-1; i++)
	   {
          if (array[i] > array[i+1]) 
		  {
           temp = array[i]; 
		   array[i] = array[i + 1];
		   array[i + 1] = temp;
           swap = true;
          }
       }
    } while (swap);
 }
 
/*void shower(int array[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << array[i] << " " << endl;
	}
}/* */
How many arrays do you allocate?
How many arrays do you deallocate?
As many as the user wishes. That is the sentinal value "9999" that will stop it.
You deallocate exactly one array.
So i need to specify the array? like:

"delete [count] memptr;"
Roughly, an approach could look like this:
Dynamically allocate an array of size C. We'll call C the "capacity" of the array.
Keep track of an integer named S, which stands for the "size" of the array. Let S contain the value 0.

Each time a value needs to be added to your array, write it at index S, and increment S.
If and only if S is equal to C, allocate a new array with size C * 2. Then, copy the elements from the old array to the new array, and deallocate the old array.

This is basically the idea behind dynamic containers like std::vector.

Are you familiar with classes?
Thank you all so much!!! I finally got it working right!
Topic archived. No new replies allowed.