vendredi 27 février 2015

c++ Remove the duplicates in sorted array - print the new array


I am testing out this piece of code in c++



#include <iostream>

using namespace std;

int removeDuplicates(int A[], int n) {
if(n == 0) return 0;

int index = 0;

for(int i=0; i<n; i++)
{
if(A[index] == A[i])
{

continue;
}
index++;
A[index] = A[i];
}
cout<<"New array is "<<A[index]<<endl;
return index+1;
}


int main(){

int x[10] = {1,1,6};
int n = 3;
int z = removeDuplicates(x, n);
cout<<"Length is "<<z;
}


The result of length is right. However, when cout the new array. The result is only 6. 1 is missing.


Where should I put cout<<"New array is "<<A[index]<<endl;


To give me the result as New array is 16


More code I need to add? Please help, anyone?




Aucun commentaire:

Enregistrer un commentaire