Coping Non-Repeating numbers from one array to another.

My issue is on line 73 through 99. What i cant seem to figure out is how to loop through an array and copy all the non repeating numbers and place them in another array? each time ive done it, it will either give me random numbers or only display the repeating numbers

i have my main array which is called alpha and my other array which will hold my non repeating numbers called diffAlpha.
and im supposed to be using dynamic arrays.



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
 #include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;


int main()
{
	int *alpha;
	alpha = new int[50]; //dynamic array alpha

	int num = 0;  //used to determine how big diffAlpha will be since we dont know how many numbers will be placed in the array
	int *diffAlpha = new int[num];

	std::fill(alpha, alpha + 50, -1); //set all elements to -1
	
	cout << "Output the value of the first component of the array alpha." << endl;
	cout << alpha[0] << endl;
	
  	alpha[25] = 62;
	alpha[10] = (alpha[49] * 3) + 10;

	cout << "Use a for loop to output the value of a component of alpha if its index is a multiple of 2 or 3" << endl;
	for (int i = 0; i < 50; i++)
	{
		if (!(alpha[i] % 2))
		{
			cout << alpha[i] << endl;
		}
		if (!(alpha[i] % 3))
		{
			cout << alpha[i] << endl;
		}
	}

	cout << "Output the value of the last component of alpha." << endl;
	cout << alpha[49] << endl;

	cout << "Output the value of the alpha so that 15 components per line are printed." << endl;
	for (int i = 0; i < 15; i++)
	{
		cout << alpha[i] << "  ";
	}
	cout << endl;
	for (int i = 15; i < 30; i++)
	{
		cout << alpha[i] << "  ";
	}
	cout << endl;
	for (int i = 30; i < 45; i++)
	{
		cout << alpha[i] << "  ";
	}
	cout << endl;
	cout << "Use a for loop to increment every other element (the even indexed elements)." << endl;

	int count = 0;
	for (int i = 0; i < 50; i++)
	{
		if (!(alpha[i] % 2))
		{
			count++;
			if (count % 2)
			{
				cout << alpha[i] << "  ";
				
			}
		}	
	}
	cout << endl;
	cout << "Create a new  array,  diffAlpha,  whose  elements  are  the  differences  between consecutive elements in alpha" << endl;

	for (int i = 0; i < 50; i++)
	{
	
		for (int j = 0; j < 50; j++)
		{
			if (alpha[i] == alpha[j])
			{
				int value = 0; //Where the number will be placed in the array.
				diffAlpha[value] = alpha[i];
				value += 1; 
				num++;   //increment num value to determine size of the array.
				
			}
		}		
		
	}
	cout << endl;
	
	for (int i = 0; i < num; i++)
	{
		cout << diffAlpha[i] << "  ";
	}
	system("Pause");
    return 0;
}
Last edited on
Lines 14-15: new int[0]; // error
How many elements are in an array that has 0 elements?

Make a temporary array of 50 elements. Copy the uniques.
Now you know how long the diffAlpha must be. Create it so.
Copy from temporary to diffAlpha and then remove the temporary.
Topic archived. No new replies allowed.