#include <iostream>
#include <stdlib.h>
usingnamespace 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 = newint [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 = newint [N_unique_numbers];
unique_numbers_freq = newint [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