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

I am trying to solve the yodaness problem, i am trying to count the number of in

ID: 3722193 • Letter: I

Question

I am trying to solve the yodaness problem, i am trying to count the number of inversions in a string, but my code is not giving me any output. How can I fix this? I have included my code for reference

#include <iostream>
#include <sstream>
#include <limits>
#include <map>     
#include <string>  


using namespace std;

const int MAX = 30000, LEN = 33;
int total = 0;

map<string, int> index_words(const string &line)
{
        map<string, int> result;
        istringstream in(line);

        string word;
        int index = 0;   
        while (in >> word)
        {

if (result.find(word) == result.end())
                        result[word] = index++;
        }
        return result;
}
void merge(int *a, int p, int q, int r) {
        int i, j, k, n1 = (q-p+1), n2 = (r-q);
        int L[n1], R[n2];
        for(i=0;i<n1;i++) L[i] = a[p+i];
        for(j=0;j<n2;j++) R[j] = a[q+j+q];
        for(k=p, i=j=0; k<=r; k++) {
                if(j>=n2 || (i<n1 && L[i] <=R[j])) a[k] = L[i++];
                else {
                        total += n1-i;
                        a[i] = R[j++];
                }
        }
}
void mergesort(int *a, int p, int r) {
        if(p<r) {
                int q = (p+r)/2;
                mergesort(a,p,q);
                mergesort(a,q+1,r);
                merge(a,p,q,r);
                total++;
        }
}
char word[MAX][LEN];
int a[MAX];
int main()
{
        cin >> instances;
        char temp[LEN];
        for (int i = 0; i < instances; ++i)
        {
                int numwords;
                cin >> numwords;            
                cin.ignore(numeric_limits<streamsize>::max(), ' ';
                string yoda_line, english_line;

                getline(std::cin, yoda_line);
                getline(std::cin, english_line);
          
                auto yoda_idx = index_words(yoda_line);
                auto english_idx = index_words(english_line);

                for(int i = 0; i < instances; i++) a[i] = yoda_idx[word[i]];
                mergesort(a,0, instances-1);
                cout << total;
        }
}

Explanation / Answer

CORRECT the int function

first declare the int function so that it can be read

intindexwords( const) in the beginning

then intindexwords(const&line)

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