I am having some trouble with this program ..
if input is abaabcdbab output should read abcd.... program is supposed to delete the repeated characters and output only one of each character but it is not working .... and ideas would be welcome

//Reads the letters in an array and deletes all repeated letters.
//Requires a function called deleteRepeats, also a partially filled
//array that requires two arguments. The function will have two formal parameters:
//an array parameter and a formal parameter of type int that gives the number of array positions.

#include <iostream>
using namespace std;
const int DECLARED_SIZE = 10;

void fillArray(char a[], int size, int& numberUsed);

void sort(char a[], int numberUsed);

void deleteRepeat(char a[], int& n);

int indexOfFirst(const char a[], int startIndex, int numberUsed);

int main()
{
    cout << "This program sorts characters and deletes any repeated characters.\n";

    int size = 4, numberUsed;
    char a[10];

    fillArray(a, 10, numberUsed);
    sort(a, numberUsed);
    deleteRepeat(a, size);

    cout << "In sorted order the letters are:\n";
    for (int index = 0; index < numberUsed; index++)
        cout << a[index] << " ";
    cout << endl;

    return 0;
}

void fillArray(char a[], int size, int& numberUsed)
{
    cout << "Enter up to " << size << " letters or characters.\n"
         << "Mark the end of the list with a negative number.\n";
    int next, index = 0;
    cin >> next;
    while ((next >= 0) && (index < size))
    {
        a[index] = next;
        index++;
        cin >> next;
    }

    numberUsed = index;
}

void sort(char a[], int numberUsed)
{
    int indexOfNext;
    for (int index = 0; index < numberUsed - 1; index++)
    {
        indexOfNext = indexOfFirst(a, index, numberUsed);

    }
}

void deleteRepeat(char a[], int& n)
{
    int i,j,k;

    for(i=0; i<n-1; i++)
        for(j=i+1; j<n; j++)
            if(a[i]==a[j])
            {
                for(k=j; k<n; k++)
                {
                    a[k]=a[k+1];
                        n--;
                        j--;
                }
            }
}

int indexOfFirst(const char a[], int startIndex, int numberUsed)
{
    int min = a[startIndex],
        indexOfMin = startIndex;
    for (int index = startIndex + 1; index < numberUsed; index++)
        if (a[index] < min)
        {
            min = a[index];
            indexOfMin = index;
        }
    return indexOfMin;
}

Dani AI

Generated

, two issues are tripping you up before duplicates are even removed. As noted, the input loop reads into an int but you are asking for letters; as soon as you type a letter the extraction fails and numberUsed stays 0. Read chars (or a whole token/line) and stop on EOF or newline instead of a negative number. Also, your sort never swaps anything; it calls indexOfFirst but does not move the min into place. Either complete the selection sort, or just use std::sort on the used portion of the array.

Your duplicate-removal loop has two hazards: it can read past the end (a[k+1] when k == n-1), and changing j/n inside the shift loop can skip elements. called out the second one. A simpler, linear solution preserves first-appearance order and avoids shifting entirely:

void deleteRepeat(char a[], int& n) {
    bool seen[256] = {false};          // ASCII
    int write = 0;
    for (int read = 0; read < n; ++read) {
        unsigned char c = static_cast<unsigned char>(a[read]);
        if (!seen[c]) {                // first time we see this char
            seen[c] = true;
            a[write++] = a[read];
        }
    }
    n = write;                         // shrink logical size
}

Call it with deleteRepeat(a, numberUsed) and then print only numberUsed elements. Do not pass a hardcoded size = 4. Also ensure your buffer is large enough for the input.

If you do not need to keep the original order, sort then unique the used range: std::sort(a, a + numberUsed); numberUsed = std::unique(a, a + numberUsed) - a;. That yields abcd for abaabcdbab, but the characters end up sorted.

Recommended Answers

All 3 Replies

For starters, your input function can't make up its mind what it's supposed to do.

void fillArray(char a[], int size, int& numberUsed)
{
   cout << "Enter up to " << size << " letters or characters.\n"
      << "Mark the end of the list with a negative number.\n";
   int next, index = 0;
   cin >> next;
   while ((next >= 0) && (index < size))
   {
      a[index] = next;
      index++;
      cin >> next;
   }

   numberUsed = index;
}

You're taking input into an int variable, but you've told the user to enter "letters or characters". Letters are a subset of character, namely a-z and A-Z. Characters are all things you can press on the keyboard, and a few other things. When you enter a letter for that input statement, the input fails, as it's expecting a numeric input. Your loop exits, and the function returns, have read nothing in.

If you want letters, make the input variable type char. Then you'll need a different way to end the loop besides a negative number.

A common way to handle this is a loop controlled by both the limit of the input array and the success of the input action. Use of control-z at the keyboard (control-D in *nix) will simulate end of file action, so this method will also be useful reading from files.

char input;
int count = 0;
while ( count < limit && cin >> input )
{
      // do stuff
      count++;
}

Also, when you post code, please use the code tags correctly. Don't use quote block, just the code tags, like this:

[code]

your code goes here

[/code]

Get that working, then we'll look at your sort and deletion parts.

thanks for the help ...

decrement of n and j should be outside of for loop for k. in the if statement.

void deleteRepeat(char a[], int& n)
{
    int i,j,k;
    for(i=0; i<n-1; i++)
        for(j=i+1; j<n; j++)
            if(a[i]==a[j])
            {
                for(k=j; k<n; k++)
                {
                    a[k]=a[k+1];

                }n--;
                 j--;
            }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.