c++ remove duplicates from array

ok... i'm trying to remove same value that is entered something like this

Enter a non-negative integer (negative to quit): 1
Enter a non-negative integer (negative to quit): 2
Enter a non-negative integer (negative to quit): 3
Enter a non-negative integer (negative to quit): 2
Enter a non-negative integer (negative to quit): 1
Enter a non-negative integer (negative to quit): 6
Enter a non-negative integer (negative to quit): 3
Enter a non-negative integer (negative to quit): 4
Enter a non-negative integer (negative to quit): 5
Enter a non-negative integer (negative to quit): 2
Enter a non-negative integer (negative to quit): -4
You entered:
1 2 3 6 4 5

here is my 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
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using namespace std;

const int ARRAY_SIZE = 1000;

void printList(const int list[],
	int numItems);
int indexOfSmallest(const int list[], int startingIndex, int numItems);
void sort(int list[], int numItems);
void readNumbers(int list[], int &numItems);
  // bool checkSameNumber(int list[], int list2[], int numItems);

int main()
{
	int list[ARRAY_SIZE];
	int numItems;


	readNumbers(list, numItems);
	sort(list, numItems);
	cout << "The sorted list: "
		<< endl;
	printList(list, numItems);

	system("pause");
	return 0;
}



void readNumbers(int list[], int &numItems)
{
	int number;
	int count = 0; // the number of numbers that have been entered

	cout << "Enter a number (negative number to quit): ";
	cin >> number;
	while (number >= 0 && count < ARRAY_SIZE) {
		list[count] = number;
		count++;
		if (count < ARRAY_SIZE) {
			cout << "Enter a number (negative number to quit): ";
			cin >> number;
		}
		else {
			cout << "The array is now full." << endl;
		}
	}
	numItems = count;
}


void sort(int list[], int numItems)
{
	for (int count = 0; count < numItems - 1; count++) {
		swap(list[indexOfSmallest(list, count, numItems)],
			list[count]);
	}

}


int indexOfSmallest(const int list[], int startingIndex, int numItems)
{
	int targetIndex = startingIndex;

	for (int count = startingIndex + 1; count < numItems; count++) {
		if (list[count] < list[targetIndex]) {
			targetIndex = count;
		}
	}

	return targetIndex;
}

void printList(const int list[],
	int numItems)

{
	int i = 0, k = 0;
	while (i < numItems) {
		if (list[i] == list[i + 1])
			i++;
		else {
			list[k] = list[i]; // at here ERROR displaced not quit sure why....
			k++; i++;
		}
	}
	for (int j=0; j<k; j++){
		cout << list[j] << " ";
	}
}


all the things are working fine but i am struggling with void printList(const int list[],
int numItems)
i tried set the two array and remove when it's same but it seems not working... any solution?
Last edited on
The printList function takes the array as a const, but then you try to change the array on line 85. Remove the const keyword from the parameter list as well as in the prototype and it should work fine.
Topic archived. No new replies allowed.