Write a code in C++

User Generated

wnanune

Programming

Description

Write a piece of code in C++. Function that checks binary file char elements and delete the repeating ones (for example, if in file I have a a b c b, output should be a b c)

User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

#include <iostream>
using namespace std;

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

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

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

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

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

    int size = 4;
    char a[10];

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

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

    return 0;
}

void fillArray(int 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(int a[], int numberUsed)
{
    int indexOfNextSmallest;
    for (int index = 0; index < numberUsed - 1; index++)
    {
        indexOfNext = indexOfFirst(a, index, numberUsed);

    }
}

void delete_Repeats(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 indexOfSmallest(const int 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;


Anonymous
Just what I needed…Fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags