1

I know how this should work in my head but for some reason I am not being able to code it.

I need to write a c++ function which takes two character arrays and returns a "cross-product" (not multiplication) combination of them.

string crossProd(const char* a, const char* b, int sizeofa, int sizeofb)

Example: input: a-->"abc", b-->"123" output: a1b2c3

Thank you for any assistance.

  • What have you tried? What didn't work? – Alan Birtles Nov 25 '19 at 06:15
  • Moreover `a1b2c3` has nothing to do with a *cross-product*, you are simply folding two strings together. – David C. Rankin Nov 25 '19 at 06:15
  • @DavidRankin-ReinstateMonica Isn't that a question of how "cross product" is defined? [SO: Cross Concatenate Elements of 2 Vectors to Produce a Third](https://stackoverflow.com/a/44590662/7478597) ;-) – Scheff's Cat Nov 25 '19 at 06:52
  • A `cross-product` is a vector operation (as in physical vector, not C++ dynamic template array) The vector cross-product of `a, b, c` with `1, 2, 3` would result in `(b3-c2)i + (c1-a3)j + (a2-1b)k` in 3-Dimensional space, not `"a1b2c3"`, and the vector result would be orthogonal to both `1,2,3` and `a,b,c` and a *normal* for the plane formed by the original two. – David C. Rankin Nov 25 '19 at 14:45

2 Answers2

1

Try this:

#include <iostream>
#include <string.h>

std::string crossProd( const char* a, const char* b, int sizeofa, int sizeofb)
{
    std::string return_str = "";

    if (sizeofb >= sizeofa)
    {
        for (int i=0; i<=sizeofa; i++)
        {
            return_str = return_str + a[i] +b[i];
        }

        for(int i=sizeofa+1; i<=sizeofb; i++)
        {
            return_str = return_str + b[i];
        }

    }
    else
    {
        for (int i=0; i<=sizeofb; i++)
        {
            return_str = return_str + a[i] +b[i];
        }
        for(int i=sizeofb+1; i<=sizeofa; i++)
        {
            return_str = return_str + a[i];
        }
    }
    return return_str;
}

int main()
{
    const char* a = "Chitta";
    const char* b = "123456";
    std::cout<<a<<":"<<b<<":"<<crossProd(a,b,6,6)<<std::endl;

    const char* a1 = "ChittaRanjan";
    const char* b1 = "12345";
    std::cout<<a1<<":"<<b1<<":"<<crossProd(a1,b1,12,5)<<std::endl;

    const char* a2 = "Chitta";
    const char* b2 = "12345678";
    std::cout<<a2<<":"<<b2<<":"<<crossProd(a2,b2,6,8)<<std::endl;
    return 0;
}

Output:

  • Chitta:123456:C1h2i3t4t5a6

  • ChittaRanjan:12345:C1h2i3t4t5aRanjan

  • Chitta:12345678:C1h2i3t4t5a678

Note: This is not optimized code.

1

Your use of the term cross-product is a bit ambiguous. A Cross product is a binary operation on two vectors in 3-Dimensional space. The result you show as a "cross-produce" is simply folding two strings together by alternating characters from each of the strings until the characters are exhausted.

You have a good answer from @ChittaRajan, but there are numerous ways to approach the problem. On one hand you can simply use operator[] and an index to combine common characters from each string and then add the remaining characters from whichever is longer, e.g.:

std::string fold (const std::string& a, const std::string& b)
{
    std::string s {};
    size_t i = 0, j;

    while (a[i] && b[i]) {  /* alternating add from a & b */
        s += a[i];
        s += b[i++];
    }
    j = i;

    while (a[i])            /* if a longer, add remaining chars */
        s += a[i++];
    while (b[j])
        s += b[j++];        /* if b longer, add remaining chars */

    return s;
}

Of since you have made use of the basic char* type, you can simply us a pointer to the raw character .c_str() for each string and a pair of pointers to accomplish the same thing, e.g.

std::string fold (const std::string& a, const std::string& b)
{
    std::string s {};
    const char *pa = a.c_str(), *pb = b.c_str();

    while (*pa && *pb) {    /* alternating add from a & b */
        s += *pa++;
        s += *pb++;
    }
    while (*pa)             /* if a longer, add remaining chars */
        s += *pa++;
    while (*pb)
        s += *pb++;         /* if b longer, add remaining chars */

    return s;
}

In both cases when paired with a short example:

int main (void) {

    std::string a {"abc"}, b {"123"}, c = fold (a, b);

    std::cout << c << '\n';
}

Example Use/Output

You receive the desire output:

$ ./bin/strfold
a1b2c3

Look over both and let me know if you have further questions.

David C. Rankin
  • 69,681
  • 6
  • 44
  • 72