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
|
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void delete_repeats (char arr[], int slots);
/* Slots is the declared size of the character array arr.
Repeated characters will be deleted within the array, whilst the empty spaces will be moved to the end of the array.
The displaychars function is then called, with the modified array and the new size of the array passed to it. */
void displaychars (char arrayoutput[], int slots);
/* This function displays the characters of the array passed to it, along with the size of the array, as represented by
slots */
int main()
{
char testarray[20] = {'a', 'b', 'a', 'c', 'd',
'd', 'e', 'f', 'g', 'h', 'e', 'e', 'f', 'g', 'i', 'j', 'k', 'l', 'l', 'm'};
int size = 20;
cout << "Here is the test array prior to removing the duplicate characters: \n"; displaychars(testarray, size);
cout << "\n\nHere is the modified array, with duplicates removed: \n";
delete_repeats(testarray, size);
}
void delete_repeats (char arr[], int slots)
{
int index = 0;
int compare = 1;
int shifter = 0;
do
{
do
{
if (arr[index] == arr[compare]) //Catches duplicates
{
shifter = compare; // shifter is used in place of compare so compare can retain integer value
do
{
arr[shifter] = arr[shifter+1]; //Since a duplicate was found, this loop overwrites
shifter++; //the duplicate with the next character in the array
} while (shifter <= slots); //and proceeds to shift all characters up one position
//in the array
slots = slots - 1; //Since a match was found, the # of slots has been reduced by one.
}
if (arr[index] != arr[compare]) //Catches any consecutive duplicates (or rather, purposely "misses" them)
{ //in order to keep the integer value of compare appropriate.
compare = compare + 1; //Compare is only incremented if the following character is not a duplicate too.
}
} while (compare <= slots); //This guides the "index versus rest of chars in array" loop.
index = index + 1; //Index is incremented by one to test the next character in the array.
compare = index + 1;//Compare gets initialized to the location in the array that follows index.
//Due to the structure of the preceding loops, there will be no duplicates preceding index,
//thus index need only be compared to the array characters that follow it.
} while (index <= slots); //This guides the controlling loop... should be obvious.
slots = slots + 1; //This takes care of slots being decreased by one errantly on the final index comparison.
displaychars(arr, slots);
system ("pause"); //This was needed when working in Dev C++. I used both VS2008 and DevC++ for this lab.
}
void displaychars(char arrayoutput[], int slots)
{
cout << "# of slots used = "; cout << slots; cout << "\n";
int tempdisplay = 0;
do
{
cout << arrayoutput[tempdisplay];
tempdisplay ++;
} while (tempdisplay < slots);
cout << endl << endl;
}
|