0

I'm having some problems with this function. Its main purpose is to sorts the array of classes(below) in descending order by the number of sections. Here is the array of classes:

CIS_CLASSES *pCls, *pLast;
CIS_CLASSES  clsList[NUM_CLS] =
{
    {"CIS 35A", 2, {61, 63}},
    {"CIS 35B", 1, {62}},
    {"CIS 41A", 3, {1, 2, 61}},
    {"CIS 28",  1, {61}},
    {"CIS 22C", 4, {3, 4, 61, 63}},
    {"CIS 26B", 1, {61}},
    {"CIS 22B", 8, {1, 2, 3, 4, 6, 61, 62, 63}},
    {"CIS 29",  1, {61}},
    {"CIS 22A", 8, {1, 3, 5, 6, 7, 8, 61, 63}},
};

I've also defined NUM_CLS using struct:

#define NUM_CLS 9

typedef struct
{
    char  course[10];
    int   noSections;
    int   sections[16];
    int final;
} CIS_CLASSES;`

Here is the function :

void sortDescend(CIS_CLASSES  list[], CIS_CLASSES *pLast);
{
    CIS_CLASSES *pCurr;
    CIS_CLASSES *pWalk;

    for (pCurr = list- 1; pCurr < pLast; pCurr++)
    {
        CIS_CLASSES temp = *pCurr;
        pWalk = pCurr - 1;
        while (pWalk >= 0 && temp.final > pLast->list[pWalk].final)
        {
            pLast->list[pWalk + 1] = pLast->list[pWalk];
            pWalk--;
        }
        pLast->list[pWalk + 1] = temp;
    }
  }
}

When I try to run this code I get the error message: no member named 'list' in 'CIS_CLASSES'. But I don't understand why? and How can I make it run? Please let me know if I'm missing anything!

2
  • What is your intent with this expression: pLast->list[pWalk]? Commented Jan 18, 2021 at 1:17
  • Your parameter is CIS_CLASSES list[] and then you define pCurr = list- 1 but it makes no sense to subtract 1 from an array. Even if it did make sense it would give you a pointer to some memory BEFORE the start of the array (memory that you shouldn't be touching). Then you define pWalk = pCurr - 1; so pWalk is now a pointer to some other memory BEFORE the start of the array (memory that you shouldn't be touching). Then you use that pWalk pointer as an index to the array. But pointers are not array indexes so that part is wrong. I am not sure what you actually meant to do... Commented Jan 18, 2021 at 1:26

3 Answers 3

2

Looking at your structure I don't see an element list there:

typedef struct
{
    char  course[10];
    int   noSections;
    int   sections[16];
    int final;
} CIS_CLASSES;

Therefore

pLast->list[pWalk].final

must fail.

Sign up to request clarification or add additional context in comments.

Comments

0

Please try to do as the following.

void sortDescend(CIS_CLASSES  pInput[], CIS_CLASSES **pOutput);
{
    // pOutput should be declared in your main function as the following
    // CIS_CLASSES **pOutput = (CIS_CLASSES **)malloc(NUM_CLS * sizeof(PVOID));

    // copy pointer arrays of CIS_CLASSES list elements
    for(int i=0; i<NUM_CLS; i++)
    {
        pOutput[i] = &pInput[i];
    }

    // bubble sort
    for (int i=0; i<NUM_CLS-1, i++)
    {
        for (int j=i+1; j<NUM_CLS; j++)
        {
            CIS_CLASSES temp = *pCurr;
            if(pOutput[i]->noSection < pOutput[j]->noSection)
            {
                pCurr = pOutput[i];
                pOutput[i] = pOutput[j];
                pOutput[j] = pCurr;
            }
        }
    }
    // You can access the descending sorted list item like pOutput[index]->course, ... 
}

Comments

0

Thanks All! I updated my code to this :

    void sortDescend(CIS_CLASSES  list[], CIS_CLASSES *pLast);

{

CIS_CLASSES *pCurr;
 CIS_CLASSES *pWalk;

for (pCurr = list+1; pCurr < pLast; pCurr++)
{
    CIS_CLASSES temp = *pCurr;
    pWalk = pCurr - 1;
    while (pWalk >= 0 && strcmp(temp.course, pWalk->course) > 0)
   {
         *(pWalk + 1) = *pWalk;
          pWalk--;
   }
   *(pWalk + 1)  = temp;
}

} }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.