Question
Write function in C++:
int removeDups (string a[], int n); For every sequence of consecutive identical items in a, retain only one item of that sequ
0 0
Add a comment Improve this question Transcribed image text
Answer #1

We can do it as follows,

  1. Define a function removeDups() that accepts an integer array and an integer n, and returns an integer
  2. Declare and initialize non_dup_index to 0
  3. Use for-loop to iterate over each item in string array
  4. Use while-loop to skip to next index if consecutive items are same
  5. Update a[non_dup_index] with non duplicate item
  6. Increment non_dup_index by 1
  7. return non duplicate items count

Program:


#include <iostream>
#include <string.h>
using namespace std;

int removeDups(string a[], int n){  /* function removeDups() */
    int non_dup_index =0;           /* initialize non_dup_index to 0  */
    for(int i=0;i<n;i++){           /* for each item in string array */
        while(i<n-1&&a[i].compare(a[i+1])==0){ /* skip to next item if consecutive items are same */
            i++;                    /* skip to next item */
        }
        a[non_dup_index] = a[i];    /* update non_dup_index with non_dup_index item */
        non_dup_index++;            /* increment non_dup_index */
    }
    return non_dup_index;           /* return non duplicate count */
}

int main()
{
    int n=9;
    string d[9] = {
        "jon","daenerys","samwell","samwell","margaery","margaery", "margaery", "samwell", "samwell"
        
    };
    cout<<"Before : "<<endl;
    for(int i=0;i<n;i++){ /* print items before function call */
        cout<<d[i]<<endl;
    }
    
    n = removeDups(d, n); /* call the function removeDups() */
    
    cout<<endl<<endl<<"After : "<<endl; 
    for(int i=0;i<n;i++){ /* print items after function call */
        cout<<d[i]<<endl;
    }
    return 0;
}

Screenshot:

#include <iostream> #include <string.h> using namespace std; int removeDups (string a[], int n){ /* function removeDups() */

Output:

Before jon daenerys samwell samwell margaery margaery margaery samwell samwell After : jon daenerys samwell margaery samwell

Please don't forget to give a Thumbs Up.

Add a comment
Know the answer?
Add Answer to:
Write function in C++: int removeDups (string a[], int n); For every sequence of consecutive identical...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please help with this function i'm having trouble with: must be written in c++ All of...

    Please help with this function i'm having trouble with: must be written in c++ All of the functions you must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning. Your implementations must not use any global variables whose values may be changed during execution. Your program must build successfully under both Visual C++ and either clang++ or g++. Your program must not use...

  • Can you make this a function? Thank you. int locationOfMin(const string a[], int n): Return the...

    Can you make this a function? Thank you. int locationOfMin(const string a[], int n): Return the position of a string in the array such that that string is < = every string in the array. If there is more than one such string, return the smallest position of such a string. Return -1 if the array has no elements. Here's an example: string people[5] = { "samwell", "jon", "margaery", "daenerys" "tyrion" }: int j = locationOfMin(people, 5);//returns 3, since daenerys...

  • Please help with this function i'm having trouble with: must be written in c++ All of...

    Please help with this function i'm having trouble with: must be written in c++ All of the functions you must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning. Your implementations must not use any global variables whose values may be changed during execution. Your program must build successfully under both Visual C++ and either clang++ or g++. Your program must not use...

  • C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ ONLY Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "c" "" "b" "a" "d" "e" Write a function, int flip(string a[], int n); It reverses the order of the elements of...

  • C++ language Write a function, int flip(string a[], int n); It reverses the order of the...

    C++ language Write a function, int flip(string a[], int n); It reverses the order of the elements of the array and returns n. The variable n will be greater than or equal to zero. Example: string array[6] = { "a", "b", "", "c", "d", "e" }; int q = flip(array, 4); // returns 4 // array now contains: "C" "" "" "a" "d" "e" O Full Screen 1 code.cpp + New #include <string> 2 using namespace std; 3 4- int...

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • This is in C++. (10 points) Write a de-duplication function that iteratively sanitizes (removes) all consecutive...

    This is in C++. (10 points) Write a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string. Consecutive duplicates are a pair of duplicate English alphabets sitting next to each other in the input string. Example: "AA", "KK", etc., are all consecutive duplicates. This function will internally run as many iterations as needed to remove all consecutive duplicates until there is either no consecutive duplicates left, or the string becomes empty (in which the function returns...

  • Must be in C. 2. Show code for function, appendArray, based on the following prototype: int...

    Must be in C. 2. Show code for function, appendArray, based on the following prototype: int appendArray(int array1M[], int iCounti,int array2M[], int iCount2, int iMaxArray1) This function should take the contents of array2M (which has iCount2 elements) and append it onto the end of array1M (which originally has iCount1 elements). The maximum number of elements in array1M should be iMaxArray1. appendArray also returns the new count of items in array1M. Example: array1M 11 12 13 iCount1 3 iMaxArrayl8 array2M 24...

  • Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)

    please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...

  • Write a function getScores(...) that is given a string, an int type array to fill and...

    Write a function getScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted. For example: int values[3]; getScores("123 456 789", values, 3 ); would return the count of 3 and fill the array with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT