Problem with dynamic array

I want to calculate which modi are present in the numbers that the user gives in. This is the code:

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
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    // Declaring variables
    // read in numbers
    int N;       // how many numbers does the user want to give
    int * numbers; // array to save the numbers

    // statistics 
    int * modus; // array to save modus
    int N_modus; // how many modus are there

   // help-variables
   int temporary;
   bool changed; 
   int * unique_numbers;
   int * unique_numbers_freq;
   int N_unique_numbers;
   int frequency, max_freq;

   // as user how many numbers
   cout << "How many numbers do want to give in?" << endl;
   cin >> N;
   
   // make dynamic array numbers
   numbers = new int [N];

   // ask user for numbers and save to array
   cout << "Give in the numbers.";
   for (int i=0; i<N; i++) cin>>numbers[i];

   // sort numbers from small to large
   do
   {
        changed = 0;
        for (int i = 0; i<(N-1); i++)
        {
             if (numbers[i+1]<numbers[i])
             {
                 temporary = numbers[i];
                 numbers[i] = numbers[i+1];
                 numbers[i+1] = temporary;
                 changed = 1;
              }
         }
   } while (changed == 1);

   // calculate which unique numbers are present and how many times
   	frequency = 1;
	max_freq = 1;
	N_unique_numbers = 1;
	
	for (int i=0; i<N-1; i++)
	{
		
		if (numbers[i] == numbers[i+1]) 
			frequency ++;
		else 
		{
			unique_numbers = new int [N_unique_numbers];
			unique_numbers_freq = new int [N_unique_numbers];

			unique_numbers[N_unique_numbers] = numbers[i]; 
			unique_numbers_freq[N_unique_numbers] =frequency;
			N_unique_numbers++;
			frequency= 1; 
		}

		if (frequency>max_freq)
			max_freq = frequency;
	}

	//Put in the last number
	unique_numbers[N_unique_numbers] = numbers [N-1];
	unique_numbers_freq[N_unique_numbers] = frequency;
	N_unique_numbers++;


However, the problem is, that when I want to show the array with the unique numbers with following code:

1
2
3
	
        // But here is the problem
        for (int i = 0; i < N_unique_numbers; i++) cout << unique_numbers[i] << " ";


I get this for output (when I give in 5 numbers: 1 2 3 4 5):
-842150451
-842150451
-842150451
4
5

Can anybody help me out?
Last edited on
Topic archived. No new replies allowed.