home / study / questions and answers / engineering / computer science / write a
ID: 3761905 • Letter: H
Question
home / study / questions and answers / engineering / computer science / write a function called delete_repeatss that has ... Question Write a function called delete_repeatss that has a partially filled array of characters as a formal parameter and that deletes all repeated letters from the array . Since a partially filled array requires two arguments , the function will actually have two formal parameters : an array parameter and a formal parameter of type int that gives the number of array positions used. When a letter is deleted, the remaining letters are moved forward to fill in the gap. This will create empty positions at the end of the array so that less of the array is used. Since the formal parameter is a partially filled array , a second formal parameter of type int will tell how many array positions are filled. This second formal parameter will be a call-by-reference parameter and will be changed to show how much of the array is used after the repeated letters are deleted. For example, consider the following code: char a[10]; a[0] = 'a'; a[1] = 'b'; a[2] = 'a'; a[3] = 'c'; int size = 4; delete_repeats(a, size); After this code is executed, the value of a[0] is 'a', the value of a[1] is 'b', the value of a[2] is 'c', and the value of size is 3. (The value of a[3] is no longer of any concern, since the partially filled array no longer uses this indexed variable .) You may assume that the partially filled array contains only lowercase letters. Embed your function in a test program that reads all the characters on a line (up to but and including the newline character ) and stores all of them EXCEPT the newline character in an array . Assume no more than 80 characters per line. The test program keeps count of the number of characters actually stored in the array . The test program prints out the number of characters stored and then prints out the characters on a single line twice. The program then passes the array to the function delete_repeats and now prints the characters effectively stored in the array on a single line followed by the number of characters that are effectively stored in the array this code must allow the user to input a sentence and use those letters for the code
Explanation / Answer
#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 << " Program to sort characters and to delete repeated characters. ";
int size = 4, numberUsed;
char a[10];
fillArray(a, 10, numberUsed);
sort(a, numberUsed);
deleteRepeat(a, size);
cout << "In sorted order the letters are: ";
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. "
<< "Mark the end of the list with a negative number. ";
char input;
int count=0;
while (count<size && cin >> input)
{
a[count] = input;
count++;
cin >> input;
}
numberUsed = count;
}
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.