Arrays, The Binary Search Algorithm

Hi I have to write a program that removes all duplicate values from a sorted array of integers, leaving only a single copy of each value. I'm almost done with the program however, I'm having problems checking the number for duplicates.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include #include "simpio.h" void removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]); int main() { int size=5; int inpArray[size], inpArraySize, outArray[size], arr[size]; removedup(inpArray, inpArraySize, outArray, arr); getchar(); return 0; } void removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]) { int x, y, w=1; int z=0; printf("Enter the size of inpArray\n"); inpArraySize=GetInteger(); printf("Enter %d integers\n", inpArraySize); for (x=0; x
You don't need a binary search to remove duplicates from a sorted array. Binary search is a Search algorithm. Here, you're not searching anything.

Here's an easier to understand program.

You can write a separate function to fill-in the input array and call this function.

 ``1234567891011121314`` ``````int removeDup(int* a, int& size) { for(int k=1; k
Last edited on
Ok I changed the program a little towards your example n4nature. However, in the output there are duplicates.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include "simpio.h" int removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]); int main() { int size=5; int inpArray[size], inpArraySize, outArray[size], arr[size]; removedup(inpArray, inpArraySize, outArray, arr); getchar(); return 0; } int removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]) { int i, x, k; printf("Enter the size of inpArray\n"); inpArraySize=GetInteger(); printf("Enter %d integers\n", inpArraySize); for (x=0; x
Can you post the sample input and output?
sure.

input size = 8
input for 'inpArray[x]' = 1 2 1 4 3 5 7 3
output for 'outArray[k]' = 2 1 4 3 5 7 3
Didn't you mention in your program statement that the input array was supposed to be a sorted one, with duplicates?
Oh...Yes I am. I forgot about that.
Anyway, I do see a few things, as mentioned below:

Original snippet:
 ``12345678910`` ``````for(int k=1; k

Modify it to:
 ``12345678910`` ``````for(int k=1; k
ok however, now the program crashes..

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include "simpio.h" int removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]); int main() { int size=5; int inpArray[size], inpArraySize, outArray[size], arr[size]; removedup(inpArray, inpArraySize, outArray, arr); getchar(); return 0; } int removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]) { int i, x, k; printf("Enter the size of inpArray\n"); inpArraySize=GetInteger(); printf("Enter %d integers\n", inpArraySize); for (x=0; x
It was an uncomplied program. I had a chance to compile the program now.

This works for me:
 ``123456789101112131415`` ``````int removeDup(int* a, int& size) { for(int k=1; k
Last edited on
Actually I almost figured program out. It's just not printing out anything out. (And thanks for reminding me about sorting the array).

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include "simpio.h" int removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]); int main() { int size=5; int inpArray[size], inpArraySize, outArray[size], arr[size]; removedup(inpArray, inpArraySize, outArray, arr); getchar(); return 0; } int removedup (int inpArray[], int inpArraySize, int outArray[], int arr[]) { int j = 0; int x, k, i; int count = 0; int flag, temp; printf("Enter the size of inpArray\n"); inpArraySize=GetInteger(); printf("Enter %d integers\n", inpArraySize); for (x=0; x
I figured my program out. And thanks n4nature.
Topic archived. No new replies allowed.