Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem Statement: Merging and Sorting of characterarrays You are required to wr

ID: 3614907 • Letter: P

Question

Problem Statement: Merging and Sorting of characterarrays

You are required to write a program that takes character valuesin two different arrays as input from user. After getting input,make a third array and merge both these array in third array.Output array must be sorted in ascending order.

Detailed Description:

1. Declarethree character type arrays.

2. Use twoarrays to take input from user.

o  Take input as char type.

o  Take 10 values in each array.

3. Merge boththese input arrays.

o  If an input character is in both arrays, then it should appear oncein resulted array.

4. Storeresult in third array.

5. Sortresulted array in ascending order.

o   Ascending order means string values starting from‘a’ will come first, and then starting from‘b’ and so on.

6. Displaythem after sorting

Sample Output

Enter values in first array: a h b c u v I j k e

Enter values in Second array: y u d f g k I w q a

Merged array: a y h u b d c f g v k I j w q e

  

Sorted Array in ascending order: a b c d e f g h I j k q uv w y

Explanation / Answer

#include<iostream>
#include <string>
#include <cstring>
#include <cctype>

using namespacestd;

const size_t N = 10;
const char space = ' ';
void selectionSort(char *, size_t length);
void printArray(const char *);

int main(int argc, char*argv[]) {
    string line;
    string::iterator i;
    size_t j, k;
    char *a1 = new char[N],
        *a2 = new char[N],
        *merged = newchar[N*2];

    cout<< "For arrays A1 and A2, enter up to " << N <<endl;
    cout << "characters (not counting spaces)"<< endl;
    cout << "A1 > ";
    getline(cin,line);
    for (i = line.begin(), j = 0; (i != line.end())&& (j < N); i++) {
        if (*i != space) a1[j++]= *i;
    }
    cout << "A2 > ";
    getline(cin,line);
    for (i = line.begin(), j = 0; (i != line.end())&& (j < N); i++) {
        if (*i != space) a2[j++]= *i;
    }

    //Merge
    for (j = 0, k = 0; j < N; j++) {
        merged[k++] = a1[j];
        if (strchr(a1,a2[j]) ==NULL) {
           merged[k++] = a2[j];
        }
    }
    cout << "Merged : ";
    printArray(merged);

    //Sort
    selectionSort(merged,k);
    cout << "Sorted : ";
    printArray(merged);

    delete[] a1;
    delete [] a2;
    delete [] merged;

    return0;
}

void printArray(const char*a) {
    for (size_t i = 0; i < strlen(a); i++) {
        cout << a[i]<< " ";
    }
    cout << endl;
}

//
// Selection sort of a char array, ignoring case.
//
void selectionSort(char *a, size_t n) {
    char min, t;

    for(size_t i = 0; i < n; i++) {
        min = i;
        for (size_t j = i+1; j< n; j++) {
           if (tolower(a[j]) < tolower(a[min])) {
               min = j;
           }
        }
        t = a[min];
        a[min] = a[i];
        a[i] = t;
    }
}

#if 0

Sample run:

For arrays A1 and A2,enter up to 10
characters (not counting spaces)
A1 > a h b c u v I j k e
A2 > y u d f g k I w q a
Merged : a y h b d c f u g v I j w k q e
Sorted : a b c d e f g h I j k q u v w y

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote